Tableau function Aggregate

Tableau VAR to SQL

VAR(expression) returns the sample variance of the non-null values, dividing by n - 1. In SQL that is var_samp(Sales). A group with a single value has no sample variance and returns null.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a real number: the sample variance, dividing by n - 1. Null when the group has no values or only 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

VAR([Sales])

SQL (DuckDB)

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

East's four sales values give 12,291.67. South has one value: null, since a sample needs two.

RegionResult
East12,292
Northnull
Southnull
West11,667

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

Where does a straight translation go wrong?

Common mistake

Why does var_pop give a smaller variance?

VAR is the sample variance: it divides by n - 1. var_pop divides by n, the population version (VARP). East comes out at 12,291.67 against 9,218.75 for the population version.

Tableau

VAR([Sales])

Tempting SQL

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

SQL that matches

SELECT Region, var_samp(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East12,2929,21912,292
Northnullnullnull
Southnull0null
West11,6678,75011,667

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

What does VAR return for an all-null group and a single value?

North has two order lines and no sales value on either. Per product, Lamp has one line with a sale (row 4); the North lines have no product and no sales: null for both. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupVAR([Sales])Region, var_samp(Sales)12,292nullnull11,667
one valueVAR([Sales])Product, var_samp(Sales)1339,250nullnull

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_SAMP(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own VAR 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 VAR?

Should I use VAR or VARP?

Use the sample version when your rows are a sample of a bigger population, which is the usual case. VARP is the population version.

Does VAR ignore nulls?

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

What does VAR return for one value?

Null: dividing by n - 1 = 0 is undefined. South has one sale and returns null.

Where is this documented?