Tableau function Table calculation

Tableau LOOKUP to SQL

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

RegionMonthResult
East1null
East2100
East3120
East460
West1null
West250
West370
West4null

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

RegionMonthResult
East160
East2120
East3null
East4null
West1null
West290
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 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 (ORDER BY Region, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  lag(sum(Sales), 1) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1nullnullnull
East2100100100
East3120120120
East4606060
West1null120null
West2505050
West3707070
West4nullnullnull

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

SQL that matches

SELECT
  Region,
  Month,
  lag(sum(Sales), 1) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1nullnullnull
East2100100100
East3120120120
East4606060
West1nullnullnull
West2505050
West3707070
West4null70null

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

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  LAG(SUM(Sales), 1) 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 LOOKUP 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 LOOKUP?

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.

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.

Where is this documented?