Counting events

bertilak

New Member
Joined
Jun 5, 2019
Messages
17
I would like help with the following ...

Let's say events occur regularly, say every 90 minutes, starting at midnight. Given two timestamps, I want to calculate how many times the event occurred from time 1 to time 2.

Some examples:

00:05 - 01:25 (1 hr 20 min): The event did not occur at all.
00:05 - 01:45 (1 hr 40 min): The event occurred once at 01:30 am
01:25 - 03:05 (also 1 hr 40 min): The event occurred twice at 01:30 and 03:00)

Note that the same elapsed time (e..g. 1 hr 40 min) can encompass either 1 or two events.

Any ideas?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If you can make a column of the times for your events, you could use the formula shown here:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
Book1
ABCDEFG
1StartEndOcurrencesStart TimeInterval
200:0501:25000:0001:30:00
300:0501:45101:30
401:2503:05203:00
503:0004:40204:30
606:0507:45106:00
707:30
809:00
910:30
1012:00
1113:30
1215:00
1316:30
1418:00
1519:30
1621:00
1722:30
Sheet1
Cell Formulas
RangeFormula
C2=COUNTIF($F$2:$F$17,">=" &A2)-COUNTIF($F$2:$F$17,">="&B2)
F3=F2+$G$2


I don't know how you would do it without the range of event times though - good luck!
[/FONT]
 
Upvote 0
Here is my current attempt. It simply counts time spans ignoring the edge cases I gave examples of above.

If the interval is 90 minutes that means there are 16 intervals in a day.

Timestamps are recorded in this format: 6/5/2019 9:39:37 AM

I subtract two timestamps (giving the number of days as a decimal number), multiply by 16 and keep only the integer value. Something like this: INT((T2-T1)*16)

That sorta works, even for crossing a day boundary or spanning multiple days, but ignores those edge cases. I was hoping to get clever, maybe by adjusting T1 and T2 in some way or adjusting the final result based on where T1 and T2 are in relation to the 90 minute boundaries.

Next I want to be able to set the start time so it isn't exactly midnight. That shouldn't be too hard once I have the above working, but one step at a time.
 
Upvote 0
P.S. I do have a table giving the time of each 90 minute period. COUNTIF might help but then I would need to work in the number of days (times 16) for long periods (multiple days).
 
Upvote 0
I will give it some thought, but it'll have to wait till the morning now!
 
Upvote 0
Maybe something like this ?

Code:
=(A2-ROUNDDOWN(A1,0))/(90/1440)-MOD(A1,1)/(90/1440)

where A1 contains start date and time, A2 contains finish date and time, and 90 is the minutes interval between events ?

I haven't fully tested this, it seems to work for one or two simple cases.

Edit to add - IF this approach works, then I think it might ONLY work for cases where the interval divides exactly into a day by a whole number, so that events always start at midnight.
If you have intervals of odd numbers that don't divide neatly into 1440 - like 57 as a random example - so that each day the first interval is at a different time, then I think you will need a different approach.
 
Last edited:
Upvote 0
Wow Gerald, that maths is way out of my league! but sticking with my formula, counting events in a table, try this:
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]=COUNTIF($I$2:$I$17,">="&MOD(A3,1))-COUNTIF($I$2:$I$17,">="&VALUE("23:59:59"))+COUNT($I$2:$I$17)-COUNTIF($I$2:$I$17,">"&MOD(B3,1))+(INT(B3-A3)-1)*16[/FONT]
This first checks to see if we span one or more dates. If not, it just looks up the number of events between start and end times, otherwise, it adds the number of events in the first day fragment, the number in the last day fragment, and 16 for any complete days in the middle.
 
Upvote 0
Lovin' the signature, #Gerald , and totally agree! Pleased to say I knew what it would do before I ran it.
 
Upvote 0
Gerald and ClaireS. Thanks to the ideas you both posted I think I have a working solution that is quite straightforward.

The general ideas I used were:
  1. Instead of 1440/90 I used the number of intervals per day. This is what is actually configured into the device I am monitoring.
  2. I used INT instead of ROUNDDOWN wherever an interval count is needed.
  3. Everything starts on an interval boundary, which helps simplify things considerably. This is OK until the occasional power failure which will restart things off-boundary in which case I need to do a reset on an interval boundary (not necessarily midnight).
This results in the formula looking like this
INT(A2*J2)-INT(A1*J2)​
Where the number of intervals for the day is in column J. Column A is the timestamp (day and hour).

P.S. For some reason this board is NOT notifying me when there is a post so it took me some time to realize you both had made some useful posts.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top