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.
SELECT Region,
Month,
(sum(Sales) -avg(sum(Sales)) OVER (PARTITIONBY Region))
/stddev_samp(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
0
East
2
0.71
East
3
−1.41
East
4
0.71
West
1
−1
West
2
0
West
3
null
West
4
1
✓ 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.
SELECT Region,
Month,
(sum(Sales) -avg(sum(Sales)) OVER (PARTITIONBY Region))
/stddev_pop(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
(sum(Sales) -avg(sum(Sales)) OVER (PARTITIONBY Region))
/stddev_samp(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
0
0
0
East
2
0.71
0.82
0.71
East
3
−1.41
−1.63
−1.41
East
4
0.71
0.82
0.71
West
1
−1
−1.22
−1
West
2
0
0
0
West
3
null
null
null
West
4
1
1.22
1
✓ 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 (PARTITIONBY Region))
/STDDEV_SAMP(SUM(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
(SUM(Sales) -AVG(SUM(Sales)) OVER (PARTITIONBY Region))
/STDDEV_SAMP(SUM(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.