Forum Discussion

jimminy's avatar
jimminy
Frequent Visitor
3 years ago
Solved

Rank groups within unique IDs and by date

Hi community!

 

I have the current example data:

ticket_idstart_date_time_localService Team
1123/05/2023 11.42Team 1
1123/05/2023 11.46Team 1
1123/05/2023 11.47Team 2
1123/05/2023 11.47Team 3
2224/05/2023 15.16Team 5
2225/05/2023 14.01Team 5
2207/06/2023 05.19Team 2
2203/07/2023 15.59Team 2
2204/07/2023 12.25Team 4
2204/07/2023 12.25Team 1
2204/07/2023 14.47Team 1

 

And am trying to create a ranked column with the below output:

ticket_idstart_date_time_localService TeamRank
1123/05/2023 11.42Team 11
1123/05/2023 11.46Team 11
1123/05/2023 11.47Team 22
1123/05/2023 11.47Team 33
2224/05/2023 15.16Team 51
2225/05/2023 14.01Team 51
2207/06/2023 05.19Team 22
2203/07/2023 15.59Team 22
2204/07/2023 12.25Team 43
2204/07/2023 12.25Team 14
2204/07/2023 14.47Team 14

 

Thanks!

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi jimminy ,

     

    Here's my solution.

    1.Create an index column in Power Query.

     

    2.Go back to Power BI Desktop and create two calculated columns.

    Column = 
    IF(
        'Table'[Index]=
    MINX(
        FILTER(ALL('Table'),'Table'[ticket_id]=EARLIER('Table'[ticket_id])&&'Table'[Service Team]=EARLIER('Table'[Service Team])),[Index]),1,0)
    Final Rank = 
    SUMX(
        FILTER(ALL('Table'),
        'Table'[ticket_id]=EARLIER('Table'[ticket_id])&&'Table'[Index]<=EARLIER('Table'[Index])),[Column])

     

     

                                                                                                                                                             

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

4 Replies

  • Hi,

    I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.

    It is for creating a new column.

     

     

     

    Team Rank CC =
    VAR _t =
        GROUPBY (
            Data,
            Data[ticket_id],
            Data[Service Team],
            "@startdatetime", MAXX ( CURRENTGROUP (), Data[start_date_time_local] )
        )
    RETURN
        RANK (
            SKIP,
            _t,
            ORDERBY ( [@startdatetime], ASC, Data[Service Team], ASC ),
            PARTITIONBY ( Data[ticket_id] )
        )
    

     

  • jimminy's avatar
    jimminy
    Frequent Visitor

    Thanks for taking the time, and this solution is what I'm looking for. Although when I use the above code I don't get exactly the right result. For example the result for a ticket using this code is shown below:

    ticket_id
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    start_date_time_local
    23/05/2023 11.42.58
    23/05/2023 11.46.28
    23/05/2023 11.47.14
    23/05/2023 11.47.45
    24/05/2023 14.46.47
    24/05/2023 15.16.44
    24/05/2023 15.16.56
    25/05/2023 14.01.43
    07/06/2023 05.19.03
    03/07/2023 15.59.57
    04/07/2023 12.25.03
    04/07/2023 12.25.33
    04/07/2023 14.47.46
    service_and_support_team_id
    Team1
    Team1
    Team1
    Team2
    Team2
     
    Team2
    Team2
    Team2
    Team1
    Team1
    Team2
    Team2
    Team Rank CC
    2
    2
    2
    3
    3
    1
    3
    3
    3
    2
    2
    3
    3

     

    Where as the result I am looking for would be this:

    ticket_id
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    7777777
    start_date_time_local
    23/05/2023 11.42.58
    23/05/2023 11.46.28
    23/05/2023 11.47.14
    23/05/2023 11.47.45
    24/05/2023 14.46.47
    24/05/2023 15.16.44
    24/05/2023 15.16.56
    25/05/2023 14.01.43
    07/06/2023 05.19.03
    03/07/2023 15.59.57
    04/07/2023 12.25.03
    04/07/2023 12.25.33
    04/07/2023 14.47.46
    service_and_support_team_id
    Team1
    Team1
    Team1
    Team2
    Team2
     
    Team2
    Team2
    Team2
    Team1
    Team1
    Team2
    Team2
    Team Rank CC
    1
    1
    1
    2
    2
    3
    4
    4
    4
    5
    5
    6
    6
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi jimminy ,

     

    Here's my solution.

    1.Create an index column in Power Query.

     

    2.Go back to Power BI Desktop and create two calculated columns.

    Column = 
    IF(
        'Table'[Index]=
    MINX(
        FILTER(ALL('Table'),'Table'[ticket_id]=EARLIER('Table'[ticket_id])&&'Table'[Service Team]=EARLIER('Table'[Service Team])),[Index]),1,0)
    Final Rank = 
    SUMX(
        FILTER(ALL('Table'),
        'Table'[ticket_id]=EARLIER('Table'[ticket_id])&&'Table'[Index]<=EARLIER('Table'[Index])),[Column])

     

     

                                                                                                                                                             

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

    • jimminy's avatar
      jimminy
      Frequent Visitor

      Hi, Thanks for the solution. However I run into an issue when the team repeats e.g. if it returns to team 1 after going to team 2. Do you know how to solve this?