Forum Discussion
Date Diff in Hours : Minutes excluding weekends..
- 7 years ago
Hi Anonymous,
I've implemented your additional requirements for handling hours at weekends. You need some new columns:
First of all, you need to find a new Start timestamp. If it is Saturday or Sunday, just use the start of tomorrow.
NextPossibleStart = IF ( WEEKDAY ( Table1[Start]; 2 ) >= 6; DATEADD ( Table1[Start].[Date]; 1; DAY ); Table1[Start] )Then you need to find the start of a day if the end timestamps is Saturday or Sunday.
PreviousPossibleEnd = VAR IsWeekend = WEEKDAY ( Table1[End]; 2 ) >= 6 VAR NewEndDate = IF ( IsWeekend; Table1[End].[Date]; Table1[End] ) VAR IsNewEndDateBeforeStartDate = NewEndDate < Table1[NextPossibleStart] RETURN IF ( IsNewEndDateBeforeStartDate; Table1[NextPossibleStart]; NewEndDate )Use these 2 new columns in finding all weekend days between 2 dates.
CountOfWeekdays = VAR tableOfDays = CALENDAR ( Table1[NextPossibleStart]; Table1[PreviousPossibleEnd] ) VAR tableOfWeekdays = FILTER ( tableOfDays; WEEKDAY ( [Date]; 2 ) >= 6 ) VAR countOfWeekdays = COUNTROWS ( tableOfWeekdays ) RETURN IF ( ISBLANK ( countOfWeekdays ); 0; countOfWeekdays )And also in the diff:
DiffWithoutWeekends = VAR DiffInMinutes = DATEDIFF ( Table1[NextPossibleStart]; Table1[PreviousPossibleEnd]; MINUTE ) VAR DiffInHours = QUOTIENT ( DiffInMinutes; 60 ) VAR WeekendHours = 24 * Table1[CountOfWeekdays] VAR DiffInHoursWithoutWeekend = DiffInHours - WeekendHours VAR ModuloDiffInMinutes = MOD ( DiffInMinutes; 60 ) VAR Result = FORMAT ( DiffInHoursWithoutWeekend; "00" ) & ":" & FORMAT ( ModuloDiffInMinutes; "00" ) RETURN ResultSome tests:
And you can also download the PowerBI file again, I've uploaded the new version of it.
Hi Anonymous,
I've implemented your additional requirements for handling hours at weekends. You need some new columns:
First of all, you need to find a new Start timestamp. If it is Saturday or Sunday, just use the start of tomorrow.
NextPossibleStart =
IF (
WEEKDAY ( Table1[Start]; 2 ) >= 6;
DATEADD ( Table1[Start].[Date]; 1; DAY );
Table1[Start]
)Then you need to find the start of a day if the end timestamps is Saturday or Sunday.
PreviousPossibleEnd =
VAR IsWeekend =
WEEKDAY ( Table1[End]; 2 ) >= 6
VAR NewEndDate =
IF ( IsWeekend; Table1[End].[Date]; Table1[End] )
VAR IsNewEndDateBeforeStartDate = NewEndDate < Table1[NextPossibleStart]
RETURN
IF ( IsNewEndDateBeforeStartDate; Table1[NextPossibleStart]; NewEndDate )Use these 2 new columns in finding all weekend days between 2 dates.
CountOfWeekdays =
VAR tableOfDays =
CALENDAR ( Table1[NextPossibleStart]; Table1[PreviousPossibleEnd] )
VAR tableOfWeekdays =
FILTER ( tableOfDays; WEEKDAY ( [Date]; 2 ) >= 6 )
VAR countOfWeekdays =
COUNTROWS ( tableOfWeekdays )
RETURN
IF ( ISBLANK ( countOfWeekdays ); 0; countOfWeekdays )And also in the diff:
DiffWithoutWeekends =
VAR DiffInMinutes =
DATEDIFF ( Table1[NextPossibleStart]; Table1[PreviousPossibleEnd]; MINUTE )
VAR DiffInHours =
QUOTIENT ( DiffInMinutes; 60 )
VAR WeekendHours = 24 * Table1[CountOfWeekdays]
VAR DiffInHoursWithoutWeekend = DiffInHours - WeekendHours
VAR ModuloDiffInMinutes =
MOD ( DiffInMinutes; 60 )
VAR Result =
FORMAT ( DiffInHoursWithoutWeekend; "00" ) & ":"
& FORMAT ( ModuloDiffInMinutes; "00" )
RETURN
ResultSome tests:
And you can also download the PowerBI file again, I've uploaded the new version of it.
You are amazingly awesome.. Thank you so much and working perfectly..
- Jcarbo6 years agoNew Member
Hi all.
I have the need to exclude from the time difference in
Hours and Minutes Calculated here the non Office working hours. is there someone who can give me some help on how to do that?WHstart and WHend could be a variable used to calculate that