Tableau function Aggregate

Tableau CORR to SQL

CORR(expression1, expression2) returns the Pearson correlation of two numbers over the rows where both are present: 1 means they rise together, -1 that one falls as the other rises. In SQL it is corr(Sales, Units). Rows with a null on either side are dropped, not counted as 0.

Updated · Checked on the xmetric engine

What is the syntax?

CORR(expression1, expression2)
expression1, expression2
two numeric fields or row-level calculations, paired row by row.

Returns a real number from -1 to 1, over rows where both values are non-null. Null when there are none.

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.

How closely do sales follow units?

Tableau

CORR([Sales], [Units])

SQL (DuckDB)

SELECT corr(Sales, Units) AS result
FROM input

Over the eight lines with both values, 0.985: bigger orders have more units, almost in a straight line.

Result
0.99

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

Where does a straight translation go wrong?

Common mistake

Why does filling nulls with 0 weaken the correlation?

Tableau leaves out rows where either value is null. coalesce(..., 0) keeps them as sales of 0 with 1 to 3 units, which drops the correlation to 0.63.

Tableau

CORR([Sales], [Units])

Tempting SQL

SELECT corr(coalesce(Sales, 0), coalesce(Units, 0)) AS result
FROM input

SQL that matches

SELECT corr(Sales, Units) AS result
FROM input
TableauTempting SQLSQL that matches
0.990.630.99

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

What does CORR return when one side is all null?

Keeping only North's sales leaves no value on that side, since North has none. CORR returns null. Columns 1 to 1 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1
all-null inputCORR(IIF([Region] = 'North', [Sales], NULL), [Units])corr(CASE WHEN Region = 'North' THEN Sales END, Units)null

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 CORR(Sales, Units) AS result
FROM input

Snowflake documentation

BigQuery

SELECT CORR(Sales, Units) AS result
FROM input

BigQuery documentation

What does your own CORR 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 CORR?

Which data sources support CORR?

Tableau's help lists Tableau extracts, Cloudera Hive and EXASolution. On other sources, extract the data or use WINDOW_CORR.

Why is my correlation empty for a small group?

It needs at least two complete pairs whose values vary. A group with one pair, or with a constant field, has no correlation.

Is CORR the same as R-squared on a trend line?

For a straight-line trend, R-squared is CORR squared: 0.985 squared is about 0.97.

Where is this documented?