Rounding time using two cases in one formula - excel

I want a formula for Excel work like that
08:45 turns into 8:00
and at the same time if
8:55 turns into 9:00
So I mean ignore the minutes and round it to the hour except if the minute is 55 or above round it to the next hour as shown above.

I think =TIME(IF(MINUTE(A1) >= 55; HOUR(A1)+1;HOUR(A1));0;0) is what you need.

Related

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

rounding cells up when the number hits X.60

I'm making something in excel which calculates my hours I work a week/month, when a number gets to 30.60 (two shifts which when finishing on the half an hour) it calculates it as 30.60*wage=not the right pay.
so far I have =ROUND(SUM(C6:I6),0) which rounds up the number, which works fine until I have another day which I work till half an hour then it will just show 16 or so.
As you can see here, it calculates it fine until I work 7.30 hours on a wednesday, the total shows 23.00 instead of 23.30.
How can this be done.
Thank you.
your problem is with excel understanding of your "hours". When you write 7.30 you mean 7 hours 30 minutes = 7.5 hours. But excel understands that as 7 hours and 30/100 of hour = 18 minutes. The easiest solution would be to use 7,5 for 7 hours 30 minutes.
(for sake of checking the question off the unanswered I copied my comment)
If you don't want to use 7.5 (Seven-and-a-half-hours) or 7:30 (7 hours, 30 minutes - but remember to multiply this by 24, since Excel stores this as the fraction of a day, 0.3125) then you can use INT and MOD:
=INT(C6)+(MOD(C6,1)/0.6)
The first part, INT(c6) will give you the Integer part (i.e. whole hours) which we don't want to scale/skew.
The second part has 2 stages. First, MOD(c6,1) will give us the Decimal part of the number (i.e. 7.3 will become 0.3) and the second part is to divide by 0.6 to convert from "fake-minutes" to "fraction-of-real-hour"
Finally, since you want to apply the formula to an array of cells, you will need to swap from SUM to SUMPRODUCT:
=SUMPRODUCT(INT(C6:I6)+(MOD(C6:I6,1)/0.6))
But, overall, best option is to use 7:30 and set Data Validation only allow actual Time values in that field.
{EDIT} Of course, this will give your output with 0.5 for 30 minutes. If you want to reverse back to 0.3 for 30 minutes (although, I can scarcely fathom why) then you need to run the same calculation in reverse:
=INT(SUMPRODUCT(INT(C6:I6)+(MOD(C6:I6,1)/0.6))) + 0.6*MOD(SUMPRODUCT(INT(C6:I6)+(MOD(C6:I6,1)/0.6)),1)

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)

Round time to nearest 15 minutes

I'm trying to round my overtimes in Excel and I found 2 formulas:
=FLOOR(A1,"00:15")
as well as
=ROUNDDOWN((A1)*(24*60/15),0)/(24*60/15)
and they both failed when there was a time which does NOT need rounding down.
It is all good when I want to round down e.g. 01:29 to 01:15 or 00:48 to 00:45, but when I have e.g. 01:30 it rounds it down to 01:15, when obviously I need to leave it as it is:/
A1 is the example cell when my time is stored.
for 01:30, this formula:
=FLOOR(A1,"00:15")
returns 01:30 for me.
I am on Excel 2010
Like others have said, =FLOOR(A1,"00:15") should work, but if your version of Excel doesn't do the right thing for some reason, this should work:
=TIME(HOUR(A1),FLOOR(MINUTE(A1)/15,1)*15,0)
=FLOOR(D4*96;1)/96 works fine.
Why 96 ? Because for time units, 1 = 1 day, so 1 hour = 1/24.
You multiply by 24 then by 4 (for quarters), then floor, then divide by 4 to get rounded quarters.

Round Excel Time Difference to Next 15 Minute Interval

I have a start time and an End Time in Excel:
Start Time
9:15 PM
End Time 9:30 PM
Time Spent 0:15
I'm looking to round up to the next 15 minute increment, and are using the formula:
=ROUNDUP(A1*96,0)/96
However, this rounds up the example data above from 0:15 to 0:30 (It should stay at 0:15 because it's a quarter hour already)
How to I avoid the rounding if the time difference is ALREADY on a quarter hour?
I would normally expect the original formula to work. When I test in Excel 2007 with manually input time values that are always whole minutes the original formula with ROUNDUP, brettdj's formula and the following formula:
=CEILING(A1,"0:15")
.....all give me the same results
....but if the times are derived from some sort of formula calculation then excel floating point arithmetic can cause very small rounding errors so that 0:15 is deemed to be very marginally above that value (although it would still display as 0:15:00.000) and is therefore rounded up to 0:30.
Because brettdj's suggestion only looks at hours and minutes that problem is avoided but it will round 0:15:59 to 0:15 too, so this approach might be preferable
=CEILING(MROUND(A1,"0:0:01"),"0:15")
That rounds the original time to the nearest second with MROUND (thus dealing with any floating point errors) and then rounds up to the next quarter hour
Edit: if you are using a formula to get the A1 time value it might be worth incorporating some rounding within that formula, e.g. wrap formula in MROUND like this to round to the nearest second
=MROUND(formula,"0:0:01")
It may look like that does nothing in terms of changing the formula results but that will potentially avoid any further floating point issues with any other calculations you might need to do.....and then your original formula with ROUNDUP should work......
You could use
=HOUR(A1)/24+CEILING(MINUTE(A1),15)/(24*60)
which converts a time in A1 of
- 9:15 PM to 9:15 PM
- 9:16 PM to 9:30 PM
etc

Resources