Tableau function Table calculation pattern

Tableau Two-period moving average to SQL

WINDOW_AVG(SUM([Sales]), -1, 0) averages the previous and current marks. In SQL that is avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND CURRENT ROW). The first month of each region has no previous mark and averages itself; a null mark is skipped.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a number: the average of the expression over the previous and current 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 average each month with the one before?

Tableau

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

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

Compute using Month: East month 2 is (100 + 120) / 2 = 110. West month 3 has no sales and shows 70; month 4 shows 90.

RegionMonthResult
East1100
East2110
East390
East490
West150
West260
West370
West490

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

Where does a straight translation go wrong?

Common mistake

Why is the first month blank in the SQL?

(lag + current) / 2 is null whenever either side is missing: the first month and the months next to West's null. WINDOW_AVG averages the values it has.

Tableau

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

Compute using Month, restarting for each Region

Tempting SQL

SELECT
  Region,
  Month,
  (lag(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) + sum(Sales))
    / 2 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 CURRENT ROW) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month
RegionMonthTableauTempting SQLSQL that matches
East1100null100
East2110110110
East3909090
East4909090
West150null50
West2606060
West370null70
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 CURRENT ROW) 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 CURRENT ROW) AS result
FROM input
GROUP BY Region, Month
ORDER BY Region, Month

BigQuery documentation

What does your own Two-period moving 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 Two-period moving 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 make it a three-month trailing average?

Widen the start: WINDOW_AVG(SUM([Sales]), -2, 0), and in SQL ROWS BETWEEN 2 PRECEDING AND CURRENT ROW.

Where is this documented?