Tableau function Aggregate

Tableau COVAR to SQL

COVAR(expression1, expression2) measures how two numbers move together across the rows of a group, dividing by n - 1, where n counts the rows where both values are present. In SQL it is covar_samp(Sales, Units). Rows with a null on either side are left out.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a real number: the sample covariance over rows where both values are non-null. Null when there are none or only one pair.

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 do sales and units move together in each region?

Tableau

COVAR([Sales], [Units])

SQL (DuckDB)

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

East pairs four lines (row 2 has no sales); West pairs three (row 7 has no units). South has one pair: null.

RegionResult
East192
Northnull
Southnull
West200

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

Where does a straight translation go wrong?

Common mistake

Why does covar_pop give a different covariance?

COVAR is the sample covariance and divides by n - 1. covar_pop divides by n, which is Tableau's COVARP.

Tableau

COVAR([Sales], [Units])

Tempting SQL

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

SQL that matches

SELECT Region, covar_samp(Sales, Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East192144192
Northnullnullnull
Southnull0null
West200133200

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

Common mistake

Why does filling nulls with 0 change the covariance?

Tableau drops a row when either value is null. coalesce(Sales, 0) keeps row 2 as a sale of 0 with 3 units, which pulls East's covariance down.

Tableau

COVAR([Sales], [Units])

Tempting SQL

SELECT
  Region,
  covar_samp(coalesce(Sales, 0), coalesce(Units, 0)) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, covar_samp(Sales, Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East192130192
Northnull0null
Southnullnullnull
West200133200

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

What does COVAR return for an all-null group and a single pair?

North has two order lines and no sales value on either. Per product, Lamp has one complete pair (row 4) and the unnamed North lines none: null for both. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupCOVAR([Sales], [Units])Region, covar_samp(Sales, Units)192nullnull200
one pairCOVAR([Sales], [Units])Product, covar_samp(Sales, Units)0142nullnull

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Which data sources support it?

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

Does the order of the two arguments matter?

No. COVAR([Sales], [Units]) equals COVAR([Units], [Sales]).

How do I get a number between -1 and 1?

Use CORR, which divides the covariance by both standard deviations.

Where is this documented?