Tableau function Table calculation pattern

Tableau Regional revenue z-score to SQL

This formula says how many standard deviations each month sits above or below its region's average. In SQL: (sum(Sales) - avg(sum(Sales)) OVER (PARTITION BY Region)) / stddev_samp(sum(Sales)) OVER (PARTITION BY Region). WINDOW_STDEV is the sample standard deviation, so the SQL must be too.

Updated · Checked on the xmetric engine

What is the syntax?

(SUM([Sales]) - WINDOW_AVG(SUM([Sales]))) / WINDOW_STDEV(SUM([Sales]))

Returns a number: the mark's distance from its partition average in sample standard deviations.

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 unusual is each month for its region?

Tableau

(SUM([Sales]) - WINDOW_AVG(SUM([Sales]))) / WINDOW_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: East averages 100 with a standard deviation of 28.28, so month 3 (60) is -1.41. West's null month stays null.

RegionMonthResult
East10
East20.71
East3−1.41
East40.71
West1−1
West20
West3null
West41

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

Where does a straight translation go wrong?

Common mistake

Why are the SQL z-scores larger?

stddev_pop divides by n and is smaller than the sample standard deviation, so every score grows: West month 4 is 1.22 instead of 1. WINDOW_STDEV is stddev_samp.

Tableau

(SUM([Sales]) - WINDOW_AVG(SUM([Sales]))) / WINDOW_STDEV(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  (sum(Sales) - avg(sum(Sales)) OVER (PARTITION BY Region))
    / 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,
  (sum(Sales) - avg(sum(Sales)) OVER (PARTITION BY Region))
    / stddev_samp(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1000
East20.710.820.71
East3−1.41−1.63−1.41
East40.710.820.71
West1−1−1.22−1
West2000
West3nullnullnull
West411.221

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,
  (SUM(Sales) - AVG(SUM(Sales)) OVER (PARTITION BY Region))
    / 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,
  (SUM(Sales) - AVG(SUM(Sales)) OVER (PARTITION BY Region))
    / 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 Regional revenue z-score 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 Regional revenue z-score?

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.

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?