Tableau function Table calculation pattern

Tableau Difference from first period to SQL

SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST()) subtracts the first month of each region from every month. In SQL that is sum(Sales) - first_value(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month). The first month is always 0, and a month with no sales is null.

Updated · Checked on the xmetric engine

What is the syntax?

SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())

Returns a number: the current mark minus the first mark of its 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 much has each month moved since the region's first month?

Tableau

SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  sum(Sales)
    - first_value(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 is measured against 100 (month 3 is -40), West against 50 (month 4 is +40).

RegionMonthResult
East10
East220
East3−40
East420
West10
West220
West3null
West440

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

Where does a straight translation go wrong?

Common mistake

Why is West measured against East's first month?

Without PARTITION BY, first_value takes the first of all eight marks, East month 1 (100). Compute Using Month restarts for each region, so West subtracts 50.

Tableau

SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  sum(Sales)
    - first_value(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,
  sum(Sales)
    - first_value(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
East1000
East2202020
East3−40−40−40
East4202020
West10−500
West220−3020
West3nullnullnull
West440−1040

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,
  SUM(Sales)
    - FIRST_VALUE(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,
  SUM(Sales)
    - FIRST_VALUE(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 Difference from first period 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 Difference from first period?

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.

Why is West month 3 null?

It has no sales, and null minus 50 is null. Use ZN(SUM([Sales])) (SQL: coalesce(sum(Sales), 0)) if a missing month means zero.

How do I show it as a percentage of the first month?

Divide by the same lookup: (SUM([Sales]) - LOOKUP(SUM([Sales]), FIRST())) / LOOKUP(SUM([Sales]), FIRST()); in SQL, divide by the first_value window.

Where is this documented?