Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
vulpix999
Frequent Visitor

Top N + others for whole period

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:

  1. Takes the top N advertisers by invested value (where N is specified in the 'TopN Selection' table).
  2. Calculates the investments for these top N advertisers.
  3. Groups all other advertisers (those not in the top N) into the "Other" category.
  4. Calculates the investment amount for the entire "Other" category.

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:

Screenshot 2023-07-25 095934.png

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!

 

 

2 REPLIES 2
amitchandak
Super User
Super User

@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

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors