---
schema_version: "1"
id: 5e91fe1f3d16f5f82195e48a92bdfa8bfb9fc7fed4cd048716c25cc21d4a2628
title: Aggregate Functions in Tableau
collection: tableau
version: current
locale: en-us
portal_url: https://docs.salesforce.com/tableau/current/en-us/pro/desktop/calculations_calculatedfields_aggregate_create.md
canonical_url: https://help.tableau.com/current/pro/desktop/en-us/calculations_calculatedfields_aggregate_create.htm
source_site: help.tableau.com
content_type: documentation
content_hash: e4bda882b5e6315eec3f7ecf5bcea858bebf26bf241f83f1c8ce2a90bcfdb57c
description: Reference of Tableau aggregate functions like AVG, COUNT, and CORR, with an example calculation.
---
[Skip to main content](#main-content)

# Aggregate Functions in Tableau

This article introduces aggregate functions and their uses in Tableau. It also demonstrates how to create an aggregate calculation using an example.

## Why use aggregate functions

Aggregate functions allow you to summarize or change the granularity of your data.

For example, you might want to know exactly how many orders your store had for a particular year. You can use the COUNTD function to tally the exact number of unique orders your company had, and then break the visualization down by year.

The calculation might look something like this:

`COUNTD(Order ID)`

The visualization might look something like this:

![](/tableau/current/en-us/pro/desktop/en-us/Img/aggregate_function1.png)

## Aggregate functions available in Tableau

**Aggregations and floating-point arithmetic**: The results of some aggregations may not always be exactly as expected. For example, you may find that the SUM function returns a value such as -1.42e-14 for a column of numbers that you know should sum to exactly 0. This happens because the Institute of Electrical and Electronics Engineers (IEEE) 754 floating-point standard requires that numbers be stored in binary format, which means that numbers are sometimes rounded at extremely fine levels of precision. You can eliminate this potential distraction by using the ROUND function (see [Number Functions](https://help.tableau.com/current/pro/desktop/en-us/functions_functions_number.htm)) or by formatting the number to show fewer decimal places.

### ATTR

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>ATTR(expression)</code></td></tr><tr><td>Definition</td><td>Returns the value of the expression if it has a single value for all rows. Otherwise returns an asterisk. Null values are ignored.</td></tr></tbody></table>

### AVG

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>AVG(expression)</code></td></tr><tr><td>Definition</td><td>Returns the average of all the values in the expression. Null values are ignored.</td></tr><tr><td>Notes</td><td><code>AVG</code> can only be used with numeric fields.</td></tr></tbody></table>

### COLLECT

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>COLLECT(spatial)</code></td></tr><tr><td>Definition</td><td>An aggregate calculation that combines the values in the argument field. Null values are ignored.</td></tr><tr><td>Notes</td><td><code>COLLECT</code> can only be used with spatial fields.</td></tr></tbody></table>

### CORR

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>CORR(expression1, expression2)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Number from -1 to 1</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns the Pearson correlation coefficient of two expressions.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre>example</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>The Pearson correlation measures the linear relationship between two variables. Results range from -1 to +1 inclusive, where 1 denotes an exact positive linear relationship, 0 denotes no linear relationship between the variance, and −1 is an exact negative relationship.</p>
                                <p>The square of a CORR result is equivalent to the R-Squared value for a linear trend line model. See <a href="https://help.tableau.com/current/pro/desktop/en-us/trendlines_add.htm#Terms" target="_blank">Trend Line Model Terms</a>.</p>
                                <p><b>Use with table scoped LOD&nbsp;expressions:</b>
                                </p>
                                <p>You can use CORR to visualize correlation in a disaggregated scatter using a <a href="https://help.tableau.com/current/pro/desktop/en-us/calculations_calculatedfields_lod.htm#Table" target="_blank">table-scoped level of detail expression</a>. For example:</p><pre>{CORR(Sales, Profit)}</pre>
                                <p>With a level of detail expression, the correlation is run over all rows. If you used a formula like <code>CORR(Sales, Profit)</code> (without the surrounding brackets to make it a level of detail expression), the view would show the correlation of each individual point in the scatter plot with each other point, which is undefined.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Database limitations</td>
                            <td>
                                <p><code>CORR</code> is available with the following data sources: Tableau data extracts, Cloudera Hive, EXASolution, Firebird (version 3.0 and later), Google BigQuery, Hortonworks Hadoop Hive, IBM PDA (Netezza), Oracle, PostgreSQL, Presto, SybaseIQ, Teradata, Vertica.</p>
                                <p>For other data sources, consider either extracting the data or using <code>WINDOW_CORR</code>. See <a href="https://help.tableau.com/current/pro/desktop/en-us/functions_functions_tablecalculation.htm" target="_blank">Table Calculation Functions</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### COUNT

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>COUNT(expression)</code></td></tr><tr><td>Definition</td><td>Returns the number of items. Null values are not counted.</td></tr></tbody></table>

### COUNTD

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>COUNTD(expression)</code></td></tr><tr><td>Definition</td><td>Returns the number of distinct items in a group. Null values are not counted.</td></tr></tbody></table>

### COVAR

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>COVAR(expression1, expression2)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns the <i>sample</i> covariance of two expressions.</td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Covariance quantifies how two variables change together. A positive covariance indicates that the variables tend to move in the same direction, as when larger values of one variable tend to correspond to larger values of the other variable, on average. <i>Sample covariance</i> uses the number of non-null data points n - 1 to normalize the covariance calculation, rather than n, which is used by the population covariance (available with the <code>COVARP</code> function). Sample covariance is the appropriate choice when the data is a random sample that is being used to estimate the covariance for a larger population.</p>
                                <p>If <code>&lt;expression1&gt;</code> and <code>&lt;expression2&gt;</code> are the same, for example <code>COVAR([profit], [profit])</code>, <code>COVAR</code> returns a value that indicates how widely values are distributed.</p>
                                <p>The value of <code>COVAR(X, X)</code> is equivalent to the value of <code>VAR(X)</code> and also to the value of <code>STDEV(X)^2</code>.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Database limitations</td>
                            <td>
                                <p><code>COVAR</code> is available with the following data sources: Tableau data extracts, Cloudera Hive, EXASolution, Firebird (version 3.0 and later), Google BigQuery, Hortonworks Hadoop Hive, IBM PDA (Netezza), Oracle, PostgreSQL, Presto, SybaseIQ, Teradata, Vertica.</p>
                                <p>For other data sources, consider either extracting the data or using <code>WINDOW_COVAR</code>. See <a href="https://help.tableau.com/current/pro/desktop/en-us/functions_functions_tablecalculation.htm" target="_blank">Table Calculation Functions</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### COVARP

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>COVARP(expression 1, expression2)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns the <i>population</i> covariance of two expressions.</td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Covariance quantifies how two variables change together. A positive covariance indicates that the variables tend to move in the same direction, as when larger values of one variable tend to correspond to larger values of the other variable, on average. <i>Population covariance</i> is sample covariance multiplied by (n-1)/n, where n is the total number of non-null data points. Population covariance is the appropriate choice when there is data available for all items of interest as opposed to when there is only a random subset of items, in which case sample covariance (with the <code>COVAR</code> function) is appropriate.
</p>
                                <p>If <code>&lt;expression1&gt;</code> and <code>&lt;expression2&gt;</code> are the same, for example <code>COVARP([profit], [profit])</code>, <code>COVARP</code> returns a value that indicates how widely values are distributed.

Note: The value of <code>COVARP(X, X)</code> is equivalent to the value of <code>VARP(X)</code> and also to the value of <code>STDEVP(X)^2</code>.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Database limitations</td>
                            <td>
                                <p><code>COVARP</code> is available with the following data sources: Tableau data extracts, Cloudera Hive, EXASolution, Firebird (version 3.0 and later), Google BigQuery, Hortonworks Hadoop Hive, IBM PDA (Netezza), Oracle, PostgreSQL, Presto, SybaseIQ, Teradata, Vertica</p>
                                <p>For other data sources, consider either extracting the data or using <code>WINDOW_COVAR</code>. See <a href="https://help.tableau.com/current/pro/desktop/en-us/functions_functions_tablecalculation.htm" target="_blank">Table Calculation Functions</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### MAX

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>MAX(expression)</code> or <code>MAX(expr1, expr2)</code></td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Same data type as the argument, or <code>NULL&nbsp;</code>if any part of the argument is null. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns the maximum of the two arguments, which must be of the same data type. </p>
                                <p><code>MAX</code> can also be applied to a single field as an aggregation.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">MAX(4,7) = 7<br>MAX(#3/25/1986#, #2/20/2021#) = #2/20/2021# <br>MAX([Name]) = "Zander"</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p><b>For strings</b>
                                </p>
                                <p><code>MAX</code> is usually the value that comes last in alphabetical order. </p>
                                <p class="note">For database data sources, the <code>MAX</code> string value is highest in the sort sequence defined by the database for that column.</p>
                                <p><b>For dates</b>
                                </p>
                                <p>For dates, the <code>MAX</code> is the most recent date. If <code>MAX</code> is an aggregation, the result will not have a date hierarchy. If <code>MAX</code> is a comparison, the result will retain the date hierarchy. </p>
                                <p><b>As an aggregation</b>
                                </p>
                                <p><code>MAX(expression)</code> is an aggregate function and returns a single aggregated result. This displays as <code>AGG(expression)</code> in the viz. </p>
                                <p><b>As a comparison</b>
                                </p>
                                <p><code>MAX(expr1, expr2)</code> compares the two values and returns a row-level value.</p>
                                <p>See also <code><a href="#MIN" class="xref">MIN</a></code>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### MEDIAN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>MEDIAN(expression)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns the median of an expression across all records. Null values are ignored.</td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td><code>MEDIAN</code> can only be used with numeric fields. </td>
                        </tr>
                        <tr>
                            <td>Database limitations</td>
                            <td>
                                <p><code>MEDIAN</code> is <b>not</b> available for the following data sources: Access, Amazon Redshift, Cloudera Hadoop, HP Vertica, IBM DB2, IBM PDA (Netezza), Microsoft SQL Server, MySQL, SAP HANA, Teradata.</p>
                                <p>For other data source types, you can extract your data into an extract file to use this function. See <a href="https://help.tableau.com/current/pro/desktop/en-us/extracting_data.htm" target="_blank">Extract Your Data</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### MIN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>MIN(expression)</code> or <code>MIN(expr1, expr2)</code></td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Same data type as the argument, or <code>NULL&nbsp;</code>if any part of the argument is null. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns the minimum of the two arguments, which must be of the same data type. </p>
                                <p><code>MIN </code>can also be applied to a single field as an aggregation.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">MIN(4,7) = 4<br>MIN(#3/25/1986#, #2/20/2021#) = #3/25/1986#<br>MIN([Name]) = "Abebi"</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p><b>For strings</b>
                                </p>
                                <p><code>MIN</code> is usually the value that comes first in alphabetical order. </p>
                                <p class="note">For database data sources, the <code>MIN</code> string value is lowest in the sort sequence defined by the database for that column.</p>
                                <p><b>For dates</b>
                                </p>
                                <p>For dates, the <code>MIN</code> is the earliest date. If <code>MIN</code> is an aggregation, the result will not have a date hierarchy. If <code>MIN</code> is a comparison, the result will retain the date hierarchy. </p>
                                <p><b>As an aggregation</b>
                                </p>
                                <p><code>MIN(expression)</code> is an aggregate function and returns a single aggregated result. This displays as <code>AGG(expression)</code> in the viz. </p>
                                <p><b>As a comparison</b>
                                </p>
                                <p><code>MIN(expr1, expr2)</code> compares the two values and returns a row-level value.</p>
                                <p>See also <code><a href="#MAX" class="xref">MAX</a></code>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### PERCENTILE

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>PERCENTILE(expression, number)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns the percentile value from the given expression corresponding to the specified <code>&lt;number&gt;</code>. The <code>&lt;number&gt;</code> must be between 0 and 1 (inclusive) and must be a numeric constant.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">PERCENTILE([Score], 0.9)</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Database limitations</td>
                            <td>
                                <p>This function is available for the following data sources:&nbsp;Non-legacy Microsoft Excel and Text File connections, Extracts and extract-only data source types (for example, Google Analytics, OData, or Salesforce), Sybase IQ 15.1 and later data sources, Oracle 10 and later data sources, Cloudera Hive and Hortonworks Hadoop Hive data sources, EXASolution 4.2 and later data sources.</p>
                                <p>For other data source types, you can extract your data into an extract file to use this function. See <a href="https://help.tableau.com/current/pro/desktop/en-us/extracting_data.htm" target="_blank">Extract Your Data</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### STDEV

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>STDEV(expression)</code></td></tr><tr><td>Definition</td><td>Returns the statistical standard deviation of all values in the given expression based on a sample of the population.</td></tr></tbody></table>

### STDEVP

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>STDEVP(expression)</code></td></tr><tr><td>Definition</td><td>Returns the statistical standard deviation of all values in the given expression based on a biased population.</td></tr></tbody></table>

### SUM

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>SUM(expression)</code></td></tr><tr><td>Definition</td><td>Returns the sum of all values in the expression. Null values are ignored.</td></tr><tr><td>Notes</td><td><code>SUM</code> can only be used with numeric fields.</td></tr></tbody></table>

### VAR

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>VAR(expression)</code></td></tr><tr><td>Definition</td><td>Returns the statistical variance of all values in the given expression based on a sample of the population.</td></tr></tbody></table>

### VARP

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"> <col></colgroup><tbody><tr><td>Syntax</td><td><code>VARP(expression)</code></td></tr><tr><td>Definition</td><td>Returns the statistical variance of all values in the given expression on the entire population.</td></tr></tbody></table>

  

## Create an aggregate calculation

Follow along with the steps below to learn how to create an aggregate calculation.

1. In Tableau Desktop, connect to the **Sample - Superstore** saved data source, which comes with Tableau.
2. Navigate to a worksheet and select Analysis \> Create Calculated Field.
3. In the calculation editor that opens, do the following:
    
    - Name the calculated field Margin.
    - Enter the following formula:
        
        `IIF(SUM([Sales]) !=0, SUM([Profit])/SUM([Sales]), 0)`
        
        **Note**: You can use the function reference to find and add aggregate functions and other functions (like the logical IIF function in this example) to the calculation formula. For more information, see [Use the functions reference in the calculation editor](https://help.tableau.com/current/pro/desktop/en-us/calculations_calculatedfields_tips.htm#functions).
    - When finished, click OK.
    
    The new aggregate calculation appears under **Measures** in the **Data** pane. Just like your other fields, you can use it in one or more visualizations.
    
    **Note**: Aggregation calculations are always measures.
    
    When Margin is placed on a shelf or card in the worksheet, its name is changed to AGG(Margin), which indicates that it is an aggregate calculation and cannot be aggregated any further.
    
    ![A graphic depicting what happens when you place an aggregate
    calculation on a shelf. The field displays the AGG prefix.](/tableau/current/en-us/pro/desktop/en-us/Img/calc_agg8.png)

## Rules for aggregate calculations

The rules that apply to aggregate calculations are as follows:

- For any aggregate calculation, you cannot combine an aggregated value and a disaggregated value. For example, `SUM(Price)*[Items]` is not a valid expression because SUM(Price) is aggregated and Items is not. However, `SUM(Price*Items)` and `SUM(Price)*SUM(Items)` are both valid.
- Constant terms in an expression act as aggregated or disaggregated values as appropriate. For example: `SUM(Price*7)` and `SUM(Price)*7` are both valid expressions.
- All of the functions can be evaluated on aggregated values. However, the arguments to any given function must either all be aggregated or all disaggregated. For example: `MAX(SUM(Sales),Profit)` is not a valid expression because Sales is aggregated and Profit is not. However, `MAX(SUM(Sales),SUM(Profit))` is a valid expression.
- The result of an aggregate calculation is always a measure. This includes expressions like ATTR(Dimension) or MIN(Dimension).
- Like predefined aggregations, aggregate calculations are computed correctly for grand totals. Refer to Grand Totals for more information.

## See also

[Understanding Calculations: Aggregate Calculations](https://help.tableau.com/current/pro/desktop/en-us/calculations_calculatedfields_understand_types.htm#Aggregate)

[Data Aggregation in Tableau](https://help.tableau.com/current/pro/desktop/en-us/calculations_aggregation.htm)

[Functions in Tableau](https://help.tableau.com/current/pro/desktop/en-us/functions.htm)

[Tableau Functions (by Category)](https://help.tableau.com/current/pro/desktop/en-us/functions_all_categories.htm)

[Tableau Functions (Alphabetical)](https://help.tableau.com/current/pro/desktop/en-us/functions_all_alphabetical.htm)