Tableau function Table calculation pattern

Tableau Difference from last period to SQL

SUM([Sales]) - LOOKUP(SUM([Sales]), LAST()) compares every month with the last month of its region. In SQL that is sum(Sales) - last_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING). The full frame is required: by default last_value stops at the current row.

Updated · Checked on the xmetric engine

What is the syntax?

SUM([Sales]) - LOOKUP(SUM([Sales]), LAST())

Returns a number: the current mark minus the last mark of its 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 far is each month from the region's latest month?

Tableau

SUM([Sales]) - LOOKUP(SUM([Sales]), LAST())

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  sum(Sales)
    - last_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East compares with 120 (month 3 is -60), West with 90 (month 1 is -40). The last month is always 0.

RegionMonthResult
East1−20
East20
East3−60
East40
West1−40
West2−20
West3null
West40

The Tableau formula and the SQL return the same 8 rows.

Where does a straight translation go wrong?

Common mistake

Why is every month 0 in the SQL?

A window with ORDER BY and no frame runs from the first row to the current row, so last_value returns the current mark and each month is compared with itself. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

Tableau

SUM([Sales]) - LOOKUP(SUM([Sales]), LAST())

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(Sales)
    - last_value(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,
  sum(Sales)
    - last_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1−200−20
East2000
East3−600−60
East4000
West1−400−40
West2−200−20
West3nullnullnull
West4000

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(Sales)
    - LAST_VALUE(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  SUM(Sales)
    - LAST_VALUE(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Difference from last period 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 Difference from last period?

Why does the expression need SUM inside it?

A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.

Why does first_value work without a frame but last_value does not?

The default frame always starts at the first row of the partition, so first_value sees it. It ends at the current row, so last_value never sees the rows after it.

Why is West month 3 null?

It has no sales, and null minus 90 is null.

Where is this documented?