Tableau function Aggregate

Tableau VARP to SQL

VARP(expression) returns the population variance of the non-null values, dividing by n. In SQL that is var_pop(Sales). Watch the name: in DuckDB, Snowflake and BigQuery the unqualified VARIANCE is the sample version, not this one.

Updated · Checked on the xmetric engine

What is the syntax?

VARP(expression)
expression
a numeric field or row-level calculation.

Returns a real number: the population variance, dividing by n. Null when the group has no values; 0 when it has one.

What does it return, and what is the SQL?

Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What is the variance of sales in each region?

Tableau

VARP([Sales])

SQL (DuckDB)

SELECT Region, var_pop(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

East's four sales values give 9,218.75. South has one value: 0, since one value does not vary.

RegionResult
East9,219
Northnull
South0
West8,750

The Tableau formula and the SQL return the same 4 rows.

Where does a straight translation go wrong?

Common mistake

Why does DuckDB's variance() give a bigger variance?

In DuckDB, Snowflake and BigQuery, plain VARIANCE means the sample variance (divide by n - 1), Tableau's VAR. VARP is the population version and divides by n, so write var_pop explicitly. South's single value shows it: 0 here, null for variance.

Tableau

VARP([Sales])

Tempting SQL

SELECT Region, variance(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, var_pop(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East9,21912,2929,219
Northnullnullnull
South0null0
West8,75011,6678,750

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does VARP return for an all-null group?

North has two order lines and no sales value on either. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupVARP([Sales])Region, var_pop(Sales)9,219null08,750

On every row, each Tableau formula and its SQL return the same value.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT Region, VAR_POP(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT Region, VAR_POP(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own VARP formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Product], [Sales], [Units].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about VARP?

Should I use VARP or VAR?

Use the population version when your rows are every item you care about, not a sample. VAR is the sample version.

Does VARP ignore nulls?

Yes. Empty values are left out of both the mean and the count, as in SQL.

What does VARP return for one value?

0: a single value has no spread. South has one sale and returns 0.

Where is this documented?