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

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.

Related

Excel - Formula to filter column based on variable time

I have a column that needs split based on "morning" and "evening" although the morning and evening times move every day (it's based on sidereal day). Calling them morning and evening is a little deceiving though because eventually the time will creep into the next day and I want to keep the groups distinct. It is more accurate to call them group 1 and group 2. It just so happens that they are around 12 hours apart so it looks like you can just separate based on time of day but once the later group creeps into the AM hours, it would start to get counted as "morning" and the earlier group would roll into the afternoon and be counted as "afternoon" See screenshot below for example data.
I need them split so I can perform operations on the value column so I can distinguish the values in the first group from the values in the second group. I thought of doing some sort of flip flop algorithm based on the previous cell but there may be a more elegant way to do it. Also, it's not shown on the example data but sometimes the day may skip but the times more or less continue in the same pattern of increasing by 3-5 minutes each day.
A date with a time stamp is stored as a number in Excel. Days are stored as whole numbers, time is stored in decimals. So, disregarding the date part, look at the decimal of the number and determine if that is before or after the time you want.
0.5 for example is midday, or 12 noon. So if the decimal part of A1 is less than 0.5, the time stamp would be in the morning.
=if(A1-int(A1)<0.5,"before noon","after noon")
It is not clear from your question how sidereal relates to the data in your sample.

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 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.

Excel, MIN for overnight times

My current data is like so:
I use the MIN formula to get the minimum of these times. I am measuring a process time, so the time on T is the least, and T+1 is actually greater. How can I alter my MIN formula to account the 11:51 as the min time?
I can use MAX for above problem, but then when times are 3, 4 , 5 AM, it will give me 5 AM when I want MIN throughout.
One way around this is to add the date to the time. You do not need to display it, but you do need the date as part of the time. JNevill is all over this without coming right out and saying it. You can either do as Jnevil suggest and offsetting all your time by an equal amount so it is all in the same day or you have to add +1 to the time when it crosses the midnight threshold. Adding +1 to your time will tell excel that it is on the following day.
In Excel time is stored as a decimal and days are stored as integers. So any time with no date attached will go from 0.xxx to 1.xxx when +1 is added. The cell will still display xxx as time and the 1 does not enter in to the display. However the 1 will be very important in determining MIN or MAX because of that integer of 0 or 1 out front.
You will probably need to do this through a helper column. Without seeing a column of data it is hard to say if you only need to add 1 or if you will need to add 2 or more depending on how many days the data covers

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

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]))

Resources