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
GROUPBY Region
ORDERBY Region
East's four sales values give 9,218.75. South has one value: 0, since one value does not vary.
Region
Result
East
9,219
North
null
South
0
West
8,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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, var_pop(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
9,219
12,292
9,219
North
null
null
null
South
0
null
0
West
8,750
11,667
8,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.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
VARP([Sales])Region, var_pop(Sales)
9,219
null
0
8,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
GROUPBY Region
ORDERBY Region