# Top 5 and Bottom 5 Product within each categories

Hi folks,

I want to calculate Top 5 and Bottom 5 products within each category.
For Top 5 Products within each category I have used this formula:

Top 5 Products within each category =
VAR RankingContext = VALUES(‘Dim Products’[Products])
return
CALCULATE([Sum of sales],TOPN(5,ALL(‘Dim Products’[Products]),[Sum of sales]),RankingContext)

I am using Category and Product(Drill down) in rows section of Matrix and the Measure " Top 5 Products within each category" in the values section.
Its working well for Top 5 but I need help in calculating the bottom 5 products within each category.

Hi there!

Did you know you can also filter on Top 5/Bottom 5 in the filter pane:

Kind regards,
Rens

Yes I know this but what I want is Bottom 5 Products within each category.It is not possible with this.

Hi ,
can share the PBIX file?

can do a ranking with a group of data, but need to use Power Query

lb

See if this will helps you solve it.

Link to knowledge base on TOPN.
You can set the order to be applied. 0/FALSE/DESC – descending; 1/TRUE/ASC – ascending.

.
Related content on dealing with categories

If this doesn’t help resolve the issue then like @LucaB72 already suggested provide a mock up PBIX file so members can better assist you.

Give this a go:

``````Top 5 by Category =

VAR SalesThisProduct = [Total Sales]
VAR RankingTopDown =
CALCULATE (
COUNTROWS (
FILTER (
ALLSELECTED ( Products[Product Name] ),
[Total Sales] >= SalesThisProduct
)
),
ALLEXCEPT ( Products, Products[Product Category] )
)
VAR Top5 =
IF ( RankingTopDown <= 5, RankingTopDown, BLANK () )
RETURN
Top5
``````

Bottom 5 is a little more complicated:

``````Bottom 5 by Category =

VAR SalesThisProduct = [Total Sales]
VAR AllRows =
CALCULATE ( COUNTROWS ( Products ), ALL ( Products ) )
VAR RankingBottomUp =
CALCULATE (
COUNTROWS (
FILTER (
ALLSELECTED ( Products[Product Name] ),
[Total Sales] <= SalesThisProduct
)
),
ALLEXCEPT ( Products, Products[Product Category] )
)
VAR MinRankByCategory =
CALCULATE (
MINX ( Products, [Rank by Category] ),
ALLEXCEPT ( Products, Products[Product Category] )
)
VAR AdjRank = RankingBottomUp - MinRankByCategory + 1
VAR Bottom5 =
RETURN
Bottom5
``````

Here it is all put together:

I hope this is helpful. Full solution file attached.

1 Like

Hi @ajinkyakadam0009, we aim to consistently improve the topics being posted on the forum to help you in getting a strong solution faster. While waiting for a response, here are some tips so you can get the most out of the forum and other Enterprise DNA resources.

• Use the forum search to discover if your query has been asked before by another member.
• When posting a topic with formula make sure that it is correctly formatted to preformatted text </>.
• Use the proper category that best describes your topic
• Provide as much context to a question as possible.
• Include the masked demo pbix file, images of the entire scenario you are dealing with, screenshot of the data model, details of how you want to visualize a result, and any other supporting links and details.

I also suggest that you check the forum guideline How To Use The Enterprise DNA Support Forum. Not adhering to it may sometimes cause delay in getting an answer.