Hi Community,
I got a small problem at my hands and most of the relating discussions solve something slightly different and with very limited expression skills I'm at a loss here...I hope the example will also help others in the future.
Problem:
Calculate the duration of overlapping time frames. Eg. i got a random time frame (date+time) 'a' and a time frame 'b'. I've managed to determine IF they overlap (thanks @david_fetters for the formula below), but I can't work out how to calculate how long for they overlap.
#Base Formula from david_fetters to determine if the overlap:
IF (DateTimeDiff([Begin2],[Begin1],"seconds") > 0 &&
DateTimeDiff([Begin2],[End1],"seconds") < 0) ||
(DateTimeDiff([End2],[Begin1],"seconds") > 0 &&
DateTimeDiff([End2],[End1],"seconds") < 0) THEN 1 ELSE 0 ENDIF
#My formula 1 to determine overlap:
IF (DateTimeDiff([start_date_time_b],[start_date_time_a],"seconds") > 0 &&
DateTimeDiff([start_date_time_b],[end_date_time_a],"seconds") < 0) ||
(DateTimeDiff([end_date_time_b],[start_date_time_a],"seconds") > 0 &&
DateTimeDiff([end_date_time_b],[end_date_time_a],"seconds") < 0) THEN 1 ELSE 0 ENDIF
#My formula 2 to determine overlap (a swapped with b):
IF (DateTimeDiff([start_date_time_a],[start_date_time_b],"seconds") > 0 &&
DateTimeDiff([start_date_time_a],[end_date_time_b],"seconds") < 0) ||
(DateTimeDiff([end_date_time_a],[start_date_time_b],"seconds") > 0 &&
DateTimeDiff([end_date_time_a],[end_date_time_b],"seconds") < 0) THEN 1 ELSE 0 ENDIF
Start/End dates already formatted to proper datetime 'dd/MM/yyy hh:mm:ss' - Example attached.
start_date_time_a | start_date_time_b | end_date_time_a | end_date_time_b
2021-01-14 11:55:49 | 2021-01-14 11:55:59 | 2021-01-14 11:56:52 | 2021-01-14 11:56:10
So for this example above, the two time frames would overlap between 11:55:59 until 11:56:10 and I would like to have an output like "11" (seconds). I should be simple maths to subtract start/end times, but I don't know how to write that in expression format. Another issue is that either 'a' or 'b' can start first or end first hence i had to duplicate the formula above and swap a with b to capture all possible overlaps.
Hope this makes sense. Looking forward to hear your thoughts and solutions.
Cheers,
Thomas