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 (PARTITIONBY Region ORDERBY Month ROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
−20
East
2
0
East
3
−60
East
4
0
West
1
−40
West
2
−20
West
3
null
West
4
0
✓ 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 (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
sum(Sales)
-last_value(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month ROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
−20
0
−20
East
2
0
0
0
East
3
−60
0
−60
East
4
0
0
0
West
1
−40
0
−40
West
2
−20
0
−20
West
3
null
null
null
West
4
0
0
0
✓ 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 (PARTITIONBY Region ORDERBY Month ROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
SUM(Sales)
-LAST_VALUE(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month ROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.