Tableau function Table calculation pattern

Tableau Centered three-period average to SQL

WINDOW_AVG(SUM([Sales]), -1, 1) averages the previous, current and next marks. In SQL that is avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING). At the edges of each region the window is clipped to two marks, and a null mark is skipped, so the divisor is the number of values actually in the window.

Updated · Checked on the xmetric engine

What is the syntax?

WINDOW_AVG(SUM([Sales]), -1, 1)

Returns a number: the average of the expression over the previous, current and next marks.

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 do I smooth each month with its neighbours?

Tableau

WINDOW_AVG(SUM([Sales]), -1, 1)

Compute using Month, restarting for each Region

SQL (DuckDB)

SELECT
  Region,
  Month,
  avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Compute using Month: East month 1 averages months 1 and 2 (110), month 2 averages all three (93.33). West month 3 averages 70 and 90.

RegionMonthResult
East1110
East293.33
East3100
East490
West160
West260
West380
West490

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

Where does a straight translation go wrong?

Common mistake

Why are the first and last months blank in the SQL?

Adding the neighbours with lag and lead and dividing by 3 needs all three values; at the edges and around the null month one is missing, so the sum is null. WINDOW_AVG averages whatever marks the window holds.

Tableau

WINDOW_AVG(SUM([Sales]), -1, 1)

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  (lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) + sum(Sales) + lead(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month))
    / 3 AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

SQL that matches

SELECT
  Region,
  Month,
  avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1110null110
East293.3393.3393.33
East3100100100
East490null90
West160null60
West260null60
West380null80
West490null90

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,
  AVG(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

Snowflake documentation

BigQuery

SELECT
  Region,
  Month,
  AVG(SUM(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Centered three-period average 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 Centered three-period average?

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 do the start and end arguments mean?

They are offsets from the current mark: -1, 1 covers the previous, current and next marks, and FIRST(), 0 runs from the first mark to the current one. In SQL they become ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING. Use ROWS, not RANGE: Tableau counts marks, and RANGE counts values of the ORDER BY column.

How do I hide the edge months instead of averaging two?

Return null unless the window is full: IF WINDOW_COUNT(SUM([Sales]), -1, 1) = 3 THEN WINDOW_AVG(SUM([Sales]), -1, 1) END; in SQL, CASE WHEN count(sum(Sales)) OVER (...) = 3 THEN avg(sum(Sales)) OVER (...) END with the same frame.

Where is this documented?