AOB
Well-known Member
- Joined
- Dec 15, 2010
- Messages
- 667
- Office Version
- 365
- 2016
- 2013
- Platform
- Windows
As with all good problems, straightforward to define, not so straightforward to resolve...
I have a table of timestamps relating to a particular "widget" - start (date)times and end (date)times. I need to calculate the total duration per widget but taking into account that some records for a particular widget may overlap, and others may have gaps between them. Easiest to explain with an example...
Basic table shows each start and end time, with the Individual Duration easily calculated :
Obviously one can sum the individual durations for each record, per widget, using :
But that's incorrect because it overlooks the fact that there are overlaps between some of the records and they are being tuple-counted?
Obviously one can also just take the difference between the earliest start time and the latest end time, per widget, using :
But that's also incorrect because it overlooks the fact that there are gaps (e.g. between 18:00 and 22:00) and they are being included when they shouldn't be?
The correct answer should be 9 hours (8 hours from 10:00 to 18:00 with various overlaps, plus 1 hour from 22:00 to 23:00)
So the challenge becomes, is it possible to construct a formula that can perform this calculation, preferably as a single column (although a reasonable finite number of helper columns would be acceptable) and preferably without relying on the data being sorted and ordered in a particular way first? Bear in mind the timestamps may cross over multiple dates (i.e. the start and end time for a particular row may not necessarily share the same day) and may be more granular than just hours (i.e. minutes and seconds) And there may be multiple widgets in the table (some may only appear once, others may appear any arbitrary number of times)
I reckon I could probably knock up a UDF in VBA to achieve this but would prefer to try to get it with a native formula first?
Thanks in advance for any suggestions or tips!
AOB
I have a table of timestamps relating to a particular "widget" - start (date)times and end (date)times. I need to calculate the total duration per widget but taking into account that some records for a particular widget may overlap, and others may have gaps between them. Easiest to explain with an example...
Basic table shows each start and end time, with the Individual Duration easily calculated :
Excel Formula:
=[@[End Time]] - [@[Start Time]]
WidgetID | Start Time | End Time | Individual Duration |
---|---|---|---|
ABCD | 06 Feb 2024 10:00 | 06 Feb 2024 14:00 | 04:00 |
ABCD | 06 Feb 2024 16:00 | 06 Feb 2024 18:00 | 02:00 |
ABCD | 06 Feb 2024 13:00 | 06 Feb 2024 17:00 | 04:00 |
ABCD | 06 Feb 2024 22:00 | 06 Feb 2024 23:00 | 01:00 |
Obviously one can sum the individual durations for each record, per widget, using :
Excel Formula:
=SUMIFS([Individual Duration],[WidgetID],[@WidgetID])
WidgetID | Start Time | End Time | Individual Duration | Sum of Individual Durations |
---|---|---|---|---|
ABCD | 06 Feb 2024 10:00 | 06 Feb 2024 14:00 | 04:00 | 11:00 |
ABCD | 06 Feb 2024 16:00 | 06 Feb 2024 18:00 | 02:00 | 11:00 |
ABCD | 06 Feb 2024 13:00 | 06 Feb 2024 17:00 | 04:00 | 11:00 |
ABCD | 06 Feb 2024 22:00 | 06 Feb 2024 23:00 | 01:00 | 11:00 |
But that's incorrect because it overlooks the fact that there are overlaps between some of the records and they are being tuple-counted?
Obviously one can also just take the difference between the earliest start time and the latest end time, per widget, using :
Excel Formula:
=MAXIFS([End Time],[WidgetID],[@WidgetID])-MINIFS([Start Time],[WidgetID],[@WidgetID])
WidgetID | Start Time | End Time | Individual Duration | Max - Min Duration |
---|---|---|---|---|
ABCD | 06 Feb 2024 10:00 | 06 Feb 2024 14:00 | 04:00 | 13:00 |
ABCD | 06 Feb 2024 16:00 | 06 Feb 2024 18:00 | 02:00 | 13:00 |
ABCD | 06 Feb 2024 13:00 | 06 Feb 2024 17:00 | 04:00 | 13:00 |
ABCD | 06 Feb 2024 22:00 | 06 Feb 2024 23:00 | 01:00 | 13:00 |
But that's also incorrect because it overlooks the fact that there are gaps (e.g. between 18:00 and 22:00) and they are being included when they shouldn't be?
The correct answer should be 9 hours (8 hours from 10:00 to 18:00 with various overlaps, plus 1 hour from 22:00 to 23:00)
WidgetID | Start Time | End Time | Individual Duration | Actual Total Duration |
---|---|---|---|---|
ABCD | 06 Feb 2024 10:00 | 06 Feb 2024 14:00 | 04:00 | 09:00 |
ABCD | 06 Feb 2024 16:00 | 06 Feb 2024 18:00 | 02:00 | 09:00 |
ABCD | 06 Feb 2024 13:00 | 06 Feb 2024 17:00 | 04:00 | 09:00 |
ABCD | 06 Feb 2024 22:00 | 06 Feb 2024 23:00 | 01:00 | 09:00 |
So the challenge becomes, is it possible to construct a formula that can perform this calculation, preferably as a single column (although a reasonable finite number of helper columns would be acceptable) and preferably without relying on the data being sorted and ordered in a particular way first? Bear in mind the timestamps may cross over multiple dates (i.e. the start and end time for a particular row may not necessarily share the same day) and may be more granular than just hours (i.e. minutes and seconds) And there may be multiple widgets in the table (some may only appear once, others may appear any arbitrary number of times)
I reckon I could probably knock up a UDF in VBA to achieve this but would prefer to try to get it with a native formula first?
Thanks in advance for any suggestions or tips!
AOB