Tableau function Table calculation

Tableau RUNNING_VAR to SQL

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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY 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.

RegionMonthResult
East1null
East2200
East3933
East4800
West1null
West2200
West3200
West4400

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 (PARTITION BY Month ORDER BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Region: the same formula now runs East then West inside each month.

RegionMonthResult
East1null
East2null
East3null
East4null
West11,250
West21,250
West3null
West4450

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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  var_samp(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1null0null
East2200100200
East3933622933
East4800600800
West1null0null
West2200100200
West3200100200
West4400267400

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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  VAR_SAMP(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RUNNING_VAR formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

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

Which functions go with it?

What do people ask about RUNNING_VAR?

Why does the expression need SUM inside it?

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.

Where is this documented?