How to create a Top N filter by month in Power BI: step by step
This tutorial shows how to create a dynamic Top N by month filter in Power BI: useful when you want to show only the top products/customers per month and prevent small items from cluttering the report. You will understand why the method works and see a practical example with DAX and visualizations. The goal is that, when selecting a month, the report shows only the N items with the highest Sales in that period — for example, Top 5 in January, Top 10 in February, and so on.
Prerequisites
- Power BI Desktop installed.
- A fact table with columns: Date, Item, Sales (or equivalent). Ideally you have tens of thousands of rows (e.g.: 10,000–100,000) and a few hundred items (e.g.: 200–500).
- A Calendar/Date table related to the fact Date. The Date table should be continuous (no missing days) to ensure correct time filters.
For example, if you have 12 months and 500 items with monthly sales, a report without Top N can end up with hundreds of bars; Top N by month focuses attention on the leaders for each month.
Step 1: Prepare data and relationships
Why: it is important to have a relationship between the fact table and the Date table to be able to aggregate by month. Confirm that your Date table has Year, Month, MonthName columns and a continuous Date column. Also ensure that the MonthName column is in a format that facilitates ordering (e.g.: "2023-01") and that the Date table is linked to the fact with a 1→* relationship (Date[Date] → Fact[Date]).
// Exemplo mínimo de tabela Date em Power Query (M)
let
Start = #date(2022,1,1),
End = #date(2023,12,31),
Days = List.Dates(Start, Duration.Days(End-Start)+1, #duration(1,0,0,0)),
TableDates = Table.FromList(Days, Splitter.SplitByNothing(), {"Date"}),
AddYear = Table.AddColumn(TableDates, "Year", each Date.Year([Date])),
AddMonth = Table.AddColumn(AddYear, "Month", each Date.Month([Date])),
AddMonthName = Table.AddColumn(AddMonth, "MonthName", each Date.ToText([Date], "yyyy-MM"))
in
AddMonthName
Practical notes: in the model, sort MonthName by Year and Month to ensure chronological order in visuals. Confirm the relationship between tables is active and in the correct direction (Single direction normally works for this pattern).
Step 2: Create sales measure
Why: we need a base measure to sum sales by Item and by month. Use a simple, tested measure. This measure is the reference point for the Rank and for the Top N filter.
Sales = SUM('Fact'[Sales])
Example: if in March an item has Sales = 12,345, that measure will return that value in the item+month context. Test the measure in a card or table to confirm aggregated numbers by month.
Step 3: Rank measure by month
Why: the Rank measure allows ordering and identifying the Top N within each period (month). We will use RANKX with ALLSELECTED to respect date slicers. The idea is to compare each Item with all other Items in the same selected month, calculating the position by Sales.
Rank by Month =
VAR CurrentMonth = VALUES('Date'[MonthName])
RETURN
RANKX(
FILTER(
ALLSELECTED('Fact'[Item]),
NOT(ISBLANK([Sales]))
),
CALCULATE([Sales], ALLSELECTED('Fact'[Item]), 'Date'[MonthName] = MAX('Date'[MonthName])),
,
DESC,
DENSE
)
Explanation: ALLSELECTED allows other slicers (e.g.: a Category slicer) to be respected, while removing the Item filter so Rank can compare all candidates. Using DENSE avoids gaps in case of ties (if two items tie for 1st, the next will be 2nd instead of 3rd). For large datasets (thousands of items) consider optimizations like SUMMARIZECOLUMNS to reduce RANKX work.
Step 4: Create Top N parameter with slicer
Why: allow the user to choose how many items they want to see (Top 5, Top 10). The simple way is to create a value table and a slicer. Typical values range from 1 to 20; choose according to your data density.
TopN Selection = GENERATESERIES(1,20,1)
Add a slicer to the report from TopN Selection[Value] and choose single select. For example, a user can select 5 to see the 5 largest items per month. If you want default values, set the slicer with a preselected value (e.g.: 5).
Step 5: Measure to filter Top N by month
Why: we need a measure that returns 1 for items within the Top N of the selected month and BLANK() for the others, to use as a visual filter. This measure is used in the visual Filters pane to hide items that are not part of the Top N.
Show Top N =
VAR N = SELECTEDVALUE('TopN Selection'[Value], 5)
VAR r = [Rank by Month]
RETURN
IF(NOT(ISBLANK(r)) && r <= N, 1, 0)
Behavior: if N = 5, any item with Rank by Month ≤ 5 receives 1. If there are no sales for an item in that month, r will be BLANK and the item is excluded. This avoids showing zeros or items without transactions.
Step 6: Apply the filter to the visual
Why: by applying the measure as a visual filter, we show only the items that meet the Top N condition in the month. Put Item on the axis (or in table rows) and Sales as the value.
- Create a column chart or table with 'Fact'[Item] and the Sales measure.
- In the visual Filters pane, drag the Show Top N measure and select 'is 1'.
- Add the MonthName slicer (or the Date slicer) to select the month you want to analyze.
- Use the TopN Selection slicer to choose how many items to see.
Practical tip: combine this filter with a Category slicer to see the Top N by category in the same month (ALLSELECTED in the Rank measure will allow that).
Verify the result
To confirm everything is correct: select a month in the Date/MonthName slicer and view the chart or table. Only the N items with the highest Sales for that month should appear. Test with several months; if different items appear depending on the month, the filter is working. Verification examples: January shows 5 items with Sales of 25k, 18k, 12k, 9k, 7k; February shows a different set because rankings changed.
If fewer items appear than the selected N, check whether there are sales for all items in that month (context filters can remove rows). If the Rank seems incorrect, verify that the MonthName column used in the context exactly matches the one in the Date table and that the relationship is active.
Conclusion
You now have a Top N by month filter in Power BI that is interactive and respects date slicers. Next steps: adapt the Rank for percentiles, include categories (Top N by category) or create a button to toggle between Top N and show all. Tip: if you see unexpected results, check relationships between tables, data types and whether the MonthName column is consistent throughout the modeling. For large databases, monitor performance and consider creating aggregated columns in the ETL to speed up calculations.