Tableau function Table calculation

Tableau RUNNING_STDEV to SQL

RUNNING_STDEV(SUM([Sales])) is the sample standard deviation (n - 1 in the denominator) of the marks from the start of the partition to the current one. In SQL that is stddev_samp(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). With one mark there is no sample spread, so the first month is null.

Updated · Checked on the xmetric engine

What is the syntax?

RUNNING_STDEV(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 standard deviation 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 standard deviation per region?

Tableau

RUNNING_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  stddev_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 (one mark). East month 2 is the spread of 100 and 120: 14.14. West's null month is skipped.

RegionMonthResult
East1null
East214.14
East330.55
East428.28
West1null
West214.14
West314.14
West420

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

How do I run it across regions, within each month?

Tableau

RUNNING_STDEV(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  stddev_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
West135.36
West235.36
West3null
West421.21

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

Where does a straight translation go wrong?

Common mistake

Why is the SQL lower on every month after the first?

stddev_pop divides by n, the population formula. RUNNING_STDEV is the sample formula and divides by n - 1, so it is larger, and null on a single mark where the population version gives 0.

Tableau

RUNNING_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  stddev_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,
  stddev_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
East214.141014.14
East330.5524.9430.55
East428.2824.4928.28
West1null0null
West214.141014.14
West314.141014.14
West42016.3320

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,
  STDDEV_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,
  STDDEV_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_STDEV 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_STDEV?

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?