Tableau function Table calculation

Tableau RUNNING_SUM to SQL

RUNNING_SUM(SUM([Sales])) adds each mark's sales to the total of the marks before it. In SQL that's a window over the aggregated rows: sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). Compute Using decides the window: the dimensions it runs along go in ORDER BY, and the rest of the view goes in PARTITION BY.

Updated · Checked on the xmetric engine

What is the syntax?

RUNNING_SUM(expression)
expression
an aggregate, such as SUM([Sales]). Compute Using sets which marks it runs along and where it restarts.

Returns a number: the sum of the expression from the first mark in the partition to the current mark.

What does it return, and what is the SQL?

Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

How do I get a running total per region, month by month?

Tableau

RUNNING_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

SQL (DuckDB)

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

Compute using Month: the total runs along the months and restarts for each region. East reaches 280, West 150.

RegionMonthResult
East1100
East2220
East3280
West150
West2120
West3150

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

How do I run the total across regions, within each month?

Tableau

RUNNING_SUM(SUM([Sales]))

Compute using Region, restarting for each Month

SQL (DuckDB)

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

Compute using Region: the same formula now runs East then West inside each month, so West's month 1 is 100 + 50.

RegionMonthResult
East1100
East2120
East360
West1150
West2190
West390

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

Where does a straight translation go wrong?

Common mistake

Why does the SQL total run on into the next region?

Compute Using Month restarts the sum for each region. A window with only ORDER BY keeps adding, so West starts from East's 280.

Tableau

RUNNING_SUM(SUM([Sales]))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(sum(Sales)) OVER (ORDER BY Region, 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 ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1100100100
East2220220220
East3280280280
West15033050
West2120400120
West3150430150

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 ORDER BY Month) 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 ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own RUNNING_SUM formula return?

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

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

Which functions go with it?

What do people ask about RUNNING_SUM?

Why does RUNNING_SUM need SUM inside it?

A table calculation runs on the marks in the view, so its argument must be an aggregate. RUNNING_SUM([Sales]) raises an error; see All fields must be aggregate or constant.

Why sum(sum(Sales)) in the SQL?

The inner sum builds the marks (one row per Region and Month, as GROUP BY makes them); the outer sum is the window over those marks. East has two rows in month 2, and they become one mark of 120.

How do I restart the running total every year?

Put the year in the partition: in Tableau, compute using Month only, with Year on the view; in SQL, PARTITION BY Region, Year ORDER BY Month.

What happens to nulls?

A null adds nothing; the running total carries on. West's month 3 has a null row and a 30, so its mark is 30.

Where is this documented?