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
Anonymous
Not applicable

Rank based on datetime

Hi,

 

I need some help. 

 

I want to add a Rank column to below table based on the AgentID and Datetime column.

 

So the most recent Datetime for the agent should be 0, then 1 so on 

 

If for any agent (i.e AgentID 3) Datetime is same and has AgentStatis "on Call" then that should be ranked 0. Otherwise any can be ranked 0 

 

AgentIDAgentStatusDateTimeRank (New Column)
1Log in10/02/21  15:05:50          0
1On Call10/02/21  12:10:10          1
1Break09/02/21  16:05:52          2
2Log in10/02/21  02:05:39          0
2Log in10/02/21  01:10:22          1
3On Call10/02/21  12:05:15          0
3Log Off10/02/21  12:05:15          1

 

Thanks 

 

Daven

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@Anonymous try this measure

 

Rank = 
VAR __countFlag = 
    CALCULATE ( 
        COUNTROWS ( 'Rank' ), 
        ALLEXCEPT ( 'Rank', 'Rank'[AgentID], 'Rank'[DateTime] ) 
    ) 
VAR __rank = 
    RANKX ( 
        ALLEXCEPT ( 'Rank', 'Rank'[AgentID] ), 
        CALCULATE ( MIN ( 'Rank'[DateTime] ) ), , 
        DESC, DENSE 
    )
RETURN  
IF ( 
    __countFlag > 1 && MAX ( 'Rank'[AgentStatus] ) = "On Call", 0, 
    __rank - IF ( __countFlag = 1, 1, 0 ) 
)

 

Check my latest blog post Compare Budgeted Scenarios vs. Actuals I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

2 REPLIES 2
parry2k
Super User
Super User

@Anonymous try this measure

 

Rank = 
VAR __countFlag = 
    CALCULATE ( 
        COUNTROWS ( 'Rank' ), 
        ALLEXCEPT ( 'Rank', 'Rank'[AgentID], 'Rank'[DateTime] ) 
    ) 
VAR __rank = 
    RANKX ( 
        ALLEXCEPT ( 'Rank', 'Rank'[AgentID] ), 
        CALCULATE ( MIN ( 'Rank'[DateTime] ) ), , 
        DESC, DENSE 
    )
RETURN  
IF ( 
    __countFlag > 1 && MAX ( 'Rank'[AgentStatus] ) = "On Call", 0, 
    __rank - IF ( __countFlag = 1, 1, 0 ) 
)

 

Check my latest blog post Compare Budgeted Scenarios vs. Actuals I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thanks Parry 

 

It 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.

Top Solution Authors