Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now
Hi!
I have this DAX code for a measure, which calculates the investment value for Top N advertisers and for the "others" group.
In short, it:
It's all good if I want to display it as a total, but if I put it in a graph/table that shows the result in different periods, it calculates a new Top N for each period.
An example for a visual result:
What I need: The measure calculates a Top 3 for the whole data period, then shows how much each of the three advertisers spent each month.
What I have now: The measure calculates a Top 3 for each of the months, in result having a ton on different advertisers in one graph.
Here is a screenshot of the code:
Here is the code:
TOPN Investment (A) =
// Get the selected value for the number of top advertisers to consider.
VAR TOPNSelected = SELECTEDVALUE('TopN Selection'[Top player count])
// Get the currently selected advertiser.
VAR CurrentAdvertiser = SELECTEDVALUE('Pseudo Advertiser Table'[Advertiser])
// Calculate the top N advertisers based on the 'Investment type' measure.
VAR TopAdvertisers = TOPN(TopNSelected, ALLSELECTED('Pseudo Advertiser Table'[Advertiser]), 'Investment type measure'[Investment type])
// Calculate the investment amount for the top advertisers, preserving other filters.
VAR TopAdvertiserSales = CALCULATE('Investment type measure'[Investment type], KEEPFILTERS(TopAdvertisers))
RETURN
// Check if there's only one selected advertiser.
IF(HASONEVALUE('Pseudo Advertiser Table'[Advertiser]),
// If one advertiser selected, switch based on its category.
SWITCH(TRUE(),
// If the selected advertiser is not "Others," return investment for top N advertisers.
CurrentAdvertiser <> "Others", TopAdvertiserSales,
// If the selected advertiser is "Others," calculate investment for the "Other" category.
CurrentAdvertiser = "Others",
CALCULATE('Investment type measure'[Investment type], ALLSELECTED('Pseudo Advertiser Table'[Advertiser]))
- CALCULATE('Investment type measure'[Investment type], TopAdvertisers)
),
// If multiple advertisers selected, return investment for the current advertiser.
'Investment type measure'[Investment type]
)
...
I think it must be something with the filters inside the measure? I'm not sure since I'm no pro in PBI, I mainly just followed this link to create a Top N + others formula.
If anyone can help me modify this formula so that the TOP is based on the total period and stays the same regardless of how I visualize it, it would be largely appreciated!
@vulpix999 , Please refer if my approach can help
Power BI- TOPN with Others- https://youtu.be/I_TY4hVlzAE
https://medium.com/microsoft-power-bi/power-bi-topn-others-8b094203a306
Hello! I appreciate your response, but the method you've shared still has the same problem I mentioned. When visualizing the result over various periods, for example - years, it calculates a new Top for each year. However, I require the Top to be determined based on the entire period and remain constant during visualization.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
Check out the October 2025 Power BI update to learn about new features.