Sub topic 9
Moving Averages
This chapter assumes you have a marked date table with no gaps, a [Total Sales] measure, and a working grasp of CALCULATE and filter context. A moving average smooths daily or monthly noise by averaging a measure over a window that slides with the current date.
How the window works
DATESINPERIOD returns a table of dates. You give it a date column, a start date, a number of intervals, and an interval type (DAY, MONTH, QUARTER, or YEAR). A negative number looks backward from the start date.
In a visual, each cell has its own filter context. The measure reads the last visible date with MAX, builds a window that ends on that date, and then aggregates over the window.
A 30-day average
This measure averages the daily sales total across the last 30 days.
Sales 30 Day Avg =
VAR LastDate = MAX ( 'Date'[Date] )
VAR Window = DATESINPERIOD ( 'Date'[Date], LastDate, -30, DAY )
RETURN
AVERAGEX ( Window, [Total Sales] )AVERAGEX visits each date in Window and evaluates [Total Sales] for that date. Days where the total is blank are skipped. If your shop has no sales on Sundays, the average divides by the days that had sales, not by 30. Decide whether that matches your intent. To count empty days as zero, use [Total Sales] + 0 inside AVERAGEX.
A 3-month average
For monthly data, a window of 90 days is not the same as three months. DATESINPERIOD with MONTH keeps whole calendar months aligned.
Sales 3 Month Avg =
VAR Window = DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH )
RETURN
DIVIDE (
CALCULATE ( [Total Sales], Window ),
CALCULATE ( DISTINCTCOUNT ( 'Date'[Year Month] ), Window )
)The numerator is the total sales over the window. The denominator is the number of distinct months in the window, taken from the Year Month column. Because the denominator counts months that exist in the date table, early periods near the start of the table are still divided correctly. The window may hold fewer than three months there.
The Year Month column comes from the date table built earlier in this tutorial. If you used another column name, change it in the measure.
Choosing between the two patterns
| Pattern | Best for | Divides by |
|---|---|---|
| `AVERAGEX` over dates | Daily grain, or any grain where each date has a value | Dates in the window that have a non-blank total |
| Total divided by distinct months | Monthly trends shown at month level | Months in the window |
Common errors
- A flat or wrong result at month level. At month level,
MAX('Date'[Date])is the last day of the month, so a 30-day window reaches back only about one month. Use aMONTHwindow if you want a monthly average. - Blank results. If the date table is not related to the fact table, or has gaps, the window is incomplete. Check the relationship and the calendar range.
- Partial windows at the start. The first 29 days of data have fewer than 30 days behind them. The measure still returns a value. Hide it with a condition if you only want full windows.
- Using a fact-table date column. Pass
'Date'[Date]toDATESINPERIOD, notSales[Order Date]. Dates in the fact table may have gaps.
Exercise
The 3-month average measure divides total sales by DISTINCTCOUNT ( 'Date'[Year Month] ) over the window. Why is this safer than dividing by the constant 3 for the first two months of the date table?
Show answer
In the first two months, the window contains fewer than three months, because the date table starts there. Dividing by 3 would understate the average. Counting the distinct months actually in the window gives 1 for the first month and 2 for the second, so the average stays correct.
Exercise
Change Sales 30 Day Avg so that days with no sales count as zero instead of being skipped.
Show answer
Replace the last line with AVERAGEX ( Window, [Total Sales] + 0 ). Adding 0 turns a blank into 0, so every date in the window is included in the average.