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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
EduBubicz
Frequent Visitor

Get Rank based on Event and grouped by ID

Hey guys,

I really need some help with DAX;

 

I have a database and I want to set a user [Rank] that would be reset based on specific [Event] and [Event Date], for each [ID].

Below are some examples of the expected results. For each "ENV" event, the rank counting is reset.

Any Ideas?

thanks in advance

 

 

IDEventEvent DateUserRank (Expected Result)
8189576ENV29/10/2020 19:20  
8189576REVI30/10/2020 08:02alema1
8189576ENV03/11/2020 11:43  
8189576AP03/11/2020 11:44alema1
8189576AP03/11/2020 14:03jflor2
7940462ENV14/09/2020 14:39  
7940462AP17/09/2020 08:50tcosta1
7940462REVI21/09/2020 11:06msantos2
7940462ENV24/09/2020 13:45  
7940462AP28/09/2020 10:39rjunior1
7940462AP29/09/2020 08:35slima2
7919385ENV01/09/2020 14:56  
7919385AP01/09/2020 16:26tjung1
7919385REVI04/10/2020 14:39tjung2
7919385ENV08/10/2020 09:59  
7919385AP03/11/2020 16:11rsantos1
7919385AP06/11/2020 11:09ejunior2
7919359ENV03/09/2020 15:01  
7919359REVI09/09/2020 09:53rcjunior1
7919359ENV09/09/2020 15:44  
7919359REVI09/09/2020 16:14rcjunior1
7919359ENV10/09/2020 09:00  
7919359AP10/09/2020 09:15rcjunior1
7919359AP11/09/2020 07:05epombo2
7919359AP14/09/2020 09:13esouza3
7919411ENV01/09/2020 11:17  
7919411AP01/09/2020 11:24csrego1
7919411AP08/09/2020 14:14csrego2
7919411AP16/09/2020 15:43eneto3

 

 

3 REPLIES 3
mahoneypat
Employee
Employee

Please try this calculated column expression that seems to get your desired results.

 

Rank Measure =
VAR vThisDT = Events[Event Date]
VAR vThisID = Events[ID]
VAR vLastEvent =
    CALCULATE (
        MAX ( Events[Event Date] ),
        Events[Event] = "ENV",
        Events[Event Date] < vThisDT,
        Events[ID] = vThisID,
        ALL ( Events )
    )
VAR vRowsBefore =
    CALCULATE (
        COUNTROWS ( Events ),
        ALL ( Events ),
        Events[Event Date] <= vThisDT,
        Events[Event Date] > vLastEvent,
        Events[ID] = vThisID,
        ALL ( Events )
    )
RETURN
    IF (
        Events[Event] = "ENV",
        BLANK (),
        vRowsBefore
    )

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


EduBubicz
Frequent Visitor

Thansks for your answer @amitchandak , but I couldnt get the results as expected.
Here is the table with the results using your first suggestion (for the second, the results were the same).

As you can see, the rank didn't reset for each "ENV" event, and isn't blank when there are no users.

Any other suggestions? 
thanks in advance !


1.PNG

amitchandak
Super User
Super User

@EduBubicz , Create a new column like

rankx(filter(table, [ID] = earlier([ID]) && not(isblank([User]))), [Event Date],,desc,dense)

or

if(not(isblank([User])), rankx(filter(table, [ID] = earlier([ID]) && not(isblank([User]))), [Event Date],,desc,dense) , blank())

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/3...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors