Tableau function Table calculation

Tableau FIRST to SQL

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

Compute using Month: the first month of each region is 0, the fourth is -3. The count restarts for West.

RegionMonthResult
East10
East2−1
East3−2
East4−3
West10
West2−1
West3−2
West4−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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

FIRST() as the offset points every mark at month 1: East reads 100, West 50. SQL has first_value() for exactly that.

RegionMonthResult
East1100
East2100
East3100
East4100
West150
West250
West350
West450

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

SQL that matches

SELECT
  Region,
  Month,
  1
    - 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
East1000
East2−11−1
East3−22−2
East4−33−3
West1000
West2−11−1
West3−22−2
West4−33−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 (ORDER BY Region, Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  1
    - 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
East1000
East2−1−1−1
East3−2−2−2
East4−3−3−3
West10−40
West2−1−5−1
West3−2−6−2
West4−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 (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  1
    - 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 FIRST 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 FIRST?

Why is FIRST() negative?

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.

Where is this documented?