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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

DAX - RANKX with Partition By

Hi,

 

I am trying to create a rankx measure that will create a row number by date but also partitioned by category. This will also dynamically update when the user changes the date filter. For example:

 

CategoryDatern
a01/01/20221
a02/01/20222
a03/01/20223
b01/01/20221
c02/01/20221
d03/01/20221

 

If the user then change the date slicer to 02/01/2022 - 03/01/2022 the result would be:

 

CategoryDatern
a02/01/20221
a03/01/20222
c02/01/20221
d03/01/20221

 

Any help would be most welcome!

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Anonymous 

Something like this (replace Table references as necessary):

rn = 
VAR CurrentDate =
    SELECTEDVALUE ( YourTable[Date] )
VAR RankingTable =
    CALCULATETABLE ( 
        SUMMARIZE ( YourTable, YourTable[Date] ),
        ALLSELECTED (), -- filter context of visual
        VALUES ( YourTable[Category] ) -- retain current Category filter
    )
RETURN
    RANKX (
        RankingTable,
        YourTable[Date],
        CurrentDate,
        ASC
    )

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

6 REPLIES 6
CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1647121249834.png

 

For fun only, the logical is replicated in Excel,

CNENFRNL_1-1647121338276.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Thanks CNENFRNL, this worked also.

OwenAuger
Super User
Super User

Hi @Anonymous 

Something like this (replace Table references as necessary):

rn = 
VAR CurrentDate =
    SELECTEDVALUE ( YourTable[Date] )
VAR RankingTable =
    CALCULATETABLE ( 
        SUMMARIZE ( YourTable, YourTable[Date] ),
        ALLSELECTED (), -- filter context of visual
        VALUES ( YourTable[Category] ) -- retain current Category filter
    )
RETURN
    RANKX (
        RankingTable,
        YourTable[Date],
        CurrentDate,
        ASC
    )

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn
Anonymous
Not applicable

Hi OwenAuger, after getting the row number I am trying to count the categories where the row number = 1 which I thought would be a simple calculate function:

 

Count RN =
CALCULATE(
COUNT(Table, Table[Category]), FILTER(Table,'_DAX Measures'[_rn] = 1))

This doesn't seem to work. Do you know how to get round this?
 
Thanks in advance

I'm thinking something like this, if you want to count the number of times [_rn]=1 in that particular visual, assuming you're placing this as a standalone measure outside the original visual.

Count RN =
SUMX (
    SUMMARIZE (
        Table,
        Table[Category],
        Table[Date]
    ),
    IF ( [_rn] = 1, 1 )
)

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn
Anonymous
Not applicable

Thanks OwenAuger, this worked.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

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!

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.