RUNNING_VARP(SUM([Sales])) is the population variance (n in the denominator) of the marks from the start of the partition to the current one. In SQL that is var_pop(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The first month is 0.
Updated · Checked on the xmetric engine
What is the syntax?
RUNNING_VARP(expression)
expression
an aggregate, such as SUM([Sales]). Compute Using sets which marks it runs along and where it restarts.
Returns a number: the population 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 population variance per region?
Tableau
RUNNING_VARP(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
var_pop(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: month 1 is 0. East month 2 is the variance of 100 and 120: 100. West's null month is skipped.
Region
Month
Result
East
1
0
East
2
100
East
3
622
East
4
600
West
1
0
West
2
100
West
3
100
West
4
267
✓ The Tableau formula and the SQL return the same 8 rows.
How do I run it across regions, within each month?
Tableau
RUNNING_VARP(SUM([Sales]))
Compute using Region, restarting for each Month
SQL (DuckDB)
SELECT Region,
Month,
var_pop(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
0
East
2
0
East
3
0
East
4
0
West
1
625
West
2
625
West
3
0
West
4
225
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does variance() double the second month?
Plain variance is the sample variance in DuckDB, Snowflake and BigQuery, dividing by n - 1. RUNNING_VARP is the population variance and needs var_pop.
Tableau
RUNNING_VARP(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
variance(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
var_pop(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
0
null
0
East
2
100
200
100
East
3
622
933
622
East
4
600
800
600
West
1
0
null
0
West
2
100
200
100
West
3
100
200
100
West
4
267
400
267
✓ 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_POP(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.