The Problem
The need to test if two events are conflicting or not has come up several times, and every time I have to rack my brain to remember or re-invent the formula that looks something like this:
([event 1].[start time] > [event 2].[start time] AND [event 1].[start time] < [event 2].[end time]) OR ([event 2].[start time] > [event 1].[start time] AND [event 2].[start time] < [event 1].[end time])
The first problem is, with as much logical brain power as it took to write that, I’m still not sure if it’s correct.
The second problem is that it becomes even longer and more unwieldy if any of those start/end times are calculated values rather than named variables. Eg, if my end times are based on duration (ie, [event].[start time] + [event].[duration]
) then the above formula may turn into this monster:
([event 1].[start time] > [event 2].[start time] AND [event 1].[start time] < [event 2].[start time] + [event 2].[duration]) OR ([event 2].[start time] > [event 1].[start time] AND [event 2].[start time] < [event 1].[start time] + [event 1].[duration])
Possible Solution
So I would suggest a simple solution:
IsOverlapping([event 1].[start time], [event 1].[end time]), [event 2].[start time], [event 2].[end time])
Which would return a true
or false
for me, just like the monster formula.
Even Better Solution
And what would be even better (in my mind) would be to have a whole new data type for DateRange (with the associated column type, and formulas to match). And then my overlapping formula would become almost as simple as it is in my head:
IsOverlapping([event 1].[daterange], [event 2].[daterange])
And then in that case, we might access the underlying dates with [event 1].[daterange].Start()
or just simply use Min()
and Max()
. And it would be nice for any existing formula that takes two dates as its arguments to also be able to take DateRange values.
Thoughts?