LOOKUP(expression, offset) returns the expression from the mark offset steps away: -1 is the previous mark, 1 the next, 0 (or no offset) the current one. In SQL, a negative offset is lag(sum(Sales), n) and a positive one is lead(sum(Sales), n), partitioned like the Compute Using. Past the edge of the partition it returns null.
Updated · Checked on the xmetric engine
What is the syntax?
LOOKUP(expression, [offset])
expression
an aggregate, such as SUM([Sales]).
offset
an integer: negative looks back, positive looks forward, 0 is the current mark. Defaults to 0. FIRST() and LAST() work as offsets.
Returns the value of the expression at the mark offset steps from the current one, or null past the edge 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 do I get the previous month's sales?
Tableau
LOOKUP(SUM([Sales]), -1)
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
lag(sum(Sales), 1) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: month 1 of each region has nothing before it and gets null. West month 4 reads the null of month 3.
Region
Month
Result
East
1
null
East
2
100
East
3
120
East
4
60
West
1
null
West
2
50
West
3
70
West
4
null
✓ The Tableau formula and the SQL return the same 8 rows.
How do I read the value two months ahead?
Tableau
LOOKUP(SUM([Sales]), 2)
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
lead(sum(Sales), 2) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
A positive offset looks forward: East month 1 reads month 3 (60). The last two months of each region run off the edge.
Region
Month
Result
East
1
60
East
2
120
East
3
null
East
4
null
West
1
null
West
2
90
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 East's sales?
Compute Using Month stops at the edge of each region, so West month 1 has no previous mark. A lag() without PARTITION BY reaches back into East and returns its month 4 (120).
Tableau
LOOKUP(SUM([Sales]), -1)
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
lag(sum(Sales), 1) OVER (ORDERBY Region, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
lag(sum(Sales), 1) 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
100
100
100
East
3
120
120
120
East
4
60
60
60
West
1
null
120
null
West
2
50
50
50
West
3
70
70
70
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.
Common mistake
Why does the SQL skip over the month with no sales?
IGNORE NULLS looks back to the last month that has a value, so West month 4 reads 70. LOOKUP steps one mark back, and that mark is null.
Tableau
LOOKUP(SUM([Sales]), -1)
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
lag(sum(Sales), 1 IGNORE NULLS) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
lag(sum(Sales), 1) 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
100
100
100
East
3
120
120
120
East
4
60
60
60
West
1
null
null
null
West
2
50
50
50
West
3
70
70
70
West
4
null
70
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,
LAG(SUM(Sales), 1) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.
What does LOOKUP return past the first or last mark?
Null. East month 1 has no previous month, so LOOKUP(SUM([Sales]), -1) is null there; lag() does the same when the window is partitioned. Wrap it in ZN() (SQL: coalesce(..., 0)) only if a missing month really means zero.
Can the offset be a calculation?
Yes. LOOKUP(SUM([Sales]), FIRST()) reads the first mark from every row, and LOOKUP(SUM([Sales]), LAST()) the last. In SQL those are first_value() and last_value() over the whole partition.
Does LOOKUP skip null marks?
No. It steps over marks, not values. West month 4 looks back to month 3, which has no sales, and gets null.