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! Request now

Reply
awitt
Helper III
Helper III

RANKX with String Filter

Hi, 

 

I am looking to rank the top 5 products based on sales. This below works for all products.

 

Top 5 Products = 
VAR ProductRank = RANKX (ALL('ePOS Bookkeeping Report'[Product]), CALCULATE(sum('ePOS Bookkeeping Report'[NET Sales]),,DESC))
RETURN
IF( ProductRank <= 5, CALCULATE(SUM('ePOS Bookkeeping Report'[NET Sales])),blank())

 

 

However, I want to add a filter statment in the calculate function so that only the products of a certain group are shown and not all products. So in the below example, this would ideally return the top 5 "Clothing" products. However all of the results generate the same rank of 1 in this case. 

 

Top 5 Products = 
VAR ProductRank = RANKX (ALL('ePOS Bookkeeping Report'[Product]), CALCULATE(sum('ePOS Bookkeeping Report'[NET Sales]),FILTER('ePOS Bookkeeping Report','ePOS Bookkeeping Report'[Category (groups)] = "CLOTHING")),,DESC)
RETURN
IF( ProductRank <= 5, CALCULATE(SUM('ePOS Bookkeeping Report'[NET Sales])),blank())

 

 

I've also tried this, which yeilds 0 results. 

 

 

Top 5 Products = 
VAR ProductRank = RANKX (ALL('ePOS Bookkeeping Report'[Product]), CALCULATE(sum('ePOS Bookkeeping Report'[NET Sales])),,DESC)
RETURN
IF( ProductRank <= 5, CALCULATE(SUM('ePOS Bookkeeping Report'[NET Sales]),FILTER('ePOS Bookkeeping Report','ePOS Bookkeeping Report'[Category (groups)] = "Clothing")),blank())

 

 

Thoughts?

2 REPLIES 2
v-easonf-msft
Community Support
Community Support

Hi, @awitt 

You can try measure as below:

Top 5 Products of CLOTHING =
VAR rank1 =
    RANKX (
        ALLEXCEPT (
            'ePOS Bookkeeping Report',
            'ePOS Bookkeeping Report'[Category (groups)]
        ),
        CALCULATE ( SUM ( 'ePOS Bookkeeping Report'[NET Sales] ) ),
        ,
        DESC
    )
RETURN
    IF (
        MAX ( 'ePOS Bookkeeping Report'[Category (groups)] ) = "CLOTHING"
            && rank1 <= 5,
        CALCULATE ( SUM ( 'ePOS Bookkeeping Report'[NET Sales] ) ),
        BLANK ()
    )

14.png

 

Best Regards,
Community Support Team _ Eason

Greg_Deckler
Community Champion
Community Champion

@awitt This might help: To *Bleep* with RANKX! - Microsoft Power BI Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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