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