Tableau function Aggregate

Tableau COVARP to SQL

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

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a real number: the population covariance over rows where both values are non-null. Null when there are none; 0 for a single 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

COVARP([Sales], [Units])

SQL (DuckDB)

SELECT Region, covar_pop(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: 0.

RegionResult
East144
Northnull
South0
West133

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

Where does a straight translation go wrong?

Common mistake

Why does covar_samp give a different covariance?

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

Tableau

COVARP([Sales], [Units])

Tempting SQL

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

SQL that matches

SELECT Region, covar_pop(Sales, Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East144192144
Northnullnullnull
South0null0
West133200133

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

COVARP([Sales], [Units])

Tempting SQL

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

SQL that matches

SELECT Region, covar_pop(Sales, Units) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East144104144
Northnull0null
South000
West133100133

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

What does COVARP return for an all-null group?

North has two order lines and no sales value on either. Each result column is one mark, by Region.

CaseTableau, then SQL (DuckDB)EastNorthSouthWest
all-null groupCOVARP([Sales], [Units])Region, covar_pop(Sales, Units)144null0133

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Which data sources support it?

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

Does the order of the two arguments matter?

No. COVARP([Sales], [Units]) equals COVARP([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?