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
Anonymous
Not applicable

Filter the top two dates for each client date of service

I have a table where there is a client ID (1234,2345,3456, etc.). Each client has multiple dates of service. I want to filter out the top two dates of service for each and then calculate the datediff between the two dates that remain. The client_id and service_date are the column names. I've tried indexing, a calculated column, and pulling out my hair. I'm down to my last strand. Please help. Some clients have 5 dates of service while others only have 2. Some have just one and those need to be filtered out completely. 

 

The top two would be the oldest two. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Just to give help you a bit more, you can then get the first and second date using the following measure

 

First Date = MAXX(FILTER('Table1', [Date Rank] = 1), 'Table1'[Date].[Date])
Second Date = MAXX(FILTER('Table1', [Date Rank] = 2), 'Table1'[Date].[Date])

 

To get the difference you would then simply use the following and put it against UserId in a table

 

Date Diff = DATEDIFF([FirstDate], [Second Date], DAY)

 

Hopefully this helps get you close enough!

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Any updates on this? Were are you able to solve this problem?

Ashish_Mathur
Super User
Super User

Hi,

Please share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Could you use the rankx function and filter on <= 2?

Anonymous
Not applicable

I was trying to figure that out, because the filtering I though worked, didn't. How would I rankx for each client's dates of service and not a rankx of all dates of service?

Anonymous
Not applicable

Assuming you have a table with two columns, UserId and Date, the following calculated column ranks each date by UserId

 

Date Rank = RANKX(CALCULATETABLE('Table1', ALLEXCEPT(Table1, Table1[UserId])), 'Table1'[Date], , ASC)

 

Anonymous
Not applicable

Just to give help you a bit more, you can then get the first and second date using the following measure

 

First Date = MAXX(FILTER('Table1', [Date Rank] = 1), 'Table1'[Date].[Date])
Second Date = MAXX(FILTER('Table1', [Date Rank] = 2), 'Table1'[Date].[Date])

 

To get the difference you would then simply use the following and put it against UserId in a table

 

Date Diff = DATEDIFF([FirstDate], [Second Date], DAY)

 

Hopefully this helps get you close enough!

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.