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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Sanjay_S
Frequent Visitor

Dynamic Ranking Based on Filter selection

Hello Guys,

 

I have a requirement wherein, I need to get Top 5 Brands based on Sales vales in a chart. 

 

The scenario is as follows:

 

The sample data is as below

 

Brand Sales
H3500
B2500
I2200
A1500
J1400
K900
E800
F700
L650
D600
C500
N200
M150
G100
Othersnull

 

Now, the requirement is to always show Top 5 brands based on sales. i.e., Top 4 brands and the 5th brand shown as Others aggregating all the other remaining brands.

 

When the user selects any brand from the slicer(single selection), that particular brand should be ranked - 1st and as usual the next top 3 brands and last one being 'Others' grouping the remaining.

 

I have managed to get the top 4 brands and others. But, stuck in getting the dynamic ranking based on the slicer selection.

 

Please see the below measures I created:

 

Sum of Sales

SumSales = SUM(Sheet1[Sales])

Rank

Rank = RANKX(ALL(Sheet1[Brand ]),[SumSales])

Top5

Top5 = IF ([Rank] <= 4,[SumSales],
  IF(HASONEVALUE(Sheet1[Brand ]),
    IF(VALUES(Sheet1[Brand ]) = "Others",
       SUMX ( FILTER ( ALL ( Sheet1[Brand ] ), [Rank] > 4 ), [SumSales] )
    ) 
  ) 
)

 

Regards,

Sanjay

 

2 REPLIES 2
v-ljerr-msft
Employee
Employee

Hi @Sanjay_S,

 

Based on my test, you can firstly create a new table with a single column of all distinct values of Brand, and make sure there is no relationship between the new table and Sheet1.

Table = DISTINCT(Sheet1[Brand])

Then the formulas below should work in your scenario(use Brand column from the new Table as Slicer).

Rank2 = 
IF (
    HASONEVALUE ( 'Table'[Brand] ),
    VAR selectedSales =
        CALCULATE (
            [SumSales],
            FILTER ( ALL ( Sheet1 ), Sheet1[Brand] = VALUES ( 'Table'[Brand] ) )
        )
    RETURN
        IF (
            [SumSales] <= selectedSales,
            RANKX ( FILTER ( ALL ( Sheet1 ), Sheet1[Sales] <= selectedSales ), [SumSales] ),
            -1
        ),
    RANKX ( ALL ( Sheet1[Brand] ), [SumSales] )
)
Top5 =
IF (
    [Rank2] <= 4
        && [Rank2] > 0,
    [SumSales],
    IF (
        HASONEVALUE ( Sheet1[Brand] ),
        IF (
            VALUES ( Sheet1[Brand] ) = "Others",
            SUMX ( FILTER ( ALL ( Sheet1[Brand] ), [Rank2] > 4 ), [SumSales] )
        )
    )
)

r3.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

Hello @v-ljerr-msft

 

Thanks for your reply. 

 

I tested your approach and seems like I didn't make the requirement quite clear. Sorry for that.

 

The requirement is when we select any 'Brand' from the slicer, that Brand should be ranked 1st, which is working in your method. But also, the other Top 3 brands should not be the brands whose Sales are lower than the selected Brand, instead it should be regular Top 3 brands.

 

For example : In the screenshot where you have selected 'B' as the Brand, 'B' should be the 1st Brand which is working and then it should ideally show 'H,I and A' as the next Top 3 brands.

 

Please check and let me know, I'll also be trying meanwhile.

 

Thanks,

Sanjay

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.