Design Views and Analyze Data
>
Advanced Analysis
>
Calculated Fields
>
Functions
>
Table Calculation Functions

Applies to: Tableau Desktop, Tableau Online, Tableau Public, Tableau Server

Use table calculation functions to customize table calculations. Table Calculations are computations that are applied to the values in the entire table and are often dependent on the table structure itself. See Customize Table Calculations for information about creating and customizing table calculations.

Returns the number of rows from the current row to the first row in the partition. For example, the view below shows quarterly sales. When FIRST() is computed within the Date partition, the offset of the first row from the second row is -1.

When the current row index is 3, ```
FIRST()
= -2
```

.

Returns the index of the current row in the partition, without any sorting with regard to value. The first row index starts at 1. For example, the table below shows quarterly sales. When INDEX() is computed within the Date partition, the index of each row is 1, 2, 3, 4..., etc.

For the third row in the partition, `INDEX() = 3`

.

Returns the number of rows from the current row to the last row in the partition. For example, the table below shows quarterly sales. When LAST() is computed within the Date partition, the offset of the last row from the second row is 5.

When the current row index is 3
of 7, `LAST() = 4`

.

Returns the
value of the expression in a target row, specified as a relative
offset from the current row. Use FIRST() + n and LAST() - n as part of your offset definition for
a target relative to the first/last rows in the partition. If `offset`

is omitted, the row to compare to can be set on the field menu. This function returns
NULL if the target row cannot be determined.

The view below shows quarterly sales. When `LOOKUP (SUM(Sales), 2)`

is computed within the Date partition, each row shows the sales
value from 2 quarters into the future.

```
LOOKUP(SUM([Profit]),
FIRST()+2)
```

computes the SUM(Profit) in the third row of the partition.

Returns the value of this calculation in the previous row. Returns the given expression if the current row is the first row of the partition.

`SUM([Profit]) * PREVIOUS_VALUE(1)`

computes the running product of SUM(Profit).

Returns the standard competition rank for the current row in the partition. Identical values are assigned an identical rank. Use the optional `'asc' | 'desc'`

argument to specify ascending or descending order. The default is descending.

With this function, the set of values (6, 9, 9, 14) would be ranked (4, 2, 2, 1).

Nulls are ignored in ranking functions. They are not numbered and they do not count against the total number of records in percentile rank calculations.

For information on different ranking options, see Table Calculation Type: Rank.

The following image shows the effect of the various ranking functions (RANK, RANK_DENSE, RANK_MODIFIED, RANK_PERCENTILE, and RANK_UNIQUE) on a set of values. The data set contains information on 14 students (StudentA through StudentN); the Age column shows the current age of each student (all students are between 17 and 20 years of age). The remaining columns show the effect of each rank function on the set of age values, always assuming the default order (ascending or descending) for the function.

Returns the dense rank for the current row in the partition. Identical values are assigned an identical rank, but no gaps are inserted into the number sequence. Use the optional `'asc' | 'desc'`

argument to specify ascending or descending order. The default is descending.

With this function, the set of values (6, 9, 9, 14) would be ranked (3, 2, 2, 1).

Nulls are ignored in ranking functions. They are not numbered and they do not count against the total number of records in percentile rank calculations.

For information on different ranking options, see Table Calculation Type: Rank.

Returns the modified competition rank for the current row in the partition. Identical values are assigned an identical rank. Use the optional `'asc' | 'desc'`

argument to specify ascending or descending order. The default is descending.

With this function, the set of values (6, 9, 9, 14) would be ranked (4, 3, 3, 1).

Nulls are ignored in ranking functions. They are not numbered and they do not count against the total number of records in percentile rank calculations.

For information on different ranking options, see Table Calculation Type: Rank.

Returns the percentile rank for the current row in the partition. Use the optional `'asc' | 'desc'`

argument to specify ascending or descending order. The default is ascending.

With this function, the set of values (6, 9, 9, 14) would be ranked (0.25, 0.75, 0.75, 1.00).

For information on different ranking options, see Table Calculation Type: Rank.

Returns the unique rank for the current row in the partition. Identical values are assigned different ranks. Use the optional `'asc' | 'desc'`

argument to specify ascending or descending order. The default is descending.

With this function, the set of values (6, 9, 9, 14) would be ranked (4, 2, 3, 1).

For information on different ranking options, see Table Calculation Type: Rank.

Returns the running average of the given expression, from the first row in the partition to the current row.

The view below shows quarterly
sales. When `RUNNING_AVG(SUM([Sales])`

is computed within the Date
partition, the result is a running average of the sales values for
each quarter.

`RUNNING_AVG(SUM([Profit]))`

computes the running average of SUM(Profit).

Returns the running count of the given expression, from the first row in the partition to the current row.

`RUNNING_COUNT(SUM([Profit]))`

computes the running count of SUM(Profit).

Returns the running maximum of the given expression, from the first row in the partition to the current row.

`RUNNING_MAX(SUM([Profit]))`

computes the running maximum of SUM(Profit).

Returns the running minimum of the given expression, from the first row in the partition to the current row.

`RUNNING_MIN(SUM([Profit]))`

computes the running minimum of SUM(Profit).

Returns the running sum of the given expression, from the first row in the partition to the current row.

`RUNNING_SUM(SUM([Profit]))`

computes the running sum of SUM(Profit)

Returns the number of rows in the partition. For example, the view below shows quarterly sales. Within the Date partition, there are seven rows so the Size() of the Date partition is 7.

`SIZE() = 5`

when the current partition contains five rows.

Returns a Boolean result from the specified expression. The expression is passed directly to a running external service instance.

In R expressions, use .arg*n* (with a leading period) to reference parameters (.arg1, .arg2, etc.).

In Python expressions, use _arg*n* (with a leading underscore).

In this R example, .arg1 is equal to SUM([Profit]):

`SCRIPT_BOOL("is.finite(.arg1)", SUM([Profit]))`

The next example returns True for store IDs in Washington state, and False otherwise. This example could be the definition for a calculated field titled IsStoreInWA.

`SCRIPT_BOOL('grepl(".*_WA", .arg1, perl=TRUE)',ATTR([Store ID]))`

A command for Python would take this form:

`SCRIPT_BOOL("return map(lambda x : x > 0, _arg1)", SUM([Profit]))`

Returns an integer result from the specified expression. The expression is passed directly to a running external service instance.

In R expressions, use .arg*n* (with a leading period) to reference parameters (.arg1, .arg2, etc.)

In Python expressions, use _arg*n* (with a leading underscore).

In this R example, .arg1 is equal to SUM([Profit]):

`SCRIPT_INT("is.finite(.arg1)", SUM([Profit]))`

In the next example, k-means clustering is used to create three clusters:

`SCRIPT_INT('result <- kmeans(data.frame(.arg1,.arg2,.arg3,.arg4), 3);result$cluster;', SUM([Petal length]), SUM([Petal width]),SUM([Sepal length]),SUM([Sepal width]))`

A command for Python would take this form:

`SCRIPT_INT("return map(lambda x : int(x * 5), _arg1)", SUM([Profit]))`

Returns a real result from the specified expression. The expression is passed directly to a running external service instance. In

R expressions, use .arg*n* (with a leading period) to reference parameters (.arg1, .arg2, etc.)

In Python expressions, use _arg*n* (with a leading underscore).

In this R example, .arg1 is equal to SUM([Profit]):

`SCRIPT_REAL("is.finite(.arg1)", SUM([Profit]))`

The next example converts temperature values from Celsius to Fahrenheit.

`SCRIPT_REAL('library(udunits2);ud.convert(.arg1, "celsius", "degree_fahrenheit")',AVG([Temperature]))`

A command for Python would take this form:

`SCRIPT_REAL("return map(lambda x : x * 0.5, _arg1)", SUM([Profit]))`

Returns a string result from the specified expression. The expression is passed directly to a running external service instance.

In R expressions, use .arg*n* (with a leading period) to reference parameters (.arg1, .arg2, etc.)

In Python expressions, use _arg*n* (with a leading underscore).

In this R example, .arg1 is equal to SUM([Profit]):

`SCRIPT_STR("is.finite(.arg1)", SUM([Profit]))`

The next example extracts a state abbreviation from a more complicated string (in the original form 13XSL_CA, A13_WA):

`SCRIPT_STR('gsub(".*_", "", .arg1)',ATTR([Store ID]))`

A command for Python would take this form:

`SCRIPT_STR("return map(lambda x : x[:2], _arg1)", ATTR([Region]))`

Returns the total for the given expression in a table calculation partition.

Assume you are starting with this view:

You open the calculation editor and create a new field which you name Totality:

You then drop Totality on Text, to replace SUM(Sales). Your view changes such that it sums values based on the default Compute Using value:

This raises the question, What is the default Compute Using value? If you right-click (Control-click on a Mac) Totality in the Data pane and choose Edit, there is now an additional bit of information available:

The default Compute Using value is Table (Across). The result is that Totality is summing the values across each row of your table. Thus, the value that you see across each row is the sum of the values from the original version of the table.

The values in the 2011/Q1 row in the original table were $8601, $6579, $44262, and $15006. The values in the table after Totality replaces SUM(Sales) are all $74,448, which is the sum of the four original values.

Notice the triangle next to Totality after you drop it on Text:

This indicates that this field is using a table calculation. You can right-click the field and choose Edit Table Calculation to redirect your function to a different Compute Using value. For example, you could set it to Table (Down). In that case, your table would look like this:

Returns the average of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

For example, the view below shows quarterly sales. A window average within the Date partition returns the average sales across all dates.

`WINDOW_AVG(SUM([Profit]), FIRST()+1, 0)`

computes the average of
SUM(Profit) from the second row to the current row.

Returns the Pearson correlation coefficient of two expressions within the window. The window is defined as offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If start and end are omitted, the entire partition is used.

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, as when a positive change in one variable implies a positive change of corresponding magnitude in the other, 0 denotes no linear relationship between the variance, and −1 is an exact negative relationship.

There is an equivalent aggregation fuction: CORR. See Aggregate Functions.

The following formula returns the Pearson correlation of SUM(Profit) and SUM(Sales) from the five previous rows to the current row.

`WINDOW_CORR(SUM[Profit]), SUM([Sales]), -5, 0)`

Returns the count of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_COUNT(SUM([Profit]), FIRST()+1, 0)`

computes the count of SUM(Profit)
from the second row to the current row

Returns the *sample covariance* of two expressions within the window. The window is defined as offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end arguments are omitted, the window is the entire partition.

Sample covariance 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 (with the WINDOW_COVARP 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.

There is an equivalent aggregation fuction: COVAR. See Aggregate Functions.

The following formula returns the sample covariance of SUM(Profit) and SUM(Sales) from the two previous rows to the current row.

`WINDOW_COVAR(SUM([Profit]), SUM([Sales]), -2, 0)`

Returns the *population covariance* of two expressions within the window. The window is defined as offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If start and end are omitted, the entire partition is used.

Population covariance 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 WINDOW_COVAR function) is appropriate.

There is an equivalent aggregation fuction: COVARP. See Aggregate Functions.

The following formula returns the population covariance of SUM(Profit) and SUM(Sales) from the two previous rows to the current row.

`WINDOW_COVARP(SUM([Profit]), SUM([Sales]), -2, 0)`

Returns the median of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

For example, the view below shows quarterly profit. A window median within the Date partition returns the median profit across all dates.

`WINDOW_MEDIAN(SUM([Profit]), FIRST()+1, 0)`

computes the median
of SUM(Profit) from the second row to the current row.

Returns the maximum of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

For example, the view below shows quarterly sales. A window maximum within the Date partition returns the maximum sales across all dates.

`WINDOW_MAX(SUM([Profit]), FIRST()+1, 0)`

computes the maximum of
SUM(Profit) from the second row to the current row.

Returns the minimum of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

For example, the view below shows quarterly sales. A window minimum within the Date partition returns the minimum sales across all dates.

`WINDOW_MIN(SUM([Profit]), FIRST()+1, 0)`

computes the minimum of
SUM(Profit) from the second row to the current row.

Returns the value corresponding to the specified percentile within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_PERCENTILE(SUM([Profit]), 0.75, -2, 0)`

returns the 75th percentile for SUM(Profit) from the two previous rows to the current row.

Returns the sample standard deviation of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_STDEV(SUM([Profit]), FIRST()+1, 0)`

computes the standard deviation of SUM(Profit)
from the second row to the current row.

Returns the biased standard deviation of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_STDEVP(SUM([Profit]), FIRST()+1, 0)`

computes the standard deviation of SUM(Profit)
from the second row to the current row.

Returns the sum of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

For example, the view below shows quarterly sales. A window sum computed within the Date partition returns the summation of sales across all quarters.

`WINDOW_SUM(SUM([Profit]), FIRST()+1, 0)`

computes the sum of SUM(Profit) from the second row to
the current row.

Returns the sample variance of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_VAR((SUM([Profit])), FIRST()+1, 0)`

computes the variance of SUM(Profit)
from the second row to the current row.

Returns the biased variance of the expression within the window. The window is defined by means of offsets from the current row. Use FIRST()+n and LAST()-n for offsets from the first or last row in the partition. If the start and end are omitted, the entire partition is used.

`WINDOW_VARP(SUM([Profit]), FIRST()+1, 0)`

computes the variance of SUM(Profit)
from the second row to the current row.