Start/End Times falling in ranges (and crossing over midnight) - excel

I have a data set with start times in column B and end times in column C. I am currently using a sumproduct formula (=SUMPRODUCT(($B2<"07:00")*($C2>="07:59"))) to determine if the start/end time falls within time ranges (i.e. 07:00 to 07:59, 08:00 to 08:59, etc.). In the sumproduct formula the times are just examples (I will fill those with times in numerical format).
I have seen the sumproduct before and it's tried and tested, but I have yet to find a formula that does all of this AND accounts for if the start time is prior to midnight but the end time is after midnight.
The data set is for an event running from start to finish, and I ultimately want to determine what time ranges that event is running in by placing a 0/1 if the event is running in any given time interval.
My only fix to date is applying the sumproduct formula and then filtering on occurrences that cross over midnight and adapting the formula accordingly.
Any help with this would be greatly appreciated. Thank you in advance for your time.

First off I don't think you need sumproduct as you are not dealing with any sort of range that need summing so you could shorten your formula to ($B2>"07:00")*($C2<="07:59"). Note I also reversed the signs so it is actually between 7:00-7:59, also the formula won't work unless the 07:00 and 07:59 are stored in a cell because as written it is comparing a number to a text string and won't return the correct value.
For your actual question you need a second part to your function
=if(end<start,(B2<end)+(B2>start),(B2>start)*(B2<=end))
If the end time is less then the start time assume it goes past midnight so your time needs to be either after the start time or before the end time. If it does not go around midnight it has to be between them both.

Consider using a half open interval [Start,End) such that your ranges are 07:00 to 08:00, 08:00 to 09:00, etc.
This has the advantage of not skipping values like 07:59:30.
It also lets you do simple subtraction to get 1 hour instead of 59 minutes for the duration of the range.
Compare against the start of the range using <= or >= (depending on how you express it), but compare against the end of the range using < or >.
For time-only ranges that span midnight, you can just test to see if the value falls after the start or before the end.
Here is a complete formula. Replace [start], [end], and [value] as necessary.
=IF([start] <= [end], AND([start] <= [value], [value] < [end]), OR([value] >= [start], [value] < [end]))

Related

Calculate if time period is contained within a start and end time

I have a problem where I am trying to calculate in Excel if any part of a provided work duty time period is contained within a user defined period which specifies the night working hours e.g. 2330-0559 or 0000-0630.
If I provide a work duty time I want and any part of the duty is within the specified period it needs to be identified. E.g. 2230-0630 duty time is within both examples above.
I can tried a few different solutions and still not got the right way solve it.
Maybe someone can help. I also know that 24:00 in excel is used for midnight at the end of the day and 00:00 is used for midnight starting the day as excel works from 0-1 as part of the day.
=OR(MOD(A5,1)>$E$2,MOD(A5,1)<$F$2)
I would work with the number of minutes as decimal and with it make a valid range from "Duty start time" and "Duty end time".
Check if you can make any sense out of this:
Here the example:
https://drive.google.com/file/d/1O1B76srlY8sYQHsV_TiZVzi-7B_18eSj/view?usp=sharing
Update: Sorry for the lack of explanation. Here I try my best to clarify how I did it.
Conversion
First of all I try to convert any time into minutes, so 00:00 is 0 minutes, 01:00 is 60 minutes, 24:00 (excel shows 00:00) is 1440 minutes, 25:00 (excel shows 01:00) is 1500 minutes.
That conversion I do with CONVERT(E2,"day", "mn"), which does convert from day to minutes.
Range Normalization and inclusion
Now every range has to be normalized, that means the "End" has to be always bigger than "Start". For 00:00 to 06:00 this works fine, but for 22:00 to 06:00 it needs to be tweaked. So if "Start" is bigger than "End" then I add 1440 mins (24 hours) to "End". That I do with IF(A6>B6, 1440,0).
You then need to see if any "Duty range" contains the "Night range". That normally is done with the formula if (DutyEnd > NightStart AND DuttyStart < NightEnd) then TRUE.
Challenge 1
That was the main concept. But then if you try to compare a range like 00:00-06:00 contained in 24:00-06-00 it does not work. And that is because the converted minutes are 0-360 and 1440-1830, they don't contain each other.
In E3 to fix this I cut down 1440 with modulo (MOD()), then MOD(1440, 1440) = 0. So even if you use values like 25:00 or 52:00, they will be cut down to the smallest amount of minutes. E.g. 25:00 (1500 mins) = 1:00 = 60 mins, 52:00 (3120 mins) = 4:00 = 240 mins
Challenge 2
We have yet another challenge, the possible comparisons are as follows:
00:00-06:00 contained in 00:00-06:00 which in minutes is 0-390 contained in 0-390
00:00-06:00 contained in 22:00-06:00 which in minutes is 0-390 contained in 1320-1830
22:00-06:00 contained in 00:00-06:00 which in minutes is 1320-1830 contained in 0-390
These last two will not match. So that is why in the "Contains" column (E.g.: E6) I compare against the "Night criteria" - 1400 and + 1400.
Hope it is a bit clear. Let me know otherwise..
UNDERSTANDING DATE AND TIME IN EXCEL
Dates in excel are stored as INTEGERS. They represent the days since 1900/01/1 with that date being 1.
Time is stored as a decimal which represents a fraction of a day. 24 hours is 1, 0.5 is 12 noon. etc.
In other words, everything to the left of the decimal is date and everything to the right is time.
JUST USING TIME AND CROSSING MIDNIGHT
This is problematic from the view point that early morning times are less than the late times of the previous day. The fact of the matter is that they are larger. In our heads we do the mental math of knowing the are the following day but we ignore the date aspect.
A quick way to rectify this is to add the date to your time. Life will become much easier with the math. You may however not want to add full dates to start and end times. WITH THE ASSUMPTION that start and end times are not more than 24 hours the simple work around is is to add 1 to the end time when the end time is less than the start time. This means its the next day.
It the example date you provided, column C was insert to CORRECT the end time. It did the check of end less than start if so add one using the following formula:
=B6+(B6<=A6)
The part in brackets is a logic check. It either evaluates to TRUE or FALSE. When excel runs a boolean (TRUE or FALSE) through a math operator (not a function like sum) it will convert TRUE to 1 and FALSE to 0.
LENGTH
Straight forward math of C minus A since C is always after you start and is the larger of the two numbers.
=C6-A6
CROSSING MIDNIGHT
Need to be a little careful in your definition of crossing midnight when a start time or end time is exactly midnight. Technically speaking you did not cross it if you start or stop on it. The difference is really < versus <= or > versus >=. I will leave that to you to sort out. For the math I used:
=AND(A6<1,C6>=1)
Though I did not use this column for anything else
START CHECK
=OR(A6>$F$2,A6<$G$2)
END CHECK
=OR(B6>$F$2,B6<$G$2)
ANY TIME CHECK
I broke this into three columns. It can be combined into one but wanted to show the working parts. The first check is to see if the start time is before the night start and that the shift end time was after the night start time. The second check was similar for the the fisrt except you want to know if the start time is before the night end time and the shift end time is after the night end time. For the OR case you want to check to see if ANY of columns F through I are true:
COLUMN H
=AND(A6<=$F$2,C6>$F$2)
COLUMN I
=AND(A6<$G$2+1,C6>$G$2+1)
Note the +1 for night end time. This is to reflect that the end time is actually on the following day.
COLUMN J
=OR(F6,G6,H6,I6)
or
=(F6+G6+H6+I6)>=1
Place the above formulas in row 6 and copy down as needed
Well, I think I'm missing something but let's see if this works for you. To make this work:
All hours must be typed in hh:mm format
Night Criteria End must be on a different day. This means it must be over 00:00 or formulas won't work. If you type something
like start criteria=22:00 and night criteria= 23:50, both times
would be in the same day, so formulas won't work
This formulas only work in periods less than 24 hours. If anytime the different between criterias is over 24 hours, formulas won't work
properly.
Now, I replied your data like this:
The trick here is dealing with times without dates. Dates in Excel are înteger numbers and decimal parts are the hours/minutes/seconds. So to compare properly hours like this, you need to add an integer part.
Let me explain. In Excel, 18:00 would be 0,75. And 06:00 would be 0,25. If you try to get the difference between both times, you will get -0,5. In decimal it makes sense, but when trying to convert to time, it makes no sense for Excel. So, as I said before, the trick here is adding integers (in this case, because 06:00 is lower than 18:00, we would add +1), so Excel would make 1,25 - 0,75 = 0,5. And Excel can convert 0,5 to hours, and it will return exactly 12 hours, the right result.
So knowing this, the trick in formulas for your data is comparing ends with starts and add 1 or 2 to compare then properly with your criteria. That way Excel can figure out if a time is later or sooner than a criteria.
All my formulas are these ones:
LENGHT: =IF(B5<A5;B5+1-A5;B5-A5)
CROSS MIDNIGHT: =IF(Y(B5<A5;B5<>0);TRUE;FALSE)
START BETWEEN CRITERIA: =IF(AND(IF(A5<$E$2;A5+2;1+A5)>=1+$E$2;IF(A5<$E$2;A5+2;1+A5)<=$F$2+2);TRUE;FALSE)
END BETWEEN CRITERIA: =IF(AND(IF(B5<$E$2;B5+2;1+B5)>=1+$E$2;SI(B5<$E$2;B5+2;1+B5)<=$F$2+2);TRUE;FALSE)
ANY PART:=IF(OR(E5=TRUE;F5=TRUE;AND(A5<$E$2;A5+C5>1+$F$2));TRUE;FALSE)
Anyways, I uploaded a file to my Gdrive, in case it may be helpful to download and check the formulas.
This is the best I got. Probably there is a better way, but I hope this can help you, or at least, you can adapt it to your needs.
https://drive.google.com/open?id=1nrZKfyUhED_y6iiPRSUwRf7GhJlBYt-O

Excel: Count Total Schedules at 30 Minute intervals taking day into account

In assessing how many agents can be added to certain times of day without exceeding the number of seats in the call center, I'm trying to discern how many agents are scheduled for each half hour interval on each day of the week.
Using the =SUMPRODUCT(((A$2:A$1000<=D2)+(B$2:B$1000>D2)+(A$2:A$1000>B$2:B$1000)=2)+0) formula I've been able to identify how many total agents work for each interval, however this doesn't take the day of week into account.
I currently have my spreadsheet setup this way:
K is the start time of the shift, L is the end time of the shift, M to S pulls data from another sheet that shows a 1 if the agent works on that day of the week and 0 if they do not, and then U has all the time intervals listed out. In the example, it's cut off but the columns continue down as needed. U goes to 49 and I've just been using a range from 2 to 500 for the others as we currently do not have that many shifts and I'm leaving space for the moment.
After some Googling, I tried =SUMPRODUCT(--(M2:M500="1"),(((K$2:K$1000<=U2)+(L$2:L$1000>U2)+(K$2:K$1000>L$2:L$1000)=2)+0)) but it only returns #VALUE! so I'm not sure what I'm doing wrong.
Any suggestions of how I can make this work? Please let me know if more information would be useful. Thanks.
=sumproduct(($K$2:$K$1000<=U2)*($L$2:$L$1000>=U2))
That will count the number of occurrences where the start time is less than or equal to the time in U2 AND the end time is greater than or equal to U2. It will check time from row 2 to row 1000. Any time one condition is checked and its true the comparison will result in value of TRUE and FALSE when its not true. The * act like an AND condition while at the same time converts TRUE and FALSE values to 1 and 0. So both conditions have to be true for a value of 1 to result. Sumproduct then totals up all the 1 and 0 to get you a count.
In order to consider the days of the week, you will need one thing to be true. Your headers in M1:S1 will need to be unique (which I believe they are). You will need to either repeat them in adjacent columns to M or in say V1 you have a cell that can change to the header of the day of the week you are interested in. I would assume the former so you can see each day at the same time.
In order to do this you want to add more conditions and pay attention to you reference locks $.
In V2 you could use the following formula and copy down and to the right as needed:
=sumproduct(($K$2:$K$1000<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000))
UPDATE #1
ISSUE 1 Time ranges ending the following day (after midnight)
It is based on the assumption that the ending time is later than the start time. When a shift starts at 22:00 and end at 6:30 as an example, our mind says that the 0630 is later than 22:00 because it is the following day. Excel however has no clue that 0630 is the following day and without extra information assumes it to be the same day. In excel date is stored as an integer and time is stored as a decimal. When you only supply the time during entry it uses 0 for the date.
In excel the date is the number of days since 1900/01/00. So one way to deal with your time out is to add a day to it. This way excel will know your out time is after your in time when the hour is actually earlier in the day.
See your sample data below.
Using your sample data, I did a straight copy of the value in L and placed it in M (=L3 and copy down). I then changed the cell display format from time to general. This allows you to see how excel sees the time. Note how all the time is less than 1.
In column N I added 1 to the value when the out time was less than the in time to indicate that it was the following day and we had not actually invented time travel. I also used the trick of a math operation will convert a TRUE/FALSE result to 1 or 0 to shorten the equation. I used =M3+(L3<K3) and copied down. You will notice in the green bands that the values are now greater than 1.
In the next column I just copied the values from N over using =N3 copied down, and then I applied just a time display format for the cell. Because it is only time format, the date is ignored and visually your time in column O looks the same as column L. The big difference is excel now knows that it is the following day.
you can quickly fix your out times by using the following formula in a blank column and then copying the results and pasting them back over the source column using paste special values.
=M2+(L2<K2)
The next part is for your time check. When looking at the 12:00 time you need to look at 1200 of the current day incase a shift started at 12:00 and you need to look at the 1200 period of the following day. In order to do that we need to modify the the original formula as follows:
=sumproduct(($K$2:$K$1000<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000)+($K$2:$K$1000<=$U2+1)*($L$2:$L$1000>=$U2+1)*(M$2:M$1000))
Note that the + in the middle of (M$2:M$1000) + ($K$2:$K$1000<=$U2+1)? This + acts like an OR function.
Issue 2 Time In/Out 15 minute increments, range 30 minute increments
You may be able to achieve this with the ROUNDDOWN Function or MROUND. I would combine this with the TIME function. Basically you want to have any quarter hour start time be treated as 15 minutes sooner.
=ROUNDDOWN(E3/TIME(0,30,0),0)*TIME(0,30,0)
Where E3 is your time to be converted
So your formula may wind up looking something like:
=sumproduct((ROUNDDOWN($K$2:$K$1000/TIME(0,30,0),0)*TIME(0,30,0)<=$U2)*($L$2:$L$1000>=$U2)*(M$2:M$1000)+((ROUNDDOWN($K$2:$K$1000/TIME(0,30,0),0)*TIME(0,30,0)<=$U2+1)*($L$2:$L$1000>=$U2+1)*(M$2:M$1000))
similar option could be used for the leaving time and rounding it up to the next 30 minute interval. In which case just use the ROUNDUP function. Though I am not sure it is required.

EXCEL-Count weekend days within a number of dates within Countifs with overlap

I have to determine per specialty the total percentage of surgeries that started outside the regular working hours or in weekends.
This is what I have so far:
=COUNTIFS(Table1[Start surgery];">17:00")+COUNTIFS(Table1[Start surgery];"<09:00")+COUNTIFS(Table1[Surgery date];"MOD(WEEKDAY(cell), 7) <2")
The first 2 countifs work, but I'm not able to count the weekend days. And a second thing is that its possible for excel to count some surgeries twice, because a surgery can be after 17:00 and in the weekend, but that has to count as one.
Who can help me:)?
Since the first part of your formula is working for time I just modified the last part of your formula...
=COUNTIFS(Table1[Start surgery];">17:00")+COUNTIFS(Table1[Start surgery];"<09:00")+SUMPRODUCT((WEEKDAY(Table1[Surgery date])=1)+(WEEKDAY(Table1[Surgery date])=7)
Where 1 and 7 are equivalent to Saturday and Sunday.
The current problem with the formula above is that surgery time after 1700 and before 0900 on the weekend are counted twice!
To rectify this all conditions could be moved inside the sumproduct as one potential solution and you would wind up with:
=SUMPRODUCT((WEEKDAY(Table1[Surgery Date])=1)+(WEEKDAY(Table1[Surgery Date])=7)+(WEEKDAY(Table1[Surgery Date])=MEDIAN(WEEKDAY(Table1[Surgery Date]),2,6))*(Table1[Start surgery]<"09:00")+(WEEKDAY(Table1[Surgery Date])=MEDIAN(WEEKDAY(Table1[Surgery Date]),2,6))*(Table1[Start surgery]>"17:00"))
Now the bracket count may be off in there as I was not using excel at the time. The other potential issue we may have is with time. is your time n the time column text or a number formatted to display as time? This may involve modifying are time comparison to a different format, but the formula itself should remain the same.
Basic think here is we put each condition inside the sumproduct. The condition will evaluate to either TRUE or FALSE. If the condition undergoes a math operation it will be turned into 1 for TRUE and 0 for FALSE. + act like OR statements, and * act like AND statement.

COUNTIFS events between dates but also time

For reporting I export raw data from a pbx in CSV. There are many columns with data. Relevant for this case is:
Column A: Date of the events (there are many events on a same day)
Column B: Length of the events in seconds
Column C: Date + Timestamp of every event
I filter out all events shorter or equal to 90 seconds,
I am able to adjust the range by changing dates in 2 cells I created for this occasion (start date is in D1 & end date is in cell D2). Without integarting time I use:
=COUNTIFS(B:B;">=90";A:A;">="&D1;A:A;"=<"&D2)
It works like a charm. I select a range in cells D1 & D2 and I automatically get all the events between these dates excluding events shorter or = to 90 sec.
On top of this I need to know how many events / occurrences happen between certain timestamps. For example from November 1st till November 7th I need to know how man events happened between 12:00h and 13:00h, how many happened between 15:00h & 18:00h etc.
Logically I thought that just adding another criteria_range and criteria (in this example column C) would do the trick. Alas adding column C does not seem to work and I have spinned it many ways.
My intuition is that the DATE + TIME format is inadequate, innapropriate for my case.
Column C looks like this: 02/11/2015 21:59:47
Being european, I'm happy with the DD/MM/YYY notation, but it seems that no formula can take account of the TIME and neglect the DATE in front. Remember I already use column A for the dates. Here in column C I am interested in the TIMESTAMP.
Any aideas or suggestions are welcome.
Thank you very much in advance.
PS: I can always split the time from the date using text to column feature. Yet It means I will be formatting the raw exports and I would like to avoid that at all costs so that I can just copy paste new exports in my control sheet without having to do all sorts of formatting.
Without sample data it is difficult to tell whether you are treating the elapsed time as 90 seconds (integer) or 00:01:30 (as true seconds, a decimal portion of 1). Your formula seems to indicate the elapsed time as an integer but it is also wrong in other places (e.g. =< instead of <=) so the only thing for certain is that it is not a working formula. The same goes for determining the time window. Are you comparing it to 12 and 13 as integers or 12:00:00 and 13:00:00 as true time? They are decidedly NOT the same thing.
The SUMPRODUCT function can provide the cyclic processing required to treat a datetime as time only (e.g. MOD(C2:C12, 1)) or as an integer representing the hour of the day (e.g. HOUR(C$2:C$12)).
  
The formulas in F2, F5 and F7 are,
=COUNTIFS(B:B; ">="&E2; A:A; ">="&E3; A:A; "<="&E4)
=SUMPRODUCT((A$2:A$12>=E$3)*(A$2:A$12<=E$4)*(MOD(C$2:C$12; 1)>=E5)*(MOD(C$2:C$12; 1)<=E6)*(B$2:B$12>=E$2))
=SUMPRODUCT((A$2:A$12>=E$3)*(A$2:A$12<=E$4)*(MOD(C$2:C$12; 1)>=E7)*(MOD(C$2:C$12; 1)<=E8)*(B$2:B$12>=E$2))
If E5 and E6 were 12 and 13 instead of 12:00:00 and 13:00:00 then the formula in F5 would be,
=SUMPRODUCT((A$2:A$12>=E$3)*(A$2:A$12<=E$4)*(HOUR(C$2:C$12)>=E5)*(HOUR(C$2:C$12)<=E6)*(B$2:B$12>=E$2))

How do i Subtract a duration from a time in Excel?

I am having an issue with Excel and it’s probably very simple but I need to get a starting time by subtracting a duration from and end time.
For example say I know that an event needs to end at 1:55:23 pm and it will take 0:22:13 what formula would I use to find out what time I should start?
I would like to be able to input the duration in the format h:mm:ss without excel trying to turn it into a time and not a duration as well.
Thank you for any suggestions
You simply subtract them.
For example...
Cell A1 has the end time: 1:55:23 PM
Cell B1 has the duration: 0:22:13 (this is the only 'strange' part because it's actually an AM time, as in minutes after midnight... but if you think about it, that's what you want).
Cell C1 has this formula: =A1-B1
That's it.
NOTE: If your duration is longer than your event start time (for example, a 4-hour event that started at 3:00 in the morning!) then the subtraction would result in a negative time.
A negative time is OK in reality, but Excel will not display it as a formatted time... instead it displays a bunch of ############. In that case, you need to display the calculation as a decimal value, which is the fraction of a day. For example. 0.25 means exactly 6 hours. If you would prefer decimal hours you simply multiply the fractional day figure by 24.

Resources