Tableau function Table calculation

Tableau WINDOW_CORR to SQL

WINDOW_CORR(SUM([Sales]), SUM([Profit])) is the Pearson correlation of the two aggregates across the marks in the partition, from -1 to 1. In SQL that is corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region). Marks where either value is null are skipped, and a window with fewer than two complete marks is null in Tableau but NaN in DuckDB, so the SQL guards it with regr_count.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_CORR(expression1, expression2, [start, end])
expression1, expression2
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 from -1 to 1: the Pearson correlation of the two expressions over the marks in the window, or null with fewer than two complete marks.

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

Tableau

WINDOW_CORR(SUM([Sales]), SUM([Profit]))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  CASE
    WHEN regr_count(sum(Sales), sum(Profit)) OVER (PARTITION BY Region) > 1 THEN corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East's sales and profit move together at 0.88; West's three complete months at 0.999.

RegionMonthResult
East10.88
East20.88
East30.88
East40.88
West11
West21
West31
West41

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

How do I compute it across regions, within each month?

Tableau

WINDOW_CORR(SUM([Sales]), SUM([Profit]))

Compute using Region, restarting for each Month

SQL (DuckDB)

SELECT
  Region,
  Month,
  CASE
    WHEN regr_count(sum(Sales), sum(Profit)) OVER (PARTITION BY Month) > 1 THEN corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Region: each month has two marks, and two points always correlate at 1 or -1. Month 3 has one complete mark, so it is null.

RegionMonthResult
East11
East21
East3null
East41
West11
West21
West3null
West41

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.

CaseTableau, then SQL (DuckDB)East 1East 2East 3East 4West 1West 2West 3West 4
bounded (-2, 0)WINDOW_CORR(SUM([Sales]), SUM([Profit]), -2, 0)Region, Month, CASE WHEN regr_count(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) > 1 THEN corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) ENDnull−10.870.9null111

On every row, each Tableau formula and its SQL return the same value.

Where does a straight translation go wrong?

Common mistake

Why does the SQL change from month to month?

With no start and end, WINDOW_CORR covers the whole partition. Adding ORDER BY Month to the SQL window shrinks the frame to "first row to current row", so it becomes a running calculation.

Tableau

WINDOW_CORR(SUM([Sales]), SUM([Profit]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East10.88NaN0.88
East20.88−10.88
East30.880.870.88
East40.880.880.88
West11NaN1
West2111
West3111
West4111

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

Common mistake

Why does the SQL return NaN on the first month?

In the bounded form, month 1 has a window of one mark. DuckDB's corr returns NaN for a single point; Tableau returns null. Counting the complete pairs with regr_count and keeping only windows of two or more matches Tableau.

Tableau

WINDOW_CORR(SUM([Sales]), SUM([Profit]), -2, 0)

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  CASE
    WHEN regr_count(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) > 1 THEN corr(sum(Sales), sum(Profit)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1nullNaNnull
East2−1−1−1
East30.870.870.87
East40.90.90.9
West1nullNaNnull
West2111
West3111
West4111

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,
  CORR(SUM(Sales), SUM(Profit)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  CORR(SUM(Sales), SUM(Profit)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own WINDOW_CORR formula return?

Edit the formula and run it on the 9 sample rows. Fields: [id], [Region], [Month], [Sales], [Profit].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about WINDOW_CORR?

Why does the expression need SUM inside it?

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.

Where is this documented?