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
aflintdepm
Helper III
Helper III

Calculations between rows

I have a table of data that contains a list of appointments with clients.  Some rows include past appointments while other rows contain future appointments.  Here's an example:

 

Client IDClient NameAppointment DateAppointment StatusAppointment Type
1234John Doe1/1/24CompleteRoutine
1234John Doe5/1/24PendingRoutine
5678Jane Smith3/1/24CompleteRoutine

 

As I'm writing this in April 2024, the first appointment is in the past, the second one is in the future

 

I would like to calculate the time between the 2 appointments to make sure that we are having client contact on the appropriate cadence. 

 

The third row represents a possible error scenario where the client had a visit in the past, but has no record in the table for the future, so the overall formula would ideally have an IFERROR function to resolve this.

 

Thank you

1 ACCEPTED SOLUTION
v-cgao-msft
Community Support
Community Support

Hi @aflintdepm ,

Please try this calculated column:

Time Between Appointments = 
VAR __today =
    TODAY ()
VAR __cur_date = 'Table'[Appointment Date]
VAR __prev_date =
    CALCULATE (
        MAX ( 'Table'[Appointment Date] ),
        'Table'[Appointment Date] < __cur_date
            && 'Table'[Appointment Date] < __today,
        ALLEXCEPT ( 'Table', 'Table'[Client ID] )
    )
VAR __result =
    DATEDIFF ( __prev_date, __cur_date, DAY )
RETURN

vcgaomsft_0-1713234971576.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

View solution in original post

2 REPLIES 2
v-cgao-msft
Community Support
Community Support

Hi @aflintdepm ,

Please try this calculated column:

Time Between Appointments = 
VAR __today =
    TODAY ()
VAR __cur_date = 'Table'[Appointment Date]
VAR __prev_date =
    CALCULATE (
        MAX ( 'Table'[Appointment Date] ),
        'Table'[Appointment Date] < __cur_date
            && 'Table'[Appointment Date] < __today,
        ALLEXCEPT ( 'Table', 'Table'[Client ID] )
    )
VAR __result =
    DATEDIFF ( __prev_date, __cur_date, DAY )
RETURN

vcgaomsft_0-1713234971576.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

ryan_mayu
Super User
Super User

not sure if this is what you want. 

 

Column =
VAR _last=maxx(FILTER('Table','Table'[Client ID]=EARLIER('Table'[Client ID])&&'Table'[Appointment Date]<EARLIER('Table'[Appointment Date])),'Table'[Appointment Date])
return DATEDIFF(_last,'Table'[Appointment Date],DAY)
 
11.PNG
 

i think your actual data will be more complicated than the sample data you provided

 

what if the dates are all in the past? what if we have three dates, two in the future and one in the past?

 

if the solution does not meet your requirements. pls update your sample data and the expected output and make sure your sample data includes the different scenarios as many as possible. 

 

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.