To show the last N months in a Power BI matrix, create a measure that filters the date table to the last N months relative to today (or the latest date in your data), then use that measure in the matrix Values and place Month on Rows. The key is a dedicated Date table marked as a date table, a measure using DATESINPERIOD or TOPN, and a matrix visual with Month and any other grouping on Rows and the measure in Values. This guide walks through the exact steps, common pitfalls, and fixes.
What You'll Need
- Power BI Desktop (any recent version) with a data model containing a fact table and a separate Date table.
- A Date table that is marked as a date table and has a relationship to your fact table.
- Basic familiarity with creating measures and using the matrix visual.
- Your data refreshed so that "today" or the latest date is meaningful.
Step-by-Step
- Create or confirm your Date table. If you don't have one, use Modeling > New table and a DAX expression such as
DateTable = CALENDAR(DATE(2020,1,1), DATE(2025,12,31))(adjust the range to cover your data). Then mark it as a date table: select the table, go to Table tools > Mark as date table, and choose the date column. This ensures time intelligence functions work correctly. Labels in the ribbon can differ slightly by version, but the option is in the Table tools area. - Relate the Date table to your fact table. In Model view, drag the Date column of the Date table onto the date column of your fact table to create a one-to-many relationship (Date table on the "one" side). Ensure the relationship is active.
- Decide the anchor date. You can base "last N months" on TODAY() (dynamic, moves with the calendar) or on the maximum date in your fact table (useful when your data lags). For most reports, TODAY() is fine; for historical snapshots, use . The measure below uses TODAY() ; replace it with if needed.
- Create the base measure. For example,
Total Sales = SUM(FactTable[Amount]). This is the value you will show in the matrix. - Create the last-N-months measure. Use DATESINPERIOD to shift the filter context. Example for 6 months: . The -6 means six months back from today, including the current month. Adjust the number as needed.
- Build the matrix visual. Add a Matrix visual to the canvas. Drag
DateTable[Year]andDateTable[Month](or a Month-Year column) into the Rows area. Drag your last-N-months measure into Values. Optionally add another dimension (e.g., Product Category) to Columns or Rows for cross-tabulation. - Filter out empty months. By default, the matrix may show months with no data. To hide them, add a filter on the matrix visual: drag to the Filters pane, choose "Advanced filtering", and set it to "is on or after" a relative date, or use a measure-based filter. Alternatively, use the measure itself: if the measure returns BLANK for months outside the range, the matrix will still show the row but with blank values. To remove rows entirely, apply a visual-level filter on the Date table using a measure like and set the filter to 1.
- Format the matrix. Use the Format pane to adjust column widths, turn on subtotals if needed, and set the measure to display as currency or whole numbers. You can also sort the Month column chronologically by selecting the Month column and using Sort by column in the modeling tab to sort by a MonthNumber column.
- Test the result. Change the N value in the measure to verify the matrix updates. Check that the earliest month shown is exactly N months before today (or your anchor date) and that no older months appear.
Common Mistakes
- Using the fact table's date column instead of the Date table. Time intelligence functions like
DATESINPERIODrequire a marked Date table. Using the fact table's date column can produce incorrect results or errors. - Forgetting to mark the Date table as a date table. Without this,
DATESINPERIODmay not work as expected, and relationships may behave oddly. - Using TODAY() when data is not current. If your dataset ends months ago, TODAY() will show empty months. Use as the anchor instead.
- Not filtering out months with no data. The matrix will display all months in the Date table unless you filter. Use a visual-level filter or a measure to exclude them.
- Incorrect sign in
DATESINPERIOD. The number of intervals should be negative to go back in time. A positive number will look forward and return blank. - Sorting months alphabetically. Without a MonthNumber column and Sort by Column, months will sort as April, August, etc. Create a numeric month column and set it as the sort column for the Month name.
Tips
- Use a What-If parameter to let users choose N dynamically. Create a parameter with Modeling > New parameter > Numeric range , then reference it in the measure: .
- For a rolling N months that always ends at the latest data point, replace TODAY() with or .
- If you need to show the last N complete months (excluding the current partial month), use
EOMONTH(TODAY(), -1)as the end date and adjust the interval accordingly. - Consider adding a "Month Offset" column to your Date table (e.g., ) and use it as a filter (0 to N-1) instead of a measure. This can be simpler for visual-level filtering.
- Test with a small N (like 3) to verify the logic before scaling up.
Troubleshooting
- The measure returns blank for all months. Check that the Date table is marked as a date table and that the relationship to the fact table is active. Also verify the sign of the interval in
DATESINPERIOD(should be negative). - The matrix shows months outside the last N. Ensure you have applied a filter to the visual or that the measure is used in Values. If you placed the Date column on Rows without filtering, all months appear. Add a visual-level filter on the Date table using a measure or the relative date filter.
- The measure works but totals are wrong. Check that the measure is used in the Values area and that no other filters are interfering. Use Edit interactions to ensure other visuals are not filtering the matrix unexpectedly.
- Months are sorted alphabetically. Create a MonthNumber column (1-12) and use Sort by column on the Month name column.
- The last N months are not correct when using TODAY() . Remember that TODAY() uses the local date of the machine running the report. For published reports, it uses the UTC date of the Power BI service. If this matters, use instead.
Frequently Asked Questions
Can I use a slicer to change the number of months?
Yes. Create a What-If parameter (Modeling > New parameter > Numeric range) and reference it in the measure instead of a hard-coded number. Users can then adjust the slicer to change N.
Do I need a separate Date table?
Yes, for reliable time intelligence. A marked Date table ensures that functions like DATESINPERIOD work correctly and that relationships behave as expected.
Why does my matrix show months with no data?
The matrix displays all values from the rows field unless filtered. Apply a visual-level filter on the Date table (e.g., using a measure that flags the last N months) to exclude empty months.
Can I show the last N months relative to the latest data instead of today?
Yes. Replace TODAY() in the measure with or .
What if my fiscal year starts in a different month?
You can still use DATESINPERIOD with months; it does not depend on fiscal year. Just ensure your Date table includes fiscal columns if you need fiscal reporting.
Does this work in Power BI Service?
Yes, the measure and matrix work the same after publishing. Note that TODAY() uses the service's date, which may differ from your local date.