PREVIOUS_VALUE(initial) returns the value this same calculation had on the previous mark, or initial on the first mark of the partition. It turns a formula into a recurrence: PREVIOUS_VALUE(0) + SUM([Sales]) is a running total, SUM([Sales]) * PREVIOUS_VALUE(1) a running product. SQL has no self-reference, so each recurrence becomes its window aggregate, plus a guard for nulls, which break the chain in Tableau.
Updated · Checked on the xmetric engine
What is the syntax?
PREVIOUS_VALUE(expression)
expression
the starting value for the first mark, such as 0 or 1, of the same type as the result.
Returns the value of the calculation on the previous mark, or the expression on the first mark of the partition.
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 with PREVIOUS_VALUE?
Tableau
PREVIOUS_VALUE(0) +SUM([Sales])
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENcount(*) OVER (PARTITIONBY Region ORDERBY Month) =count(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) THENsum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East adds up to 400. West reaches 120, then the null month breaks the chain, so months 3 and 4 are null; the CASE reproduces that.
Region
Month
Result
East
1
100
East
2
220
East
3
280
East
4
400
West
1
50
West
2
120
West
3
null
West
4
null
✓ The Tableau formula and the SQL return the same 8 rows.
How do I build a running product?
Tableau
SUM([Sales]) *PREVIOUS_VALUE(1)
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
CASEWHENcount(*) OVER (PARTITIONBY Region ORDERBY Month) =count(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) THENproduct(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
The documented example: each mark multiplies the previous result. East grows to 86,400,000; West stops at the null month.
Region
Month
Result
East
1
100
East
2
12,000
East
3
720,000
East
4
86,400,000
West
1
50
West
2
3,500
West
3
null
West
4
null
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does the SQL keep adding after the month with no sales?
A window sum skips nulls, so West month 4 is 210. In the PREVIOUS_VALUE recurrence, month 3 is 120 + null = null, and every later mark builds on that null.
Tableau
PREVIOUS_VALUE(0) +SUM([Sales])
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
CASEWHENcount(*) OVER (PARTITIONBY Region ORDERBY Month) =count(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) THENsum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
ENDAS 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
50
50
West
2
120
120
120
West
3
null
120
null
West
4
null
210
null
✓ 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,
CASEWHENCOUNT(*) OVER (PARTITIONBY Region ORDERBY Month) =COUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month) THENSUM(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
CASEWHENCOUNT(*) OVER (PARTITIONBY Region ORDERBY Month) =COUNT(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month) THENSUM(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Each mark builds on the previous result. West month 3 has no sales, so 120 + null is null, and month 4 then adds 90 to null. RUNNING_SUM skips nulls instead; wrap the measure in ZN() if you want that here.
What is the argument for?
It is the starting value, used on the first mark of each partition. It must have the type of the result: 0 for a sum, 1 for a product.
Can SQL do a true recurrence?
Only with a recursive CTE, which is slow and awkward. Sums, products, maxima and minima all have window forms, so translate the recurrence into the aggregate it computes.