Tableau function Table calculation

Tableau WINDOW_SUM to SQL

WINDOW_SUM(SUM([Sales])) adds the expression over every mark in the partition and shows the result on each mark. In SQL that is sum(sum(Sales)) OVER (PARTITION BY Region). With start and end offsets, such as -1, 1, it becomes a moving sum: ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_SUM(expression, [start, end])
expression
an aggregate, such as SUM([Sales]).
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 sum of the expression 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 I sum all the months of each region on every row?

Tableau

WINDOW_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: every East mark shows 400, every West mark 210. The null month adds nothing.

RegionMonthResult
East1400
East2400
East3400
East4400
West1210
West2210
West3210
West4210

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

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

Tableau

WINDOW_SUM(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the window is now the two regions of each month.

RegionMonthResult
East1150
East2190
East360
East4210
West1150
West2190
West360
West4210

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

What does the bounded form return?

With start -1 and end 1, 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 (-1, 1)WINDOW_SUM(SUM([Sales]), -1, 1)Region, Month, sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)22028030018012012016090

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_SUM 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_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(sum(Sales)) 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,
  sum(sum(Sales)) OVER (PARTITION BY Region) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1400100400
East2400220400
East3400280400
East4400400400
West121050210
West2210120210
West3210120210
West4210210210

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

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.

Where is this documented?