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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
vincenardo
Helper I
Helper I

Calculate Differance Between two dates, grouping by another field in data set.

In this dataset, I have rows of data that share the same 'PhaseID'. I want to calculate the differance (in days) between the first 'ScheduledStartDate' and the last 'ScheduledCompleteDate' for each row that shares the same 'PhaseID'.

 

For Example, for PhaseID 89578 the first ScheduledStartDate = 10/22/21 and the last ScheduledCompleteDate = 11/8/2021 so the differance would be 18 days. I also want to exclude weekends in the calculation.

 

Help is appreciated!

 

vincenardo_0-1634652956179.png

 

1 ACCEPTED SOLUTION
v-xiaotang
Community Support
Community Support

Hi @vincenardo 

You can try this measure to get diff between ScheduledStartDate & ScheduledCompleteDate.

diff = 
    var _min=CALCULATE(MIN('Table'[ScheduledStartDate]),ALLEXCEPT('Table','Table'[PhaseID]))
    var _max=CALCULATE(MAX('Table'[ScheduledCompleteDate]),ALLEXCEPT('Table','Table'[PhaseID]))
return 
    DATEDIFF(_min,_max,DAY)+1

and if you want to exclude weekends in the calculation, then add a calendar table, and use the measure bellow

diff = 
    var _min=CALCULATE(MIN('Table'[ScheduledStartDate]),ALLEXCEPT('Table','Table'[PhaseID]))
    var _max=CALCULATE(MAX('Table'[ScheduledCompleteDate]),ALLEXCEPT('Table','Table'[PhaseID]))
return 
    CALCULATE(COUNTROWS('Calendar'),DATESBETWEEN('Calendar'[Date],_min,_max),'Calendar'[Isweekday]<>7&&'Calendar'[Isweekday]<>1)

 result

vxiaotang_0-1634897882753.png

 

 

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-xiaotang
Community Support
Community Support

Hi @vincenardo 

You can try this measure to get diff between ScheduledStartDate & ScheduledCompleteDate.

diff = 
    var _min=CALCULATE(MIN('Table'[ScheduledStartDate]),ALLEXCEPT('Table','Table'[PhaseID]))
    var _max=CALCULATE(MAX('Table'[ScheduledCompleteDate]),ALLEXCEPT('Table','Table'[PhaseID]))
return 
    DATEDIFF(_min,_max,DAY)+1

and if you want to exclude weekends in the calculation, then add a calendar table, and use the measure bellow

diff = 
    var _min=CALCULATE(MIN('Table'[ScheduledStartDate]),ALLEXCEPT('Table','Table'[PhaseID]))
    var _max=CALCULATE(MAX('Table'[ScheduledCompleteDate]),ALLEXCEPT('Table','Table'[PhaseID]))
return 
    CALCULATE(COUNTROWS('Calendar'),DATESBETWEEN('Calendar'[Date],_min,_max),'Calendar'[Isweekday]<>7&&'Calendar'[Isweekday]<>1)

 result

vxiaotang_0-1634897882753.png

 

 

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@vincenardo , You can create a new column like

 

Work Day = COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(Table[ShceduleStartDate],Table[ShceduleCompleted Date]),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1))

 

Business Days/ Workdays, with or without date table: https://youtu.be/Qv4wT8_P-AA

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

Thanks. I have an error becuase I also have records with blank dates in the date fields. What conditional statement could  I add?

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.