Tableau function Table calculation pattern

Tableau Next period revenue to SQL

LOOKUP(SUM([Sales]), 1) reads the next mark's sales. In SQL that is lead(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The last month of each region has no next mark and returns null.

Updated · Checked on the xmetric engine

What is the syntax?

LOOKUP(SUM([Sales]), 1)

Returns a number: the expression on the next mark, or null on the last 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 do I show next month's sales beside this month's?

Tableau

LOOKUP(SUM([Sales]), 1)

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  lead(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East month 1 shows 120, month 4 null. West month 2 shows the null of month 3.

RegionMonthResult
East1120
East260
East3120
East4null
West170
West2null
West390
West4null

The Tableau formula and the SQL return the same 8 rows.

Where does a straight translation go wrong?

Common mistake

Why does East's last month show West's sales?

Without PARTITION BY, lead runs on into the next region, so East month 4 reads West month 1 (50). Compute Using Month stops at the edge of each region.

Tableau

LOOKUP(SUM([Sales]), 1)

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  lead(sum(Sales)) OVER (ORDER BY Region, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  lead(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1120120120
East2606060
East3120120120
East4null50null
West1707070
West2nullnullnull
West3909090
West4nullnullnull

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,
  LEAD(SUM(Sales)) 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,
  LEAD(SUM(Sales)) 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 Next period revenue 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 Next period revenue?

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.

Is it lead or lag?

A positive LOOKUP offset looks forward, which is lead; a negative one looks back, which is lag.

How do I read two months ahead?

LOOKUP(SUM([Sales]), 2), in SQL lead(sum(Sales), 2) with the same window.

Where is this documented?