Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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

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

2 Replies

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Parry 

       

      It worked!