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 (PARTITIONBY Region ORDERBY Month ROWSBETWEEN1PRECEDINGANDCURRENTROW) AS result
FROM input
GROUPBY Region, Month
ORDERBY 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.
Region
Month
Result
East
1
100
East
2
110
East
3
90
East
4
90
West
1
50
West
2
60
West
3
70
West
4
90
✓ 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 (PARTITIONBY Region ORDERBY Month) +sum(Sales))
/2AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
avg(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month ROWSBETWEEN1PRECEDINGANDCURRENTROW) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
100
null
100
East
2
110
110
110
East
3
90
90
90
East
4
90
90
90
West
1
50
null
50
West
2
60
60
60
West
3
70
null
70
West
4
90
null
90
✓ 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 (PARTITIONBY Region ORDERBY Month ROWSBETWEEN1PRECEDINGANDCURRENTROW) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
AVG(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month ROWSBETWEEN1PRECEDINGANDCURRENTROW) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.