Tableau function Aggregate

Tableau STDEV to SQL

STDEV(expression) returns the sample standard deviation of the non-null values, dividing by n - 1. In SQL that is stddev_samp(Sales). A group with a single value has no sample standard deviation and returns null.

Updated · Checked on the xmetric engine

What is the syntax?

STDEV(expression)
expression
a numeric field or row-level calculation.

Returns a real number: the sample standard deviation, dividing by n - 1. Null when the group has no values or only one.

What does it return, and what is the SQL?

Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What is the standard deviation of sales in each region?

Tableau

STDEV([Sales])

SQL (DuckDB)

SELECT Region, stddev_samp(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

East's four sales values give 110.87. South has one value: null, since a sample needs two.

RegionResult
East111
Northnull
Southnull
West108

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

Where does a straight translation go wrong?

Common mistake

Why does stddev_pop give a smaller standard deviation?

STDEV is the sample standard deviation: it divides by n - 1. stddev_pop divides by n, the population version (STDEVP). East comes out at 110.87 against 96.01 for the population version.

Tableau

STDEV([Sales])

Tempting SQL

SELECT Region, stddev_pop(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, stddev_samp(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East11196.01111
Northnullnullnull
Southnull0null
West10893.54108

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does STDEV return for an all-null group and a single value?

North has two order lines and no sales value on either. Per product, Lamp has one line with a sale (row 4); the North lines have no product and no sales: null for both. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupSTDEV([Sales])Region, stddev_samp(Sales)111nullnull108
one valueSTDEV([Sales])Product, stddev_samp(Sales)11.5596.18nullnull

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

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, STDDEV_SAMP(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT Region, STDDEV_SAMP(Sales) AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own STDEV formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Product], [Sales], [Units].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about STDEV?

Should I use STDEV or STDEVP?

Use the sample version when your rows are a sample of a bigger population, which is the usual case. STDEVP is the population version.

Does STDEV ignore nulls?

Yes. Empty values are left out of both the mean and the count, as in SQL.

What does STDEV return for one value?

Null: dividing by n - 1 = 0 is undefined. South has one sale and returns null.

Where is this documented?