Tableau function Table calculation

Tableau LAST to SQL

LAST() returns how many marks are left between the current mark and the last one: 3 on the first of four marks, 0 on the last. In SQL that is count(*) OVER (PARTITION BY Region) - row_number() OVER (PARTITION BY Region ORDER BY Month). As an offset, LOOKUP(SUM([Sales]), LAST()) reads the last mark, which SQL writes as last_value() over the whole partition.

Updated · Checked on the xmetric engine

What is the syntax?

LAST()

Returns an integer: the number of marks from the current mark to the last mark in the partition (0 or positive).

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 many months are left until the last month of each region?

Tableau

LAST()

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  count(*) OVER (PARTITION BY Region)
    - row_number() OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: month 1 has three marks after it, month 4 none. West counts the same way even with a null month.

RegionMonthResult
East13
East22
East31
East40
West13
West22
West31
West40

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

How do I read the last month's sales on every row?

Tableau

LOOKUP(SUM([Sales]), LAST())

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  last_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Every mark reads its region's month 4: East 120, West 90.

RegionMonthResult
East1120
East2120
East3120
East4120
West190
West290
West390
West490

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

Where does a straight translation go wrong?

Common mistake

Why is the SQL one higher than LAST() on every row?

Numbering the marks backward with row_number() ... DESC gives 1 on the last mark. LAST() is an offset, so the last mark is 0.

Tableau

LAST()

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  row_number() OVER (PARTITION BY Region ORDER BY Month DESC) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  count(*) OVER (PARTITION BY Region)
    - row_number() OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1343
East2232
East3121
East4010
West1343
West2232
West3121
West4010

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,
  COUNT(*) OVER (PARTITION BY Region)
    - ROW_NUMBER() 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,
  COUNT(*) OVER (PARTITION BY Region)
    - ROW_NUMBER() 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 LAST 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 LAST?

How do I keep only the latest month of each region?

Filter on LAST() = 0. Because it is a table calculation, the filter runs after the marks are built, so the other months still count toward the window. In SQL, use QUALIFY row_number() OVER (PARTITION BY Region ORDER BY Month DESC) = 1.

Does a null measure change LAST()?

No. LAST() counts marks. West month 3 has no sales, and West month 2 still has two marks after it.

Why last_value() with a full frame?

With ORDER BY, a SQL window ends at the current row by default, so last_value() returns the current mark. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to reach the real last mark.

Where is this documented?