TOPN (List of Top Ten Products)

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
Hi there PBI Pros,

I need some help getting the Top 10 products by sales.

My Product names are in a Table called MAIN, my Sales Column is in a Table called VALUES
i.e. MAIN[PRODUCTS] and VALUES[SALES]

I would like to create a DAX Function to get the list.
I am using this in Power BI Desktop.

The Result I am looking to achieve is to have a column chart with the names of the Top 10 Products with the Value being the SALES.

Pulling out my hair, help.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You need to take a step back from the Excel world and think in DAX. In Excel you could create a pivot and put a topn filter on the pivot table. No such feature currently exists in Power BI. So the question is, How can you do this in DAX? There're are a few approaches you could try, depending on what you need. If it is top 10, and always top 10, then you could try this.
Break the problem into pieces.
You could create a rankx measure to rank each product from 1 to n. Create a measure that does that and stick it in a visualisation. Each product will have a number. It is helpful to "see" the interim results in a visualisation as it helps you work through the problem. Then you could filter with an if statement to on,y keep the top 10. Something like this

top 10 sales = if ([my rankx measure] <= 10, sum(values[sales]))
put this new measure in your visualisation and remove the rankx, and it should give you what you want.

another approach is is you could put a rankx column in the product table and use it to filter your chart via slicer. The problem with this is it won't respond to different time periods.
 
Upvote 0
Hello Matt,

Firstly thank you for the help. (I have been playing around with DAX for a few days now, still struggling with the basics).

By following your steps, I was able to get it done. It was fun. I did a few manual Steps to confirm the results - all good.

RankedSites = RANKX(ALL(MAIN[SITE NAME]),CALCULATE(SUMX(RELATEDTABLE('VALUES'),'VALUES'[AMOUNT]),'VALUES'[VALUE TYPE]="VALUE",'VALUES'[YEAR]=YEAR(TODAY())),CALCULATE(SUM('VALUES'[AMOUNT]),'VALUES'[VALUE TYPE]="VALUE",'VALUES'[YEAR]=YEAR(TODAY())),DESC)

Top10Sites = IF([RankedSites]<=10,CALCULATE(SUM('VALUES'[AMOUNT]),'VALUES'[VALUE TYPE]="VALUE",'VALUES'[YEAR]=YEAR(TODAY())))

Thank you again :)
 
Upvote 0

Forum statistics

Threads
1,224,151
Messages
6,176,713
Members
452,740
Latest member
MrCY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top