WINDOW_COVARP(SUM([Sales]), SUM([Profit])) is the population covariance (n in the denominator) of the two aggregates across the marks in the partition. In SQL that is covar_pop(sum(Sales), sum(Profit)) OVER (PARTITION BY Region). Marks where either value is null are skipped.
two aggregates, such as SUM([Sales]) and SUM([Profit]). Marks where either is null are left out.
start, end
optional offsets from the current mark that bound the window; without them it is the whole partition. FIRST() and LAST() work here.
Returns a number: the population covariance of the two expressions over the marks in the window.
What does it return, and what is the SQL?
Each example runs on the same 9 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
How do sales and profit vary together, as a population?
Tableau
WINDOW_COVARP(SUM([Sales]), SUM([Profit]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
covar_pop(sum(Sales), sum(Profit)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East's population covariance is 275, West's 100.
Region
Month
Result
East
1
275
East
2
275
East
3
275
East
4
275
West
1
100
West
2
100
West
3
100
West
4
100
✓ The Tableau formula and the SQL return the same 8 rows.
How do I compute it across regions, within each month?
Tableau
WINDOW_COVARP(SUM([Sales]), SUM([Profit]))
Compute using Region, restarting for each Month
SQL (DuckDB)
SELECT Region,
Month,
covar_pop(sum(Sales), sum(Profit)) OVER (PARTITIONBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Region: the window is now the two regions of each month.
Region
Month
Result
East
1
188
East
2
37.5
East
3
0
East
4
75
West
1
188
West
2
37.5
West
3
0
West
4
75
✓ The Tableau formula and the SQL return the same 8 rows.
What does the bounded form return?
With start -2 and end 0, the window moves with the current mark and is clipped at the edges of each region. Each result column is one mark, by Region and Month.
Case
Tableau, then SQL (DuckDB)
East 1
East 2
East 3
East 4
West 1
West 2
West 3
West 4
bounded (-2, 0)
WINDOW_COVARP(SUM([Sales]), SUM([Profit]), -2, 0)Region, Month, covar_pop(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
0
−25
233
367
0
35
35
40
✓ On every row, each Tableau formula and its SQL return the same value.
Where does a straight translation go wrong?
Common mistake
Why is the SQL covariance larger?
covar_samp divides by n - 1. WINDOW_COVARP is the population covariance and divides by n: West is 100, not 150.
Tableau
WINDOW_COVARP(SUM([Sales]), SUM([Profit]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
covar_samp(sum(Sales), sum(Profit)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
covar_pop(sum(Sales), sum(Profit)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
275
367
275
East
2
275
367
275
East
3
275
367
275
East
4
275
367
275
West
1
100
150
100
West
2
100
150
100
West
3
100
150
100
West
4
100
150
100
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
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,
Month,
COVAR_POP(SUM(Sales), SUM(Profit)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.
Why no ORDER BY in the SQL?
Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
What do the start and end arguments mean?
They are offsets from the current mark: -1, 1 covers the previous, current and next marks, and FIRST(), 0 runs from the first mark to the current one. In SQL they become ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING. Use ROWS, not RANGE: Tableau counts marks, and RANGE counts values of the ORDER BY column.
What happens when one of the two values is null?
The mark is left out. West month 3 has a profit of 8 but no sales, so West is computed from its other three months.