FIRST() returns how many marks the current mark sits after the first one, as a negative number: 0 on the first mark, -1 on the next, and so on. In SQL that is 1 - row_number() OVER (PARTITION BY Region ORDER BY Month). Its main use is as an offset: LOOKUP(SUM([Sales]), FIRST()) reads the first mark, which SQL writes as first_value().
Updated · Checked on the xmetric engine
What is the syntax?
FIRST()
Returns an integer: the offset from the current mark back to the first mark in the partition (0 or negative).
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 far is each month from the first month of its region?
Tableau
FIRST()
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
1-row_number() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: the first month of each region is 0, the fourth is -3. The count restarts for West.
Region
Month
Result
East
1
0
East
2
−1
East
3
−2
East
4
−3
West
1
0
West
2
−1
West
3
−2
West
4
−3
✓ The Tableau formula and the SQL return the same 8 rows.
How do I read the first month's sales on every row?
Tableau
LOOKUP(SUM([Sales]), FIRST())
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
first_value(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
FIRST() as the offset points every mark at month 1: East reads 100, West 50. SQL has first_value() for exactly that.
Region
Month
Result
East
1
100
East
2
100
East
3
100
East
4
100
West
1
50
West
2
50
West
3
50
West
4
50
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does the SQL count up from the first month instead of down?
row_number() - 1 gives the distance from the first mark as a positive number. FIRST() is an offset, so it is the same distance with a minus sign: month 4 is -3, not 3.
Tableau
FIRST()
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
row_number() OVER (PARTITIONBY Region ORDERBY Month)
-1AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
1-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
0
0
0
East
2
−1
1
−1
East
3
−2
2
−2
East
4
−3
3
−3
West
1
0
0
0
West
2
−1
1
−1
West
3
−2
2
−2
West
4
−3
3
−3
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why do West's offsets keep counting from East?
Compute Using Month restarts the count for each region. A window without PARTITION BY numbers all eight marks in one run, so West month 1 comes out as -4.
Tableau
FIRST()
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
1-row_number() OVER (ORDERBY Region, Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
1-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
0
0
0
East
2
−1
−1
−1
East
3
−2
−2
−2
East
4
−3
−3
−3
West
1
0
−4
0
West
2
−1
−5
−1
West
3
−2
−6
−2
West
4
−3
−7
−3
✓ 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,
1-ROW_NUMBER() OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
It is an offset you can pass to LOOKUP or a WINDOW_ function, and offsets count backward with negative numbers. Month 3 is two marks after month 1, so FIRST() there is -2 and LOOKUP(SUM([Sales]), -2) reaches month 1.
How do I keep only the first month of each region?
Filter on FIRST() = 0 in Tableau. In SQL, filter the window in an outer query or with QUALIFY row_number() OVER (PARTITION BY Region ORDER BY Month) = 1 (DuckDB and Snowflake; BigQuery also has QUALIFY).
Does a null measure change FIRST()?
No. FIRST() counts marks, not values. West month 3 has no sales and still gets -2.