Chaining Multiple IF Statements in Excel to Calculate Work Shifts from Times - excel

I am trying to calculate a work shift based on the time entered in an Excel worksheet. The shift allocation is as below:
If Time >= 05:30 PM & <= 01:30 AM - 1st shift
If Time >= 01:30 AM & <= 09:30 AM - 2nd shift
If Time >= 09:30 AM & <= 01:30 AM - 3rd shift
I am using IF AND conditions but still it's showing incorrect results. Below is the formula I am using.
=IF(AND(D2>=TIMEVALUE("05:30:00 PM"),(D2<=TIMEVALUE("01:30:00 AM"))),"1",
IF(AND(D2>=TIMEVALUE("01:30:00 AM"),(D2<=TIMEVALUE("09:30:00 AM"))),"2","3"))
Any idea what I am doing wrong?

The first problem is in your shift definitions. The time 1:30 AM falls in all three shifts due to the use of less-than-or-equal-to operators. These should be changed to less-than operators, such as the following:
If Time >= 5:30 PM & < 1:30 AM - 1st shift
If Time >= 1:30 AM & < 9:30 AM - 2nd shift
If Time >= 9:30 AM & < 5:30 PM - 3rd shift (note: I corrected an apparent typo here as well)
The second problem is in the first if statement:
IF(AND(D2>=TIMEVALUE("05:30:00 PM"),(D2<=TIMEVALUE("01:30:00 AM"))),"1",...)
The time can't simultaneously be both greater than 5:30:00 PM and less than 01:30:00 AM. Times in Excel are stored as numbers. TIMEVALUE("1:30 AM") returns 0.06250 and TIMEVALUE("5:30 PM") returns approximately 0.72917. As a result this if statement translates to the following:
IF(AND(D2>=0.72917, D2<=0.06250), "1", ...)
D2 cannot be both greater-than-or-equal-to 0.72917 and less-than-or-equal-to 0.06250. As a result this will always return false.
Try rewriting your code as follows:
=IF(D2<TIMEVALUE("1:30 AM"),"1",
IF(D2<TIMEVALUE("9:30 AM"),"2",
IF(D2<TIMEVALUE("5:30 PM"),"3","1")))

Related

Need help getting a shift to return a time frame in Excel

I am trying to get a formula into excel that will return a time frame based on the Shift entered in a cell. I see a lot of questions doing the opposite, taking a time frame and returning Shift 1, Shift 2 or Shift 3, but I can't seem to work the formulas backwards to fit what I'm working towards.
For reference the Shift Column, if
1 should return 6:45 am to 2:45 pm
2 should return 2:45 pm to 10:45 pm
3 should return 10:45 pm to 6:45 am
Excel Shift Snippet
Two solutions:
Use the INDEX function: store the 3 shift timeframes in range Q3:Q5 and find the correct time from it. Here is what it gives: screenshot_solution_formula (I've added two more columns for start and end in case it helps, but no problem if we remove them). Below is the formula to put in L2 and to pull.
L2 = INDEX(Q$3:Q$5,NUMBERVALUE(LEFT($K2,1)),0)
Use the CHOOSE function to choose from different timeframes specified in formula, by putting this in L2:
L2 = CHOOSE(NUMBERVALUE(LEFT($K2,1)),"6:45 am to 2:45 pm"," 2:45 pm to 10:45 pm", "10:45 pm to 6:45 am")

EXCEL - IF Statement for TIME between Ranges

Im using PivotTables and PivotCharts for a Dynamic Dashboard in Excel. Currently i have 3 shifts at work that i'd like to visualize but for that to happen i must:
Extract the time from a "Date & Time Cell" which im doing by using
=A2 - INT(A2) . No problem here
Create a nested IF AND formula to check whether the hour fits in any of theses shifts:
15:00 - 23:00 , 23:00 - 7:00 , 7:00 - 15:00
Im using this:
=IF(AND(E2>=TIME(15,0,0),E2<TIME(23,0,0)),"3-
11",IF(AND(E2>=TIME(23,0,0),E2<TIME(7,0,0)),"11 - 7","7 - 3"))
But it mixes up the shifts, its showing only 3-11 & 7-3 shifts, as if it couldn't distinguish between am and pm... im missing the 23:00 - 7:00 shift
From the TIME documentation:
The decimal number returned by TIME is a value ranging from 0 (zero) to 0.99988426, representing the times from 0:00:00 (12:00:00 AM) to 23:59:59 (11:59:59 P.M.)
In other words, AND(E2>=TIME(23,0,0),E2<TIME(7,0,0) is equivalent (rounded to 2 decimal places) to AND(E2>=0.96,E2<0.29). So your second AND formula can only be false.
Either change that AND to OR, or flip your 2nd IF logic around:
...IF(AND(E2>=TIME(7,0,0),E2<TIME(15,0,0)),"7 - 3","11 - 7")

Excel Time Comparison and Subtraction

I am trying to do a time subtraction in excel of 30 minutes and I am running into a speed bump. So the table I have are as follows.
Table "Schedule"
Column 1 is day of the week (Mon-Sun) (formated as general, as this is plain text)
Column 2 is start time of the shift (formated as h:mm AM/PM)
Column 3 is end time of the shift (formated as h:mm AM/PM)
Column 4 is duration of the shift (start to end) (formated by formula (TEXT(col3-col2,"h:mm")) )
Column 5 is paid hours (if the total hours is over 6.5 then subtract 0.5 hours for an unpaid lunch) (formula IF(col5>"6:30",col5-"0:30",D5) )
The issue is any time allotment over 10 hours start to end (where column 4, the duration hits 10 hours) no lunch is subtracted at all.
So...
Start 9:00 AM, End 6:59 PM, Hours Total 9:59, Hours Paid 9:29
But...
Start 9:00 AM, End 7:00 PM, Hours Total 10:00, Hours Paid 10:00
and that should obviously not happen. I can't find anything on google so I figured the excel gurus here may have some advice.
Thanks!
If your time columns are stores using excel's dedicated time format, this should be straightforward. Mixed data types are likely your problem.
First, be sure your time columns (columns 2 and 3) are set using the time function, i.e.,
=time(hours,minutes,seconds)
Then, you should be able to add and subtract easily.
Column 4: = column 3 - column 2
... then subtract 30 minutes also using the time() function:
Column 5: = if(column 4 > time(6,30,0),column 4 -time(0,30,0),column 4)
Excel stores time values from 0 to 1. So 24 hours=1, 12 hours=.5 etc. That means 6.5 hours=0.270833333 and .5 hours=0.020833333. As a result you can just do a simple if statement.
=IF(D2>0.270833333,D2-0.020833333,D2)
To turn it into a time format, is to just use excel's time formating options.

Creating a Timesheet with at different rates

Scenario:
I have an excel Timesheet which needs to calculate Standard Time 1x, Overtime 1.5x, Overtime 2x based on the following values:
If the start time and finish time fall between 08:00 and 16:45, then sum the hours x1
If the finish time falls between 16:45 and 00:00, then sum the hours by x1.5
If the finish time falls between 00:00 and 08:00 then sum hours by x2
Example Data:
Start time Finish time Standard time Overtime x1.5 Overtime x2
08:30:00 17:00:00 7.5 0.5 0
17:00:00 01:00:00 0 7 1
01:00:00 10:00:00 2 0 7
06:00:00 12:00:00 4 0 2
I have been racking my brain and possible ways to do this but keep falling short, does anybody know how I would go about creating a formula to do this kind of some?
Ah, I am too late as the other answer has been accepted which is great if it covers a 48-hour period. Anyway this is how mine looks - can provide some explanation later.
Here are the definitions relative to cell D2
So the basic formula is
=MAX(0,MIN(ClockOff+Split,ShiftEnd)-MAX(ClockOn,ShiftStart))
This is based on the standard formula for calculating the overlap of two ranges quoted in various places e.g. here but adds 1 (equal to one whole day) for the case when the end of shift is after midnight. This works for standard time (starting in D2) and overtime X 1.5 (starting in E2) as long as the end of the evening shift (in J4) is also entered as 1 whole day (24:00 hours). For overtime X 2, both the start of shift (0:00) and end of shift (08:00) have to be adjusted by 24 hours if the hours worked are split across midnight so the formula in F2 is
=MAX(0,MIN(ClockOff+Split,ShiftEnd+Split)-MAX(ClockOn,ShiftStart+Split))
Create a matrix of times and use an if statement to determine if the time falls within the shift or not. Sum the times as required for each shift.
Here's a link to my example http://www.scotlang.com/Overtime.xlsx.

How to count hours in excel

I have xls file in following format
Name 1 2 3 4
John 09:00-21:00 09:00-21:00
Amy 21:00-09:00 09:00-21:00
Where 1,2,3,4 and so on represent days of current month,
09:00-21:00 - working hours.
I want to calculate salary based on the following conditions:
09:00-21:00 - 10$/hour
21:00-00:00 - 15$/hour
00:00-03:00 - 20$/hour
etc.
and so on (every hour can have it's own cost, for example 03:00-04:00 - 20$/hour, 04:00-05:00 - 19$/hour, etc.)
How can i accomplish this using only Excel (functions or VBA)?
P.S. Easy way: export to csv and process in python/php/etc.
Here is a non-VBA solution. It's a pretty nasty formula, but it works. I am sure it could be made even easier to use and understand with some more ingenuity:
Assuming the spreadsheet is set up like this:
Enter this formula in cell G1 and drag down for your data set:
=IF(ISBLANK(B2),"",IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2),SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B2,2),$Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2)),SUMIF($Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B2,2),$P$2:$P$24)))
To explain the formula in detail:
IF(ISBLANK(B2),"" will return a empty string if there is no time for a given person / day combination.
LEFT(B2,2) extracts the start-time into an hour.
Mid(B2,Find("-",B2)+1,2) extracts the end-time into an hour.
IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2) will check if the start-time is less than the end-time (meaning no over-night work). If the start-time is less than the end-time, it will use this formula to calculate the total cost per hour: SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B3,2),$Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2))
If the start-time is higher than the end-time (meaning overnight work), it will use this formula to calculate: SUMIF($Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B3,2),$P$2:$P$24)
The use of the Find("-",[cell]) splits the start-and- end times into values excel can use to do math against the Time / Cost table.
The formula in column Q of the Time / Cost table is =VALUE(MID(O2,FIND("-",O2)+1,2)) and turns the ending hour to consider the cost into a value Excel can use to add, instead of having the text from your original source format.
Do this in VBA! It is native to excel and is easy to learn. Functionally, I would loop through the table, write a function to calculate the dollars earned based on the info given. If you want your results to be live updating (like a formula in excel) you can write a user defined function. A helpful function might be an HoursIntersect function, as below:
Public Function HoursIntersect(Period1Start As Date, Period1End As Date, _
Period2Start As Date, Period2End As Date) _
As Double
Dim result As Double
' Check if the ends are greater than the starts. If they are, assume we are rolling over to
' a new day
If Period1End < Period1Start Then Period1End = Period1End + 1
If Period2End < Period2Start Then Period2End = Period2End + 1
With WorksheetFunction
result = .Min(Period1End, Period2End) - .Max(Period1Start, Period2Start)
HoursIntersect = .Max(result, 0) * 24
End With
End Function
Then you can determine the start and end time by splitting the value on the "-" character. Then multiply each payment schedule by the hours worked within that time:
DollarsEarned = DollarsEarned + 20 * HoursIntersect(StartTime, EndTime, #00:00:00#, #03:00:00#)
DollarsEarned = DollarsEarned + 10 * HoursIntersect(StartTime, EndTime, #09:00:00#, #21:00:00#)
DollarsEarned = DollarsEarned + 15 * HoursIntersect(StartTime, EndTime, #21:00:00#, #00:00:00#)
I have a method that uses nothing but formulas. First create a lookup table which contains every hour and rate in say columns K & L, something like this:
K L
08:00 15
09:00 10
10:00 10
11:00 10
12:00 10
13:00 10
14:00 10
15:00 10
16:00 10
17:00 10
18:00 10
19:00 10
20:00 10
21:00 15
22:00 15
23:00 15
Make sure you enter the hours as text by entering a single quote before the digits.
Then if your hours were in cell B2 you could then use this formula to calculate the total:
=SUM(INDIRECT("L"&MATCH(LEFT(B2,5),K2:K40,0)&":L"&MATCH(RIGHT(B2,5),K2:K40,0)))
All the formula is doing is getting the left and right text of your work time, using MATCH to find their positions in the lookup table which is used to create a range address which is then passed to SUM via the INDIRECT function.
If you need to worry about minutes all you need to do is create a bigger lookup table which holds every minute of the day. You may need to add some extra logic if your work days span midnight.

Resources