Tableau function Table calculation

Tableau RUNNING_VARP to SQL

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

RegionMonthResult
East10
East2100
East3622
East4600
West10
West2100
West3100
West4267

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 (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
East10
East20
East30
East40
West1625
West2625
West30
West4225

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 (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_pop(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
East10null0
East2100200100
East3622933622
East4600800600
West10null0
West2100200100
West3100200100
West4267400267

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 (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_POP(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_VARP 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_VARP?

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?