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! Learn more

Reply
bdehning
Post Prodigy
Post Prodigy

Rankx TOP 5 Value Help

 

bdehning_0-1740330787104.png

I want Top 5 Reporting Location Cities by Count and for each City the Top 5 Cause by Count and break ties by using Total Incurred


This is used to get Reporting Location City using Top N and Top 5 by Value of Measure

Sort measure Count Location Cause =

VAR _FreqByCity = CALCULATE([Count of Total Gross Incurred], FILTER(ALL(InjuryCause[Cause Grouping]), [Top 5 by frequency and Incurred]<6))

VAR _LN = LEN(FORMAT(CALCULATE([Count of Total Gross Incurred], ALL('LossRun'[Reporting Location City])), "text"))

VAR _Pre = _FreqByCity *POWER(10, _LN*2)

VAR _Inc = CALCULATE(RANKX(ALLSELECTED(LossRun[Reporting Location City]), [Sum of Total Gross Incurred],,ASC,Dense), ALLSELECTED(InjuryCause[Cause Grouping]))

VAR _Mid = _Inc * POWER(10, _LN)

RETURN

IF(ISBLANK([Count of Total Gross Incurred]), BLANK(), _Pre + _Mid + RANKX(ALLSELECTED(InjuryCause[Cause Grouping]),[Ref],,ASC,Skip))


This is used to get the Top 5 Injury Causes -

Top 5 by frequency and Incurred =

IF (

    ISBLANK ( [Sum of Total Gross Incurred] ),

    BLANK (),

    RANKX ( ALL ( InjuryCause[Cause Grouping] ), [Ref],, DESC, SKIP )

)

Here are filters used 

bdehning_1-1740330876701.png

Table Ok but Clustered Column Chart is not?

bdehning_2-1740330904663.png

Why does table work and Visual does not?  

 

3 REPLIES 3
bdehning
Post Prodigy
Post Prodigy

I forgot to include another measure that is used inside both Measures above that may impact your solution.  

  

Ref =
VAR _MX =
    MAXX (
        ALL ( InjuryCause[Cause Grouping] ),
        CALCULATE ( SUM ( LossRun[Total Gross Incurred] ) )
    )
VAR _LNGTh =
    LEN ( FORMAT ( INT ( _MX ), "Text" ) ) + 1
RETURN
    COUNT ( LossRun[Total Gross Incurred] ) * POWER ( 20, _LNGTh )
        + SUM ( LossRun[Total Gross Incurred] )



Sahir_Maharaj
Super User
Super User

Hello @bdehning,

 

If ALL or ALLSELECTED is used in RANKX, the filtering behavior in charts can differ, leading to missing or unexpected results. Can you please try this approach:

 

Ranked Cities = 
VAR _TopCities = 
    ADDCOLUMNS (
        SUMMARIZE (
            LossRun,
            LossRun[Reporting Location City]
        ),
        "@Rank", RANKX (
            ALL(LossRun[Reporting Location City]),
            [Count of Total Gross Incurred],
            ,
            DESC,
            DENSE
        )
    )
RETURN
    IF (
        LOOKUPVALUE ( "@Rank", _TopCities, LossRun[Reporting Location City] ) <= 5,
        [Count of Total Gross Incurred]
    )
Ranked Causes = 
VAR _TopCauses = 
    ADDCOLUMNS (
        SUMMARIZE (
            LossRun,
            InjuryCause[Cause Grouping],
            LossRun[Reporting Location City]
        ),
        "@Rank", RANKX (
            FILTER (
                ALLSELECTED ( InjuryCause[Cause Grouping] ),
                LossRun[Reporting Location City] = SELECTEDVALUE ( LossRun[Reporting Location City] )
            ),
            [Sum of Total Gross Incurred],
            ,
            DESC,
            DENSE
        )
    )
RETURN
    IF (
        LOOKUPVALUE ( "@Rank", _TopCauses, InjuryCause[Cause Grouping], LossRun[Reporting Location City] ) <= 5,
        [Sum of Total Gross Incurred]
    )

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Both Returns on the two measures say the say thing.

 

Function LOOKUPVALUE expects a column reference as Argument number 1. 

 

Then "@Rank" - Parameter is not the correct type and Cannot find name "[Reporting Location City]"

 

Ranked Causes 

It says [Cause Grouping] - Cannot find name "[Cause Grouping]" in addition to what is above in Ranked Cities
 
Help?

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