Tableau function Table calculation pattern

Tableau Percent change from previous period to SQL

(SUM([Sales]) - LOOKUP(SUM([Sales]), -1)) / ABS(LOOKUP(SUM([Sales]), -1)) is month-over-month growth as a fraction: 0.2 means up 20%. In SQL, (sum(Sales) - lag(sum(Sales)) OVER w) / abs(lag(sum(Sales)) OVER w) with w partitioned by Region and ordered by Month. The ABS keeps the sign right when the previous value is negative.

Updated · Checked on the xmetric engine

What is the syntax?

(SUM([Sales]) - LOOKUP(SUM([Sales]), -1)) / ABS(LOOKUP(SUM([Sales]), -1))

Returns a number: the change from the previous mark divided by the size of the previous mark, or null on the first mark.

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.

What is the month-over-month growth in sales?

Tableau

(SUM([Sales]) - LOOKUP(SUM([Sales]), -1)) / ABS(LOOKUP(SUM([Sales]), -1))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  (sum(Sales) - lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month))
    / abs(lag(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 month 2 is +0.2 (100 to 120), month 3 -0.5, month 4 +1. The first month of each region is null.

RegionMonthResult
East1null
East20.2
East3−0.5
East41
West1null
West20.4
West3null
West4null

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

What is the growth in profit, which can be negative?

Tableau

(SUM([Profit]) - LOOKUP(SUM([Profit]), -1)) / ABS(LOOKUP(SUM([Profit]), -1))

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  (sum(Profit) - lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month))
    / abs(lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

East's profit goes from -5 in month 3 to 30 in month 4: a change of +35 over a base of 5, so +7.

RegionMonthResult
East1null
East2−0.25
East3−1.33
East47
West1null
West21.4
West3−0.33
West41.5

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

Where does a straight translation go wrong?

Common mistake

Why does growth from a loss come out negative?

Dividing by the previous value itself flips the sign when that value is negative: East goes from -5 to 30, and 35 / -5 is -7. Dividing by abs() of it gives +7, an improvement.

Tableau

(SUM([Profit]) - LOOKUP(SUM([Profit]), -1)) / ABS(LOOKUP(SUM([Profit]), -1))

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  (sum(Profit) - lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month))
    / lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  (sum(Profit) - lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month))
    / abs(lag(sum(Profit)) OVER (PARTITION BY Region ORDER BY Month)) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1nullnullnull
East2−0.25−0.25−0.25
East3−1.33−1.33−1.33
East47−77
West1nullnullnull
West21.41.41.4
West3−0.33−0.33−0.33
West41.51.51.5

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) - LAG(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month))
    / ABS(LAG(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,
  SAFE_DIVIDE(SUM(Sales) - LAG(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month), ABS(LAG(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 Percent change from previous 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 Percent change from previous 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.

What if the previous month is 0?

Tableau returns null. DuckDB returns infinity for a division by zero, so wrap the divisor in nullif(..., 0); Snowflake raises an error (use DIV0NULL), and BigQuery has SAFE_DIVIDE.

Why is West month 4 null?

Its previous month, 3, has no sales, so there is nothing to compare with.

Where is this documented?