Two Separate Ranges of Business Hours (Like Shifts) Between Date Ranges - excel

I've run into a dilemma that I thought I would overcome easily but ended in getting different results.
So I've been using the following formula:
=(NETWORKDAYS(H6,N6)-1)*($F$2-$E$2)+IF(NETWORKDAYS(N6,N6),MEDIAN(MOD(N6,1),$F$2,$E$2),$F$2)-MEDIAN(NETWORKDAYS(H6,H6)*MOD(H6,1),$F$2,$E$2)
This formula works perfectly, as I've modified it to use NETWORKDAYS.INTL because all workdays are working days, but the working hours are from 8:00 AM to 11:00 PM.
My modified code is:
=(NETWORKDAYS.INTL([#[DC_CREATION_DATE]],[#[ACTUAL_END_DATE]],"0000000")-1)*(upper-lower)
+IF(NETWORKDAYS.INTL([#[ACTUAL_END_DATE]],[#[ACTUAL_END_DATE]],"0000000"),MEDIAN(MOD([#[ACTUAL_END_DATE]],1),upper,lower),upper)
-MEDIAN(NETWORKDAYS.INTL([#[DC_CREATION_DATE]],[#[DC_CREATION_DATE]],"0000000")*MOD([#[DC_CREATION_DATE]],1),upper,lower)
Where:
DC_CREATION_DATE = Start Date
ACTUAL_END_DATE = End Date
upper = Named Range containing the End Time i.e. 11:00 PM
lower = Named Range containing the Start Time i.e. 8:00 AM
Now the problem is that for a particular month, the working hours in a day change to shifts:
Start Time 1 = 11:00 AM
End Time 1 = 5:00 PM
Start Time 2 = 9:00 PM
End Time 2 = 2:00 AM
Any time outside the above-mentioned times are non-working hours. The TTs we receive CAN be outside working hours, but I will only calculate time duration inside working hours.
What I tried doing was to use the same formula twice on the same date range (first time, upper becomes 5:00 PM and lower becomes 11:00 AM and the result is stored in the cell, the second time, upper becomes 2:00 AM and lower becomes 9:00 PM and the result is stored in a different cell), and then add the two results in separate cells to get the overall working hours between the working hours range.
Alas, I thought it was that simple but what I'm seeing are different results, for both formulas when they're run.
Because if the time is outside of the working range, even for start and end, it should return 0. (I believe I can overcome this by using IF)
The ######## is the field showing the value in negative. Since I'm formatting all cells to [h]:mm
Anyone who can help me in this regard?

Since your Upper 2 time is next day, you need to enter it as such.
The underlying value of 2:00 AM is 0.083333333. To make it next day, enter it as 1.083333333. You can stil format it as 2:00 AM if you wish, it's the underlying value your formula is using.
Some examples, with your formula unchanged (other than including Upper/Lower 1 and 2)

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 IF AND formula between two times

I would like to have a formula which will tell me if a time in a cell is between 2 separate vlaues in other cells and to return a value if so.
I have already created the below code but this is not returning any values back at all.
=IF(AND(F4>=$R$1,F4<P1),"Night Shift",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","")))
In this example the cell values are (P1 = 06:00, Q1 = 14:00, R1 = 22:00). The value in the F4 is 00:31:38.
Any help would be appreciated.
Your first AND needs to adjust a little.
Excel sees TIME as a fraction of 1 whole day. So 00:31:38 though you meant it to be the next day from 22:00, Excel does not know that and as such will not see it greater than 22:00
We also do not need to test for the Night Shift. It is the only option left if the time is not in one of the others:
=IF(F4<>"",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","Night Shift")),"")
You could also create a small table like such:
0 6:00 14:00 22:00
Night Shift AM Shift PM Shift Night Shift
Then use a HLOOKUP to return the correct value:
=HLOOKUP(F4,O1:R2,2,TRUE)
I took a slightly different path that Scotts.
A Night Shift occurs if the time is greater or equal to 10PM, OR is less than 6AM.
=OR($F$4<$P$1,$F$4>=$R$1)
An AM Shift occurs when the time is greater or equal to 6AM, AND is less than 2PM.
=AND($F$4>=$P$1,$F$4<$Q$1)
A PM Shift occurs when the time is greater or equal to 2PM, AND is less than 10PM.
=AND($F$4>=$Q$1,$F$4<$R$1)
Stick the three conditions together and you have:
=IF(OR($F$4<$P$1,$F$4>=$R$1),"Night Shift",IF(AND($F$4>=$P$1,$F$4<$Q$1),"AM Shift",IF(AND($F$4>=$Q$1,$F$4<$R$1),"PM Shift","")))
Edit
During testing I entered 00:00:00 in A1 and =A1+TIMEVALUE("00:01:00") in A2:A1440.
At 06:00:00, 14:00:00 and 22:00:00 the changeover in shift happened a minute later.
If, however, I manually typed in 06:00:00 the changeover happened on the hour. This seems to be because TIMEVALUE is calculating 6AM as 0.2499999 rather than 0.25.
=IF(OR(HOUR(NOW())>22,HOUR(NOW())<7),"NIGHT","")&IF(AND(HOUR(NOW())>6,HOUR(NOW())<15),"MORNING","")&IF(AND(HOUR(NOW())>14,HOUR(NOW())<23),"LATE","")
I know a bit late but simplifies everything without the need of using other cells (note there are different times used).
Night - 23-07
AM - 07-15
PM - 15-23

Excel Categorize Times of day

I am trying to create an excel function that categorizes the time of day.
I have a column of DateTimes, Format: 3/3/2017 13:30 (but can change the format if needed). I need a second column declaring "ON" for the hours 9 pm to 5 am, "AM" for 5 am to 9 am, "BH" for 9 am to 5 pm, and "PM" for the hours 5 pm to 9 pm. I need weekends too but I figure I can pull those manually.
Result would look like (where I have column 1 and need to calculate column 2):
DateTime Time period
3/3/2017 13:30 BH
3/3/2017 17:30 PM
3/4/2017 3:30 ON
3/5/2017 5:30 AM
Make a table with the lower time and expected out put:
Then a simple VLOOKUP:
=VLOOKUP(MOD(A2,1),F:G,2)
We can save a few characters by only testing one end of each range. Once we have tested a value for less than 5 am, we no longer need to test to see if it is greater than 5 am. (The AND( statements aren't necessary.)
=IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON"))))
If you want to detect weekends, wrap the whole thing in a weekend test. Two methods presented:
The better approach, suggested by #ScottCraner in the comments:
=IF(weekday(a1,2)>5,"WEEKEND","THE WHOLE THING")
The first argument for weekday( is obviously the date we are testing. The second forces Monday to be the first day of the week, which leaves Sat & Sun as the last two.
Combined with the rest, we would get:
=IF(weekday(a1,2)>5,"WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
An unnecessarily long, and somewhat fragile approach (breaks in non-English)
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND","THE WHOLE THING")
This works because TEXT(A1,"ddd") formats the date as the three-letter day of week. In English, at least, both weekend days starts with an "S", and we use left( to grab that first letter.
Together, it would end up looking like:
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
Just to show a different way:
=IF(NETWORKDAYS(A2,A2),CHOOSE(SUM((({"5:00";"9:00";"17:00";"21:00"}*1)<MOD(A2,1))*1)+1,"ON","AM","BH","PM","ON"),"WEEKEND")
This is an array formula and must be confirmed with ctrl+shift+enter.
Can be extended as you like without getting in trouble of the bracket-limit.
Also like the Scott's answer: this works with times like 17:43. ;)
A little long, but works:
=IF(AND(HOUR(A2)>=9,HOUR(A2)<17),"BH",IF(AND(HOUR(A2)>=17,HOUR(A2)<21),"PM",IF(AND(HOUR(A2)>=5,HOUR(A2)<9),"AM","ON")))

Formula to get the worked hours from time A to time B where B is past midnight

I'm using Excel to write down my shifts and get a total of worked hours per day and per week.
I structured it this way:
Everything seemed to work fine until I finished to work at 12.30 am. The result with my formula was -17 hours instead of 7. How can I fix my formula so that it displays a correct amount? I'm using the following formula and I want the result to be displayed in number format, not time.
=IF(C11=0,0,IFERROR(((C11-B11)-D11)*24,0))
What formula can I use?
Excel treats days as 1 for every day past Dec 31, 1899. Today happens to be 42,217. Time is nothing more than a decimal portion of a day. Today at noon was 42,217.5 and tomorrow at 03:00 will be 42,218.125.
Excel also treats boolean (e.g. TRUE/FALSE) values as either 1 or 0 when used in a mathematical equation. e.g. 0.5 + TRUE = 1.5 while 0.5 + FALSE = 0.5.
Test to see if the minuend is less than the subtrahend and if it is, add 1 to it using the result of the test itself.
=(C11+(C11<B11)-B11)*24
      
Finally, it should be mentioned that while you can subtract a larger time from a smaller time to receive a negative decimal value, the negative value cannot be interpreted as time since Excel does not recognize negative time. If you were not multiplying by 24 to retrieve the hours as integers and simply subtracting B11 from C11 the cell would be filled with hashmarks (e.g. ############) to show the error. e.g. 08:00 - 10:00 = (as time) #######.

Time start to specific time end from multiple locations

I am attempting in creating an Excell page that determines the START time from multiple locations, from different time distnances to a single point.
Lets say that i have 15 start points from varying locations trying to reach a single destination. I am trying to set the arrival time as the end point and work backwords by using the destination time it takes. For instance, i want to coordinate a multiutude of units to converge at 11pm at night. I know that the first unit takes 5h30m, and the second takes 2h 45m to reach the same point at the same time. I would have to calcuate 11pm -5h30m = 5:30pm march srart for unit one, and 11pm-2h45m = 8:15pm for unit 2.
I have tried the subraction using the [h]:mm military script with subraction of the time to desitnation. All that has resulted is the difference between. ie 23:00 - 13:00 = 10:00 . But if the destinatiin time is 08:30 - 1300 all i get is #######. Where the first time is convergeance time and the 2nd time is march time.
I was able to get it to work if the comon convergeance time did not cause the march time to be before 00:01. And thats the crux , i cant seam to get it to work for a start time before midnight.
Any ideas ?
Use date and time, and not just times. You would probably also get an 'issue' around a daylight saving cusps so best to work in gmt through-out and convert from/to day-light-saving time (user time) for entry and display.
If you want to use times only then try using MOD function, e.g. with A2 = 23:00 and B2 = 2:45 you can use
=MOD(A2-B2,1)
and that will give you 20:15
......but it also works with A2 = 08:30 and B2 = 13:00....the same formula will give you 19:30

Resources