SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST()) subtracts the first month of each region from every month. In SQL that is sum(Sales) - first_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The first month is always 0, and a month with no sales is null.
Updated · Checked on the xmetric engine
What is the syntax?
SUM([Sales]) -LOOKUP(SUM([Sales]), FIRST())
Returns a number: the current mark minus the first 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 much has each month moved since the region's first month?
Tableau
SUM([Sales]) -LOOKUP(SUM([Sales]), FIRST())
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
sum(Sales)
-first_value(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East is measured against 100 (month 3 is -40), West against 50 (month 4 is +40).
Region
Month
Result
East
1
0
East
2
20
East
3
−40
East
4
20
West
1
0
West
2
20
West
3
null
West
4
40
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why is West measured against East's first month?
Without PARTITION BY, first_value takes the first of all eight marks, East month 1 (100). Compute Using Month restarts for each region, so West subtracts 50.
Tableau
SUM([Sales]) -LOOKUP(SUM([Sales]), FIRST())
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
sum(Sales)
-first_value(sum(Sales)) OVER (ORDERBY Region, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
sum(Sales)
-first_value(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
0
0
0
East
2
20
20
20
East
3
−40
−40
−40
East
4
20
20
20
West
1
0
−50
0
West
2
20
−30
20
West
3
null
null
null
West
4
40
−10
40
✓ 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)
-FIRST_VALUE(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
SUM(Sales)
-FIRST_VALUE(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
What do people ask about Difference from first 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 is West month 3 null?
It has no sales, and null minus 50 is null. Use ZN(SUM([Sales])) (SQL: coalesce(sum(Sales), 0)) if a missing month means zero.
How do I show it as a percentage of the first month?
Divide by the same lookup: (SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())) / LOOKUP(SUM([Sales]), FIRST()); in SQL, divide by the first_value window.