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