cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Ramachandran
Helper III
Helper III

Rankx - Country and State level is not working

Hi,

I'm trying to get calculate the 3 largest state excluding the “Other States” dimension member, based on Sum of sales amount.  Then, calculate the top2 country for those 3 state again based on "Sum of Sales Amount".
So, what I did, I created Rank measure
Rank Country & State =
IF (
ISINSCOPE ( 'Sample'[State]),
if(LEFT(MIN('Sample'[State]), 6 )= "Other ", 0,
RANKX (
CALCULATETABLE (
VALUES ( 'Sample'[State] ),
ALLSELECTED ( 'Sample'[State])
),
[Sum of State Amount]
)
)
,
IF (
ISINSCOPE ( 'Sample'[Country]),
VAR CategorySales = [Sum of State Amount]
RETURN
CALCULATE (
RANKX (
VALUES ( 'Sample'[Country]),
[Sum of State Amount],
CategorySales
),
ALLSELECTED ()
)
)
)
but ranking measure is not fetching correct value. 
current output,

Ramachandran_0-1653900960850.png

I would expect output is below,

Ramachandran_1-1653901026991.png

Someone suggest me, how to I change the Ranking measure and get a solution.
For your information, Ranking calculation you dont use "Other States" dimension values that means exclude the Other States values. but you should show the report. 
The .pbix file available in https://drive.google.com/file/d/1pgrnS09XF9Lo419l8pTUUfpNYoNZkWaM/view?usp=drivesdk

thanks,

Ram

1 ACCEPTED SOLUTION
yingyinr
Community Support
Community Support

Hi @Ramachandran ,

I updated your sample pbix file(see attachment), please check whether that is what you want.

1. Update the measure [Rank-Country] and [Rank-State] as below

Rank-Country = 
VAR _selcountry =
    SELECTEDVALUE ( 'Sample'[Country] )
RETURN
    RANKX (
        ALLSELECTED ( 'Sample'[Country] ),
        SUMX ( ALLEXCEPT ( 'Sample', 'Sample'[Country] ), [Sum of State Amount] ),
        ,
        DESC,
        DENSE
    )
Rank-State = 
VAR _selcountry =
    SELECTEDVALUE ( 'Sample'[Country] )
VAR _selstate =
    SELECTEDVALUE ( 'Sample'[State] )
RETURN
    IF (
        _selstate = "Other States",
        BLANK (),
        RANKX (
            FILTER (
                ALLSELECTED ( 'Sample' ),
                'Sample'[Country] = _selcountry
                    && 'Sample'[State] <> "Other States"
            ),
            'Sample'[Sum of State Amount],
            ,
            DESC,
            DENSE
        )
    )

2. Apply a visual level filter on the matrix with the conditions:  [Rank-Country]<=2 and [Rank-State] <=3

yingyinr_0-1654158488831.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
yingyinr
Community Support
Community Support

Hi @Ramachandran ,

I updated your sample pbix file(see attachment), please check whether that is what you want.

1. Update the measure [Rank-Country] and [Rank-State] as below

Rank-Country = 
VAR _selcountry =
    SELECTEDVALUE ( 'Sample'[Country] )
RETURN
    RANKX (
        ALLSELECTED ( 'Sample'[Country] ),
        SUMX ( ALLEXCEPT ( 'Sample', 'Sample'[Country] ), [Sum of State Amount] ),
        ,
        DESC,
        DENSE
    )
Rank-State = 
VAR _selcountry =
    SELECTEDVALUE ( 'Sample'[Country] )
VAR _selstate =
    SELECTEDVALUE ( 'Sample'[State] )
RETURN
    IF (
        _selstate = "Other States",
        BLANK (),
        RANKX (
            FILTER (
                ALLSELECTED ( 'Sample' ),
                'Sample'[Country] = _selcountry
                    && 'Sample'[State] <> "Other States"
            ),
            'Sample'[Sum of State Amount],
            ,
            DESC,
            DENSE
        )
    )

2. Apply a visual level filter on the matrix with the conditions:  [Rank-Country]<=2 and [Rank-State] <=3

yingyinr_0-1654158488831.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

@Ramachandran , Refer if my example in the video can help

IsInScope - Switch Rank at different levels - https://youtu.be/kh0gezKICEM



Features || Datamarts: https://youtu.be/8tskWsJTEpg || Field Parameters : https://youtu.be/lqF3Wa1FllE?t=70
Time Intelligence Decoded : https://youtu.be/aU2aKbnHuWs&t=145s || Learn 50+ Power Query List Functions
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !! Proud to be a Super User! !!
Dashboard of My Blogs !! Connect on Linkedin !! Subscribe to my youtube Channel !!!! How to get Quick Help !!
Resources | Master Power BI !! Expertise Power BI !! Power BI For Tableau User !!

Hi Amit, I have watched your refered video's it will work to get rank both state and country level measures...
My screnario is state level exclude "Other states" dimension values.
how to exclude "Other States" dimension value and get state level ranking measure value
Rank-State = RANKX(ALLSELECTED('Sample'[State]), 'Sample'[Sum of State Amount], , DESC, Dense)
Rank-Country = RANKX(ALLSELECTED('Sample'[Country]), 'Sample'[Sum of State Amount], , DESC, Dense)
Rank Combine =
if(ISINSCOPE('Sample'[State]) , _MEASURES_[Rank-State], _MEASURES_[Rank-Country])

Helpful resources

Announcements
collabdays lisbon

CollabDays Lisbon - 26 November 2022

Sessions include practical, hands-on experience that will help you take the next step in your career and know-how.

Difinity Conference

Difinity Conference 2022

Difinity is the largest Microsoft Data, AI, Power BI, Power Platform and Business Applications Conference in New Zealand.

European Share Point Conference

European Share Point Conference

This conference returns live and in-person in Copenhagen this 28 November-01 December with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Microsoft 365 Conference â__ December 6-8, 2022

Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.