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
GROUPBY Region
ORDERBY Region
East pairs four lines (row 2 has no sales); West pairs three (row 7 has no units). South has one pair: 0.
Region
Result
East
144
North
null
South
0
West
133
✓ 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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, covar_pop(Sales, Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
144
192
144
North
null
null
null
South
0
null
0
West
133
200
133
✓ 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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, covar_pop(Sales, Units) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
144
104
144
North
null
0
null
South
0
0
0
West
133
100
133
✓ 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.