Sub topic 9
Moving averages
A moving average smooths a noisy series by averaging the most recent N periods at every point. In DAX you build one by replacing the current date filter with a window of dates that ends at the current date. This chapter shows two patterns, explains what each part returns, and lists the mistakes that give wrong numbers.
This chapter assumes you have a contiguous date table marked as the model's date table and related to your fact table. It also assumes you know how CALCULATE changes filter context, and that you have a [Total Sales] measure and have used variables and DIVIDE.
The working model
A moving-average measure does three things:
- Find the last date in the current filter context, usually with
MAX ( 'Date'[Date] ). - Build a table of dates that covers the window and ends on that date.
- Evaluate the base measure over that window and turn the result into an average.
The only new piece compared with year to date is step 2, and DATESINPERIOD does it.
A 30-day average
Sales 30 Day Avg =
VAR LastDate = MAX ( 'Date'[Date] )
VAR Window = DATESINPERIOD ( 'Date'[Date], LastDate, -30, DAY )
RETURN
AVERAGEX ( Window, [Total Sales] )On a line chart with 'Date'[Date] on the axis, each point has one date in its filter context, so LastDate is that date. Window is a one-column table holding that date and the 29 days before it. AVERAGEX visits each date in Window, evaluates [Total Sales] for that single day, and averages the results.
Both variables are evaluated in the filter context of the visual, before AVERAGEX starts iterating. That is the behavior you want. LastDate must come from the visual, not from inside the loop.
The arguments of DATESINPERIOD
| Argument | Meaning | Example |
|---|---|---|
| Dates column | The date column to return values from | `'Date'[Date]` |
| Start date | The anchor date, usually the last visible date | `LastDate` |
| Number of intervals | How many intervals to move. A negative number goes backward | `-30` |
| Interval | `DAY`, `MONTH`, `QUARTER`, or `YEAR` | `DAY` |
With a negative count, the anchor date is the last date in the window. -30, DAY returns exactly 30 days that include LastDate. With a positive count, the anchor is the first date instead, which is rarely what you want for a trailing average.
What AVERAGEX does with blanks
AVERAGEX ignores blank results. If a store sells nothing on weekends and [Total Sales] is blank on those days, the 30-day average divides by the number of days that had sales, not by 30.
A 3-month average
A month-level average needs more care. A window of three months is not a fixed number of days, and the first months of your data have fewer than three months behind them.
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 )
)Put 'Date'[Year Month] on a matrix row or a chart axis. For the March 2024 row, MAX ( 'Date'[Date] ) is 31 March. Going back three months gives a window of 1 January to 31 March 2024.
The numerator adds up sales across the whole window. The denominator counts the distinct Year Month values in the window, which is 3. DIVIDE returns the monthly average.
Why divide by distinct months
You could divide by a literal 3. That gives a wrong answer at the start of the data. The date table starts on 1 January 2020. For January 2020, the window reaches back to November 2019, but the date table has no rows for those dates. The window then contains one month, and the correct average is January's sales divided by 1. A hardcoded 3 would understate it by two thirds. Counting the months that exist keeps the early rows honest.
Why a window variable works inside CALCULATE
Window is a table of dates. Passing it to CALCULATE as a filter replaces the current date filter with those dates. Because the date table is marked, the replacement also clears other filters on the date table, such as the Year Month filter that the matrix row applies. Without that behavior, the March row would still be limited to March and the window could never reach February.
Requiring a full window
Early points on a chart average over fewer days than later points, so the left edge of the line is less reliable. If you prefer to show nothing until a full window exists, count the dates in the window.
Sales 7 Day Avg =
VAR LastDate = MAX ( 'Date'[Date] )
VAR Window = DATESINPERIOD ( 'Date'[Date], LastDate, -7, DAY )
VAR DaysInWindow = COUNTROWS ( Window )
RETURN
IF ( DaysInWindow = 7, AVERAGEX ( Window, [Total Sales] ) )COUNTROWS ( Window ) counts the dates the date table actually contains. It is 7 for every date from 7 January 2020 onward and smaller before that. The IF has no else branch, so those points return blank and the line starts at the first complete window.
Blanking dates after the last sale
Your date table runs to 31 December 2025, but sales may stop earlier. A moving average keeps producing values after the last sale, because the window still reaches back into days that have sales. The line then trails off for up to 30 days instead of ending. Cap it with the same check you use for year to date:
Sales 30 Day Avg Capped =
VAR LastSaleDate =
CALCULATE ( MAX ( Sales[Order Date] ), ALL ( 'Date' ) )
RETURN
IF ( MAX ( 'Date'[Date] ) <= LastSaleDate, [Sales 30 Day Avg] )ALL ( 'Date' ) removes the visual's date filters, so the variable holds the last date in the fact table and not the last date on the current row.
Common errors
- The moving average equals the plain total. The window is not being applied. This happens when the measure uses
MAX ( 'Date'[Date] )but filters a different date column, or when the visual uses a date column from the fact table instead of from the date table. Always use the date table's columns on axes, in slicers, and in these functions. - Early values look too low. You divided by a fixed number instead of the number of periods that exist, or you used
[Total Sales] + 0without capping dates, so empty days before the data started counted as zeros. - Values appear after the data ends. The date table extends past your last sale and the window still reaches back to real sales. Cap the measure.
- A day-level measure looks wrong at month level.
AVERAGEXover days returns the average per day, not per month. In a matrix by month,Sales 30 Day Avguses the last day of each month as its anchor. That is correct for a trailing 30 days, but you should label it as such. Use the month-based pattern when you want a monthly figure. - The window starts mid-month. If a visual is filtered to a partial month,
MAX ( 'Date'[Date] )is mid-month and a three-month window touches four distinct months. The measure divides by 4 and stays consistent, but the average covers a partial extra month. Anchor on complete months if you need strict comparability. - Gaps in the date table. Missing dates silently shrink the window. A "30 day" window may then cover 28 days of data. Check that the date table is contiguous.
Checking your measure
Pick one point on the chart and verify it by hand. For the 30-day average, filter a table visual to the 30 days ending at that point, add [Total Sales], and average the days with sales yourself. The figure should match the measure at that point. Do the same for the first and last points, because those expose the edge cases above.
Exercise
A 3-month average measure returns 12,000 for February 2020, the second month in a date table that starts on 1 January 2020. January sales were 10,000 and February sales were 14,000. Which denominator makes the result correct, and what does a hardcoded divisor of 3 return instead?
Show answer
The window for February 2020 reaches back to December 2019, which is not in the date table, so it contains only two distinct months. Dividing the total of 24,000 by 2 gives 12,000, which is correct. A hardcoded divisor of 3 returns 8,000, which understates the average.