Tableau function Table calculation pattern

Tableau Regional running total to SQL

RUNNING_SUM(SUM([Sales])), computed using Month, adds up each region's months in order and restarts for the next region. In SQL that is sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month): Region in PARTITION BY, Month in ORDER BY.

Updated · Checked on the xmetric engine

What is the syntax?

RUNNING_SUM(SUM([Sales]))

Returns a number: the sum of the expression from the region's first month to the current month.

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 build a running total that restarts for each region?

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: East reaches 400, West 210. West's null month adds nothing and carries 120.

RegionMonthResult
East1100
East2220
East3280
East4400
West150
West2120
West3120
West4210

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

Where does a straight translation go wrong?

Common mistake

Why does West start from East's total?

A window with only ORDER BY runs through every mark, so West month 1 is 400 + 50. Compute Using Month restarts for each region, which is PARTITION BY Region.

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
East4400400400
West15045050
West2120520120
West3120520120
West4210610210

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 Regional running total 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 Regional running total?

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 GROUP BY Region, Month in the SQL?

Tableau computes the table calculation on marks, one per Region and Month in this view. The GROUP BY builds those marks; East has two rows in month 2 (80 and 40), and they become one mark of 120.

How do I restart it 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.

Where is this documented?