RUNNING_VAR(SUM([Sales])) is the sample variance (n - 1 in the denominator) of the marks from the start of the partition to the current one. In SQL that is var_samp(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The first month is null: one mark has no sample variance.
Updated · Checked on the xmetric engine
What is the syntax?
RUNNING_VAR(expression)
expression
an aggregate, such as SUM([Sales]). Compute Using sets which marks it runs along and where it restarts.
Returns a number: the sample variance of the expression from the first mark in the partition to the current mark.
What does it return, and what is the SQL?
Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How do I get the running sample variance per region?
Tableau
RUNNING_VAR(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
var_samp(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: month 1 is null. East month 2 is the variance of 100 and 120: 200. West's null month is skipped.
Region
Month
Result
East
1
null
East
2
200
East
3
933
East
4
800
West
1
null
West
2
200
West
3
200
West
4
400
✓ The Tableau formula and the SQL return the same 8 rows.
How do I run it across regions, within each month?
Tableau
RUNNING_VAR(SUM([Sales]))
Compute using Region, restarting for each Month
SQL (DuckDB)
SELECT Region,
Month,
var_samp(sum(Sales)) OVER (PARTITIONBY Month ORDERBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Region: the same formula now runs East then West inside each month.
Region
Month
Result
East
1
null
East
2
null
East
3
null
East
4
null
West
1
1,250
West
2
1,250
West
3
null
West
4
450
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why is the SQL variance smaller than Tableau's?
var_pop divides by n. RUNNING_VAR is the sample variance and divides by n - 1: East month 2 is 200, not 100.
Tableau
RUNNING_VAR(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
var_pop(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
var_samp(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
null
0
null
East
2
200
100
200
East
3
933
622
933
East
4
800
600
800
West
1
null
0
null
West
2
200
100
200
West
3
200
100
200
West
4
400
267
400
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
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,
Month,
VAR_SAMP(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.
What happens to the month with no sales?
It is skipped: the running result carries the value from the months before it. West month 3 has no sales, so its running value equals month 2's. SQL aggregate windows skip nulls the same way.
Which SQL function is sample and which is population?
In DuckDB, stddev and stddev_samp are sample (divide by n - 1), stddev_pop is population (divide by n); variance and var_samp are sample, var_pop population. Tableau's names follow the same split: STDEV and VAR are sample, STDEVP and VARP population.
How do I restart it every year?
Put the year in the partition: in Tableau, compute using Month only, with Year on the view; in SQL, PARTITION BY Region, Year ORDER BY Month.