Forum Discussion
Calculate difference between two dates excluding weekends using a date table?
- 5 years ago
Anonymous
You could use this code:
WorkingDaysDiff = VAR BlankDate = ISBLANK(Weekday5[Start Date]) || ISBLANK(Weekday5[End Date]) RETURN IF(BlankDate, 0, CALCULATE( COUNTROWS ( 'Calendar'), DATESBETWEEN ( 'Calendar'[Date], 'Weekday5'[Start Date], 'Weekday5'[End Date] ), 'Calendar'["IsWorkingDay"] = TRUE, ALL ( 'Weekday5' ) ) )And you'll get this result:
Hope it helps.
Regards,
Fernando
P.S. If it helped, please consider liking the post and marking it as an answer.
Hi Anonymous ,
It's time for you to start your long journey to meet the great SQLBI guys here.
Use this code in a new column in your table:
WorkingDaysDiff =
CALCULATE(
COUNTROWS ( 'Calendar'),
DATESBETWEEN ( 'Calendar'[Date], 'Weekday'[Start Date], 'Weekday'[End Date] ),
'Calendar'["IsWorkingDay"] = TRUE,
ALL ( 'Weekday' )
)You get this:
Regards,
Fernando
calerof Thank you. This solution worked, but I see one error. If there is no date for either the Weekday[Start Date] or Weekday[End Date], such as if we have no data for the order dates, it sets WorkingDaysDiff to the maximum possible days in the calendar. Since mine goes back 2 years, I have thousands of rows set to 783 now.
How can I default them to 0 if there's no data present?
- calerof5 years agoImpactful Individual
Anonymous
You could use this code:
WorkingDaysDiff = VAR BlankDate = ISBLANK(Weekday5[Start Date]) || ISBLANK(Weekday5[End Date]) RETURN IF(BlankDate, 0, CALCULATE( COUNTROWS ( 'Calendar'), DATESBETWEEN ( 'Calendar'[Date], 'Weekday5'[Start Date], 'Weekday5'[End Date] ), 'Calendar'["IsWorkingDay"] = TRUE, ALL ( 'Weekday5' ) ) )And you'll get this result:
Hope it helps.
Regards,
Fernando
P.S. If it helped, please consider liking the post and marking it as an answer.