WINDOW_AVG(SUM([Sales])) averages the expression over the marks in the partition and repeats it on each mark. In SQL that is avg(sum(Sales)) OVER (PARTITION BY Region). Null marks are skipped. With start and end, such as -1, 1, it is a moving average over ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING.
Updated · Checked on the xmetric engine
What is the syntax?
WINDOW_AVG(expression, [start, end])
expression
an aggregate, such as SUM([Sales]).
start, end
optional offsets from the current mark that bound the window; without them it is the whole partition. FIRST() and LAST() work here.
Returns a number: the average of the expression over the marks in the window.
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 all the months of each region?
Tableau
WINDOW_AVG(SUM([Sales]))
Compute using Month, restarting for each Region
SQL (DuckDB)
SELECT Region,
Month,
avg(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East averages its four marks to 100. West averages 50, 70 and 90 to 70; the null month is skipped.
Region
Month
Result
East
1
100
East
2
100
East
3
100
East
4
100
West
1
70
West
2
70
West
3
70
West
4
70
✓ The Tableau formula and the SQL return the same 8 rows.
How do I compute it across regions, within each month?
Tableau
WINDOW_AVG(SUM([Sales]))
Compute using Region, restarting for each Month
SQL (DuckDB)
SELECT Region,
Month,
avg(sum(Sales)) OVER (PARTITIONBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Region: the window is now the two regions of each month.
Region
Month
Result
East
1
75
East
2
95
East
3
60
East
4
105
West
1
75
West
2
95
West
3
60
West
4
105
✓ The Tableau formula and the SQL return the same 8 rows.
What does the bounded form return?
With start -1 and end 1, the window moves with the current mark and is clipped at the edges of each region. Each result column is one mark, by Region and Month.
Case
Tableau, then SQL (DuckDB)
East 1
East 2
East 3
East 4
West 1
West 2
West 3
West 4
bounded (-1, 1)
WINDOW_AVG(SUM([Sales]), -1, 1)Region, Month, avg(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
110
93.33
100
90
60
60
80
90
✓ On every row, each Tableau formula and its SQL return the same value.
Where does a straight translation go wrong?
Common mistake
Why does the SQL change from month to month?
With no start and end, WINDOW_AVG covers the whole partition. Adding ORDER BY Month to the SQL window shrinks the frame to "first row to current row", so it becomes a running calculation.
Tableau
WINDOW_AVG(SUM([Sales]))
Compute using Month, restarting for each Region
Tempting SQL
SELECT Region,
Month,
avg(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
avg(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
100
100
100
East
2
100
110
100
East
3
100
93.33
100
East
4
100
100
100
West
1
70
50
70
West
2
70
60
70
West
3
70
60
70
West
4
70
70
70
✓ 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) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
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.
Why no ORDER BY in the SQL?
Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
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.
Does WINDOW_AVG average marks or rows?
Marks. East month 2 counts once as 120, not as two rows of 80 and 40. For the row-level average see TOTAL.