Tableau function Table calculation pattern

Tableau Difference from previous period to SQL

SUM([Sales]) - LOOKUP(SUM([Sales]), -1) is the change from the previous month. In SQL that is sum(Sales) - lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The first month of each region has nothing to compare with and is null, as is any month next to a month with no sales.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a number: the current mark minus the previous mark, or null 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 much did sales change from the month before?

Tableau

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

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  sum(Sales)
    - lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East month 2 is +20, month 3 -60, month 4 +60. West months 3 and 4 are null because month 3 has no sales.

RegionMonthResult
East1null
East220
East3−60
East460
West1null
West220
West3null
West4null

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

Where does a straight translation go wrong?

Common mistake

Why does West's first month show a drop of 70?

Without PARTITION BY, lag reaches back into East and subtracts its month 4 (120) from West month 1 (50). Compute Using Month restarts for each region, so West month 1 has no previous month.

Tableau

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

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(Sales)
    - lag(sum(Sales)) OVER (ORDER BY Region, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  sum(Sales)
    - lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1nullnullnull
East2202020
East3−60−60−60
East4606060
West1null−70null
West2202020
West3nullnullnull
West4nullnullnull

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

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  SUM(Sales)
    - LAG(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Difference from previous 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 previous 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.

Should the first month be 0 instead of null?

Only if you decide so: ZN(SUM([Sales]) - LOOKUP(SUM([Sales]), -1)), in SQL coalesce(..., 0). Null says there is no previous month, which is usually the honest answer.

Is this the same as Tableau's Difference From quick table calculation?

Yes: the Difference From quick calculation, relative to Previous, writes this formula with ZN around each side.

Where is this documented?