Tableau function Table calculation pattern

Tableau Running revenue product to SQL

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,
  CASE
    WHEN count(*) OVER (PARTITION BY Region ORDER BY Month) = count(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) THEN product(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY 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.

RegionMonthResult
East1100
East212,000
East3720,000
East486,400,000
West150
West23,500
West3null
West4null

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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  CASE
    WHEN count(*) OVER (PARTITION BY Region ORDER BY Month) = count(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) THEN product(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month)
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1100100100
East212,00012,00012,000
East3720,000720,000720,000
East486,400,00086,400,00086,400,000
West1505050
West23,5003,5003,500
West3null3,500null
West4null315,000null

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,
  CASE
    WHEN COUNT(*) OVER (PARTITION BY Region ORDER BY Month) = COUNT(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month) THEN EXP(SUM(LN(SUM(Sales))) OVER (PARTITION BY Region ORDER BY Month))
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  CASE
    WHEN COUNT(*) OVER (PARTITION BY Region ORDER BY Month) = COUNT(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month) THEN EXP(SUM(LN(SUM(Sales))) OVER (PARTITION BY Region ORDER BY Month))
    END AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Running revenue product 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 Running revenue product?

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.

Where is this documented?