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 (PARTITIONBY Region)
-row_number() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
3
East
2
2
East
3
1
East
4
0
West
1
3
West
2
2
West
3
1
West
4
0
✓ 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 (PARTITIONBY Region ORDERBY Month ROWSBETWEENUNBOUNDEDPRECEDINGANDUNBOUNDEDFOLLOWING) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Every mark reads its region's month 4: East 120, West 90.
Region
Month
Result
East
1
120
East
2
120
East
3
120
East
4
120
West
1
90
West
2
90
West
3
90
West
4
90
✓ 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 (PARTITIONBY Region ORDERBY Month DESC) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
count(*) OVER (PARTITIONBY Region)
-row_number() 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
3
4
3
East
2
2
3
2
East
3
1
2
1
East
4
0
1
0
West
1
3
4
3
West
2
2
3
2
West
3
1
2
1
West
4
0
1
0
✓ 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 (PARTITIONBY Region)
-ROW_NUMBER() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
COUNT(*) OVER (PARTITIONBY Region)
-ROW_NUMBER() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.