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.
SELECT Region,
Month,
(sum(Sales) -lag(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month))
/abs(lag(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
null
East
2
0.2
East
3
−0.5
East
4
1
West
1
null
West
2
0.4
West
3
null
West
4
null
✓ The Tableau formula and the SQL return the same 8 rows.
What is the growth in profit, which can be negative?
SELECT Region,
Month,
(sum(Profit) -lag(sum(Profit)) OVER (PARTITIONBY Region ORDERBY Month))
/abs(lag(sum(Profit)) OVER (PARTITIONBY Region ORDERBY Month)) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
null
East
2
−0.25
East
3
−1.33
East
4
7
West
1
null
West
2
1.4
West
3
−0.33
West
4
1.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.
SELECT Region,
Month,
(sum(Profit) -lag(sum(Profit)) OVER (PARTITIONBY Region ORDERBY Month))
/lag(sum(Profit)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
(sum(Profit) -lag(sum(Profit)) OVER (PARTITIONBY Region ORDERBY Month))
/abs(lag(sum(Profit)) 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
null
null
null
East
2
−0.25
−0.25
−0.25
East
3
−1.33
−1.33
−1.33
East
4
7
−7
7
West
1
null
null
null
West
2
1.4
1.4
1.4
West
3
−0.33
−0.33
−0.33
West
4
1.5
1.5
1.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 (PARTITIONBY Region ORDERBY Month))
/ABS(LAG(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month)) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
SAFE_DIVIDE(SUM(Sales) -LAG(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month), ABS(LAG(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month))) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.