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
Chuck1892
Regular Visitor

Calculate Column: How many occurences

Hi guys,

I'm racking my brains over a problem and I'm not getting anywhere. Even with the search I can't find anything that helps me.

 

I have a table with bookings of participants for an event, where each row represents a booking by a user (id) with the information when he booked, which event and whether he participated or not.
A user can have attended several times. That means his ID can appear several times.

 

e.g. given

user_ideventdateparticipated
1A01.01.2022   t
2A

02.01.2022

   f

3B02.01.2022   t
1X05.01.2022   t
4X06.01.2022   t
3Y08.01.2022   f
1Z10.01.2022   t
...   


In order to analyse how the user behaves with the n-th booking, I tried to add a calculated column in which it should be stated how many times the user booked this booking (row). (whether it was the first booking, second, third etc.).
I had tried to number a subset (of the respective ID) via RANKX, but I couldn't manage to get this number of the rank into the output table respectively I am not yet good enough with DAX to create such a nested function.

 

What i try to achieve

user_ideventdateparticipatedn-th occurence
1A01.01.2022   t1
2A

02.01.2022

   f

1

3B02.01.2022   t1
1X05.01.2022   t2
4X06.01.2022   t1
3Y08.01.2022   f2
1Z10.01.2022   t3
...    

 

 

Can anyone think of something and kindly help me?

 

Greetings!

 

1 ACCEPTED SOLUTION
rsbin
Super User
Super User

@Chuck1892 ,

Using the RANKX function should get you what you want:

N-th Occurrence = 
VAR _user_id = Participants[user_id]
VAR _Result = RANKX( FILTER('Participants',
                       Participants[user_id] = _user_id ),
                        Participants[date], ,ASC )
RETURN
   _Result

user_ideventdateparticipatedN-th Occurrence

1 A Saturday, January 1, 2022 t 1
2 A Tuesday, February 1, 2022 f 1
3 B Tuesday, February 1, 2022 t 1
1 X Sunday, May 1, 2022 t 2
4 X Wednesday, June 1, 2022 t 1
3 Y Monday, August 1, 2022 f 2
1 Z Saturday, October 1, 2022 t 3

This link explains it well:

https://www.sqlbi.com/articles/introducing-rankx-in-dax/

Regards,

View solution in original post

9 REPLIES 9
rsbin
Super User
Super User

@Chuck1892 ,

Using the RANKX function should get you what you want:

N-th Occurrence = 
VAR _user_id = Participants[user_id]
VAR _Result = RANKX( FILTER('Participants',
                       Participants[user_id] = _user_id ),
                        Participants[date], ,ASC )
RETURN
   _Result

user_ideventdateparticipatedN-th Occurrence

1 A Saturday, January 1, 2022 t 1
2 A Tuesday, February 1, 2022 f 1
3 B Tuesday, February 1, 2022 t 1
1 X Sunday, May 1, 2022 t 2
4 X Wednesday, June 1, 2022 t 1
3 Y Monday, August 1, 2022 f 2
1 Z Saturday, October 1, 2022 t 3

This link explains it well:

https://www.sqlbi.com/articles/introducing-rankx-in-dax/

Regards,

Thank you very much! 🙂

 

Your solution is exactly what I had in mind, but I switched the position of RANKX and FILTER (For whatever reason).

@Chuck1892 ,

My pleasure.  Glad to hear it!

lg1551
Resolver II
Resolver II

Sounds like you just want to COUNTROWS of how many times they particpated? 

Would be COUNTROWS by user ID where 'ParticpatedN-TH' = "T" as your filter.

Unfortunately not.
I would like to determine per line how many bookings the user made. In other words, whether it was the first booking, the second booking, the tenth booking, etc.
I am not interested in the total number of bookings of the user.

 

Thanks for the help anyway! 🙂

serpiva64
Super User
Super User

Hi,

can you post some more complete data because in the e.g. given there are not sufficient data for calculating the result 

I don't have much more than this information. 😅
Theoretically, it should work via the combination of user_id and date.day.

 

Chuck1892_0-1665754407189.png

 

//Edit: As a picture, an exemplary excerpt of the data I have. Due to the high number of bookings, it was not possible for me to quickly create a screenshot where a user_id is present twice.  😅

 

Dinesh_Suranga
Continued Contributor
Continued Contributor

@Chuck1892 

Hi,

Do you want to do this in DAX or Power query?

Thank you

Hello,

Yes, I just saw that I should have posted the thread in the DAX section.

I tried to implement it with DAX.

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.