Excel - Placing values that fall within a certain time range - excel

I have the following data on duration of particular services.
A B C D
Usage Start (Local time) Start Time Usage Until (Local time) End Time
03.03.2018 10:00 12:00:00 AM 03.03.2018 00:00 1:00:00 AM
03.03.2018 00:00 1:00:00 AM 03.03.2018 00:00 2:00:00 AM
03.03.2018 16:30 1:00:00 AM 03.03.2018 00:00 3:00:00 AM
And I want to count the number of times a timing falls within a certain range(eg. 00:00 - 01:00)
Start End Counts
00:00 01:00 1
01:00 02:00 2
02:00 03:00 1
03:00 04:00
04:00 05:00
05:00 06:00
What formula should I use and how do I apply it?
I have tried this
=COUNTIFS($B$2:$B$566,">="&A2,$D$2:$D$566,"<"&B2)
And this
=SUMPRODUCT(($B$2:$B$566>= A2)*($D$2:$D$566< B2))
But it doesn't account for more than 24 hour duration (Eg.3/3/2018 1:00 PM - 4/3/2018 2:00 PM, 25 hours)

It looks like you're ignoring the time portion in Columns A and C. Assuming your start/end hour bins are in columns F and G, the formula below will give you the number of times that an event passed through your first hour bin (you have to enter it as an array formula--ctrl+shift+enter). Fill down for subsequent hour bins.
=SUM(($B$2:$B$4<F2+TIME(0,30,0))+($D$2:$D$4>F2+TIME(0,30,0))+INT($C$2:$C$4)-INT($A$2:$A$4)-1)
This will count an event in an hour bin if the event is active at the half-hour mark.
If the event needs to cover the entire time bin, use this formula (again with ctrl+shift+enter):
=SUM(($B$2:$B$4<=F2)+IF(G2>F2,($D$2:$D$4>=G2))+INT($C$2:$C$4)-INT($A$2:$A$4)-1)

Was a bit more complicated than I expected, but I got it to work.
Though before we start, you need to format your table a little. You
can't have date and time in the same cell (as excel has trouble
recognizing storing two formats in 1 cell).
Instead format your table
like so:
Next we should create some hidden rows (technically you can stick what I'm about to do in 1 giant formula, but for sake of clarity I prefer hidden rows)
First we focus on the time difference in hours. Creating the two following columns
Also, make sure, you set the format in these newly added rows to
number. Technically it will work without it, but it will look
confusing to human eye
In the Time defference we use the following formula
=ABS(F2-C2) * 24
In the overnight h column:
=IF(C2>F2, 24-G2, G2) This is utilized that we properly count with transition dates (eg. 23:00 1/1/18 -> 01:00 2/1/18) we don't want to add the extra 24 hours in this case.
That would be the hours fully working, but we also need to ensure the
formula works, when there's more than one night of difference (eg.
1/1/18 -> 3/1/18)
Next, we add these these two rows
In the date difference column, as the column name suggests
=DATEDIF(A2, D2, "d") * 24
Last but not least, the overnight delta is basically the same principle as overnight h, but with dates instead of hours
So now, if we have hidden the rows, added the following formula to result =H2+I2-J2 we get our sought after table:
Which corresponds with the expected result! :)
EDIT: If you don't want to take hours into cosnideration and only count how many times an interval of 24 hours has expired use the following formula
=IF(C2>F2, DATEDIF(A2, D2, "d")-1, DATEDIF(A2, D2, "d"))
(under presumption c2,f2,a2,d2 are in the columns as in my original table i provided)

Related

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

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)

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

how to do 2 seperate operations if start time of shift is before 7 but end time is after 7

I'm trying to make an excel sheet where I only need to put in start and end time and excel chooses the correct pay rate and how many hours I've worked (already done) and outputs how much I've earned. So far I have a column (D) for the date of shift (DAY, day of month, Month, year) column for hours worked (E), column for start time and end time (F, G) I have already written the formula to calculate the hours worked but in Australia where I live my pay rate increases after 7 PM, and increases again after 12 AM. Is there a way to have excel automatically know that it needs to take the hours worked before 7 PM and multiply it by a 24.41, then the hours worked between 7 PM and 12 AM by 26.54 etc, if my shift starts for example at 5:30 pm and ends at 3 AM?
These are the different payrates at the different times: (Time is in cell A1, Pay rate is B1, etc)
Time Pay Rate
Regular $24.41
Mon-Fri 7pm-midnight $26.54
Mon-Fri 7midnight-7am $27.60
Saturday $29.30
Sunday $34.18
Public Holidays $48.83
Thanks in advance
Solutions to all of these type of questions use the standard formula for overlapping time periods
=max(0,min(end1,end2)-max(start1,start2))
The best way to do it IMO is to simplify it two ways
(1) By splitting any times that cross through midnight into two parts, one up to midnight and one from midnight onwards
(2) Using a lookup table to match the conditions (day of week and time of day) to the payscale.
Then use an array formula to do the lookup and calculation. Because you can't use MAX and MIN as above in an array formula, you have to write it out using if statements and the formula gets pretty long
=SUM((WEEKDAY(E2,2)>=PayRates!$A$2:$A$10)*(WEEKDAY(E2,2)<=PayRates!$B$2:$B$10)*24*PayRates!$E$2:$E$10*
IF(IF(G2<PayRates!$D$2:$D$10,G2,PayRates!$D$2:$D$10)-IF(F2>PayRates!$C$2:$C$10,F2,PayRates!$C$2:$C$10)<0,0,
IF(G2<PayRates!$D$2:$D$10,G2,PayRates!$D$2:$D$10)-IF(F2>PayRates!$C$2:$C$10,F2,PayRates!$C$2:$C$10)))
This has to be entered using CtrlShiftEnter
This is how my pay rates are arranged
NB When the finishing time of a pay rate is midnight, it is entered as 1 (you want it to be 24:00, but entering 24:00 just gives you the same as 00:00)
And this is the main sheet
With the following columns
A,B and C are your input.
Split
=B2<A2
StartDate1 and StartTime1 are just a copy of your input
EndTime1
=IF(Split,1,B2)
StartDate2
=E2+Split
StartTime2
00:00
EndTime2
=IF(Split,B2,0)
Total1
The main formula
Total2
The main formula copied across by four columns to give any pay for the second day when the shift goes through midnight.
Total
Total1+Total2
Public holidays can be added fairly easily.

Specific overtime calculation

I'm working on a simple timesheet that should calculate overtime1 and overtime2, I just cant figure out a good way to fix it.
Normal workday on 8hrs from 07:00 to 17:00 give no overtime (eg 07:00 to 16:00 or 08:00 to 17:00), but when time exceeds 8 hours I would like to get the exceeding hours in a cell.
My business rules are:
1) Any work greater than 8 hours between the hours of 06:00 to 20:00 get paid as overtime1.
2) Any work less than 8 hours will not generate any overtime2 even if after 20:00
3) Any work performed earlier than 06:00 or later than 20:00 get paid at the overtime2 rate.
Example 1: Working from 07:00 to 18:00 would get a value of 3 hours of overtime1
Example 2: Working from 14:00 to 22:00 would generate 0 hours of overtime2.
Example 3: Working 05:00 to 21:00 would give overtime1 6 hours and overtime2 2 hours (1 hour before 06:00, 1 hour after 20:00).
This is a pretty easy problem to solve if your data is laid out smartly. With column A as clock in time and column B as clock out time, use this formula as a helper to determine if you should use the clock in time or your base of 06:00:
=IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))
Then use this formula to determine if you should use clock out time or 20:00:
=IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))
Then you subtract the two to get fractions of a day, multiply by 24 to convert to hours, then subtract 8 to get hours of overtime1. Combined in a super formula it looks like this in C1:
OT1: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))-IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2)))*24-8)
Remember, Excel formats dates where 1 = 24 hours. Also, I added in an OR(ISBLANK(A1),ISBLANK(B1)) statement to make sure you get a null string if one of the values is blank.
Starting on the overtime2, you need to split it into two parts: before 06:00 and after 20:00. The first part checks if the clock in time is earlier than 06:00 and if so figures out how many hours. The formula ultimately ends up being:
=IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)
For after 20:00, the same pattern is used. Figure out how many parts of a day were logged after 20:00. The final formula ends up being:
=IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0)
Finally, to figure out the total number of overtime 2, just add the two formulas together in D1:
OT2: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)+IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0))
It's just about getting the logic right and understanding that Excel treats 07:00 as a decimal equal to 7/24, for example.
OT1
=IF(NOT(AND(Sheet1!$A2>=7/24,Sheet1!$B2<=17/24)),MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,0)
OT2
=IF(MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,MAX(B2-20/24,0),0)

How do I add a unit suffix to the hours an employee has worked?

I'm looking for a formula to count hours and set up a shift plan
My idea is for managers, front of house and back of house to have their own planner.
Where I fall at is when I try to count hours for each staff member BUT also make it legible to the reader when printed
For example
Staff member 4 does
0830-1600 with an hour break on Monday
1200-1900 with no break on Tuesday
I want it to look like 0830-1600, but in the total hours to count out the whole week from the times put in each day
I think you need to re-design your calendar. Excel loves raw data to be split out onto separate rows so let's start with that.
This is my end goal - it's a simplified version of yours
Data
So let's set out the data that will get us there - in rows. For this example, paste the data into cell A6:
Name Day Time In Time Out Total
Name 1 09/01/17 08:00 18:00 10:00
Name 1 12/01/17 11:00 15:00 04:00
Name 1 13/01/17 12:00 21:00 09:00
Name 2 10/01/17 08:00 13:00 05:00
Name 2 11/01/17 12:00 20:00 08:00
Name 2 13/01/17 10:00 14:00 04:00
And then add another few columns onto the end which will be useful in getting totals and making the table more readable
Unique ID is created simply by concatenating name and day : =A7&B7. And the Time String uses the TEXT function
=UPPER(TEXT(C7,"hAM/PM")&"/"&TEXT(D7,"hAM/PM"))
Table
Create your table in A1:F3 (like below). No need for manual shift data but do put your people and dates headings in:
Use this simple formula in cell B2 and drag down and across to populate shift data
=IFERROR(VLOOKUP($A2&B$1,$F$6:$G$12,2,0),"OFF")
Similarly, you can use this formula for the total hours
=SUMIF($A$7:$A$12,$A2,$E$7:$E$12)*24
Result
The final result is below, and can be easily manipulated for more people/ dates, etc.
If you have questions about any of these formulas/ techniques -- do ask

Resources