Tableau function Table calculation

Tableau WINDOW_STDEV to SQL

WINDOW_STDEV(SUM([Sales])) is the sample standard deviation (n - 1 in the denominator) of the expression across the marks in the partition. In SQL that is stddev_samp(sum(Sales)) OVER (PARTITION BY Region). A window holding one non-null mark returns null.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_STDEV(expression, [start, end])
expression
an aggregate, such as SUM([Sales]).
start, end
optional offsets from the current mark that bound the window; without them it is the whole partition. FIRST() and LAST() work here.

Returns a number: the sample standard deviation of the expression over the marks in the window.

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 spread out are each region's months?

Tableau

WINDOW_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East's four marks give 28.28, West's three give 20.

RegionMonthResult
East128.28
East228.28
East328.28
East428.28
West120
West220
West320
West420

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

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

Tableau

WINDOW_STDEV(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the window is now the two regions of each month.

RegionMonthResult
East135.36
East235.36
East3null
East421.21
West135.36
West235.36
West3null
West421.21

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

What does the bounded form return?

With start -1 and end 1, the window moves with the current mark and is clipped at the edges of each region. Each result column is one mark, by Region and Month.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 4
bounded (-1, 1)WINDOW_STDEV(SUM([Sales]), -1, 1)Region, Month, stddev_samp(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)14.1430.5534.6442.4314.1414.1414.14null

On every row, each Tableau formula and its SQL return the same value.

Where does a straight translation go wrong?

Common mistake

Why is the SQL standard deviation smaller?

stddev_pop divides by n. WINDOW_STDEV is the sample version and divides by n - 1: West's three marks give 20, not 16.33.

Tableau

WINDOW_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  stddev_pop(sum(Sales)) OVER (PARTITION BY Region) 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) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East128.2824.4928.28
East228.2824.4928.28
East328.2824.4928.28
East428.2824.4928.28
West12016.3320
West22016.3320
West32016.3320
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) 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) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own WINDOW_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 WINDOW_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.

Why no ORDER BY in the SQL?

Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

What do the start and end arguments mean?

They are offsets from the current mark: -1, 1 covers the previous, current and next marks, and FIRST(), 0 runs from the first mark to the current one. In SQL they become ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING. Use ROWS, not RANGE: Tableau counts marks, and RANGE counts values of the ORDER BY column.

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.

Where is this documented?