SUM([Sales]) * PREVIOUS_VALUE(1) multiplies each month into the result of the month before, starting from 1. In DuckDB that is product(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month), with a guard: in Tableau a null month makes that month and every later one null, while product() skips nulls.
Updated · Checked on the xmetric engine
What is the syntax?
SUM([Sales]) *PREVIOUS_VALUE(1)
Returns a number: the product of the expression from the first mark of the partition to the current mark, null from the first null on.
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 multiply the months together, region by region?
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
Compute using Month: East reaches 100 x 120 x 60 x 120 = 86,400,000. West stops at 3,500: month 3 is null and so is everything after.
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 carry on past the month with no sales?
product() skips nulls, so West month 4 is 50 x 70 x 90. In the recurrence, month 3 is null x 3,500 = null, and month 4 builds on that null. The CASE blanks every month from the first null on.
Tableau
SUM([Sales]) *PREVIOUS_VALUE(1)
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
product(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) THENproduct(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
12,000
12,000
12,000
East
3
720,000
720,000
720,000
East
4
86,400,000
86,400,000
86,400,000
West
1
50
50
50
West
2
3,500
3,500
3,500
West
3
null
3,500
null
West
4
null
315,000
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) THENEXP(SUM(LN(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) THENEXP(SUM(LN(SUM(Sales))) OVER (PARTITIONBY Region ORDERBY Month))
ENDAS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
How do I write a running product in Snowflake or BigQuery?
Neither has a PRODUCT aggregate. Use EXP(SUM(LN(x)) OVER (...)), which works only while every value is positive; zeros and negatives need their own CASE handling.
Why start PREVIOUS_VALUE at 1?
It is the value used before the first mark, and 1 leaves the first month unchanged. Starting at 0 would make every product 0.
Is it used for compound growth?
Yes: with growth factors such as 1.05 per month, the running product is the cumulative growth.