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 (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East reaches 400, West 210. West's null month adds nothing and carries 120.
Region
Month
Result
East
1
100
East
2
220
East
3
280
East
4
400
West
1
50
West
2
120
West
3
120
West
4
210
✓ 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 (ORDERBY Region, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
100
100
100
East
2
220
220
220
East
3
280
280
280
East
4
400
400
400
West
1
50
450
50
West
2
120
520
120
West
3
120
520
120
West
4
210
610
210
✓ 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 (PARTITIONBY Region ORDERBY Month) 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 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.