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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Microsoft Employee
Microsoft 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...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.