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
Flawn
Helper I
Helper I

Average difference between two dates based on two other columns

Hello Everyone, 


I asked this question a couple weeks back but didn't get a satisfactory answer, so I will post it one more time in hopes of getting one. 

We have three columns relevent to our problem: 1. A Client ID column. 2. An appointment classification column, which for our purposes has two relevent buckets; 'service requests' and 'other'. 3. An appointment creation date column.

We need to calculate the average difference in days between a service request appointment and the next  'other' appointment a client attends.

I am finding it very difficult to construct a formula that can keep track of the clients as well as identifying which is the 'next' appointment after the service request without any other identifiers (which, unfortunately, I am unable to add due to the way their database has been constructed). Perhaps the solution is simpler than my brain is making it; so i'll pass it on to other, more capable minds! The dataset is very large and confidential so i've constructed a simplified example of the data in question below and 'color coded' the pairings that should be matched and have their differences calculated for the average to better illustrate the objective.

Client IDAppointment Category Date (dd/mm/yyyy)
1Service Request01/06/2020
1aaa09/06/2020
1bbb07/06/2020
1ccc05/06/2020
2ddd02/06/2020
2Service Request06/06/2020
2eee11/06/2020
2fff15/06/2020
3Service Request4/06/2020
3ggg9/06/2020
3Service Request14/06/2020
3hhh19/06/2020


Of course, I am happy to elaborate if i was unclear on anything or if I missed some edge case that could effect the answer!

Thank you all so much in advance for your time!

Regards,
-Flawn

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Flawn , Create a new column like this and take the average


new column =
var _max = maxx(filter(table, [Client ID] =earlier([Client ID]) && [Date] <earlier([Date])),[date])
return
datediff( maxx(filter(table, [Client ID] =earlier([Client ID]) && [Date]= _date && [Appointment Category] = "Service Request"),[date]),[date], day)

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@Flawn , Create a new column like this and take the average


new column =
var _max = maxx(filter(table, [Client ID] =earlier([Client ID]) && [Date] <earlier([Date])),[date])
return
datediff( maxx(filter(table, [Client ID] =earlier([Client ID]) && [Date]= _date && [Appointment Category] = "Service Request"),[date]),[date], day)

Hello Amitchandak,

Sorry for the delayed response. The results are close to what we want, but not quite there: the calculated column produced a lot of negative values. The objective is to only calculate the average difference in days between service requests and the next non-service-request appointments for that client - so negative values shouldn't be occuring (as the next appointment to occur can't be before the date of the service request). 

We need to make some sort of adjustment to ensure that we are only calculating the difference in days for appointments that are occuring after Service Requests, not before.

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