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 (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: the total runs along the months and restarts for each region. East reaches 280, West 150.
Region
Month
Result
East
1
100
East
2
220
East
3
280
West
1
50
West
2
120
West
3
150
✓ 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 (PARTITIONBY Month ORDERBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Region: the same formula now runs East then West inside each month, so West's month 1 is 100 + 50.
Region
Month
Result
East
1
100
East
2
120
East
3
60
West
1
150
West
2
190
West
3
90
✓ 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 (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
West
1
50
330
50
West
2
120
400
120
West
3
150
430
150
✓ 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. 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.