Tableau function Table calculation

Tableau PREVIOUS_VALUE to SQL

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,
  CASE
    WHEN count(*) OVER (PARTITION BY Region ORDER BY Month) = count(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) THEN sum(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 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.

RegionMonthResult
East1100
East2220
East3280
East4400
West150
West2120
West3null
West4null

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,
  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

The documented example: each mark multiplies the previous result. East grows to 86,400,000; West stops at the null month.

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 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 (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 sum(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
East2220220220
East3280280280
East4400400400
West1505050
West2120120120
West3null120null
West4null210null

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 SUM(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 SUM(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 PREVIOUS_VALUE 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 PREVIOUS_VALUE?

Why does one null blank every later mark?

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.

Where is this documented?