EXCEL - IF Statement for TIME between Ranges - excel

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

Related

Using MOD with Time values to determine time differences that transition midnight

I created an Excel Spreadsheet with a Total Time for the Duration of the Shift ie 8:30-17:30.
Then I created this formula
=(MOD([#[Supposed Shift End]]-[#[Supposed Shift Start]],1))*24
with Format cells -> Number 2 decimal places. This gives me the [Total Supposed Shift Hours]
Giving me Duration of the Shift that needs to be covered.
Now I also created another column for the ACTUAL time the shift covered
=(MOD([#[Actual Time End]]-[#[Actual Time Start]],1))*24
This gives me the [Total Actual Time Hours]
For the Actual Time End and Actual Time Start When the employee DIDN'T show up we entered 0 for both cells with the same cell format Number -> 2 decimal places
The Problem:
This is the formula I wrote to subtract these 2 Columns
=(MOD([#[Total Supposed Shift Hours]]-[#[Total Actual Time Hours]],1))*24
Why when I subtract say the [Total Supposed Shift Hours] 9.00 - the [Total Actual Time Hours] 0.00 = 24.00 ???
9 - 0 = 9 not 24.... sigh
Will the formula be affected if the time goes from previous night 21:00 to 8:00 with the MOD formula?
Sample data (as requested)
Note: Nomenclature differs from description above: Open = Supposed and Covered = Actual
Service Date
Open Post Start
Open Post End
Total Hrs Missing
Covered Post Start
Covered Post End
Total Hrs Covered
Category
Hours Not Covered
02/06/2021
16:00
00:00
8
16:00
03:00
11
A
0
04/06/2021
16:00
00:00
8
00:00
00:00
0
A
0
10/21/2021
10:30
00:00
13.5
18:00
19:30
1.5
B
0
Initial Answer
A minor point first: You don't need to wrap the MOD function in brackets. As the function already produces a result to the *24. Thus the following works just fine:
=MOD([#[Supposed Time End]]-[#[Supposed Time Start]],1)*24
To your question: Your non-working formula reads as if it simply wants the difference (in hours) between Supposed and Actual. If that's so, simply do this:
=[#[Total Supposed Shift Hours]]-[#[Total Actual Shift Hours]]
EDIT: Using the (now posted) table, I've constructed what I think it is you're trying to doRefer to Sample results image below.
Formula in Colum E: =MOD([#[Open Post End]]-[#[Open Post Start]],1)*24
Formula in Colum H: =MOD([#[Covered Post End]]-[#[Covered Post Start]],1)*24
Formula in Colum J: =[#[Total Hours Missing]]-[#[Total Hours Covered]]
Sample results:
Now: If Column J (i.e. the response to your core question) isn't the result you're after, can you tell me what it is you would expect there (using actual expected values for each row).
Notes:
Your table column header Total Hours Missing is somewhat confusing.
But, I'm reading that a Post Duration (Duration of Shift in your original parlance).
If I understand what transpired correctly, the "spanner in the works" was #P.b suggestion to remove MOD. Revert your formulas in columns E and H as shown above.

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.

Convert date & time in Excel, to time only, and round to nearest 30 minutes

I need a formula to convert excel date & time to time only and nearest 30 minutes. Example 1/5/2017 4:38:29 PM convert to 1630
A1 B1
1/5/2017 4:38:29 PM 1630
1/5/2017 5:03:40 PM 1700
1/5/2017 4:39:27 PM 1630
I tried if function & vlookup with
=MROUND((TEXT(A1,"hhmm")),1/48)
but I get 0:00 answer, and also
=MOD(Y5,"0:30")
but I get a 0 answer.
It looks like you were almost there. You need MOD to get the decimal portion of a datetime (i.e. the time) then MROUND to the nearest half-hour.
=MROUND(MOD(A1, 1),TIME(0,30,0))
FLOOR and CEILING operate the same as MROUND in case you require the lower or higher half-hour.
You may need to format the cell for the desired time format mask.
Assuming data starts in A1, copy the following down to suit:
=HOUR(A1)&IF(MINUTE(A1)>29,30,"00")

Determine if time is before or after specific time

I have an Excel sheet with two date-time columns: Start Date and End Date. I need to calculate overtime by determining how many hours and minutes were worked before 08:00 and after 16:30.
Examples:
COLUMN A COLUMN B
1 2017/01/04 16:00 2017/01/04 18:00
2 2017/01/05 18:00 2017/01/05 21:00
3 2017/01/08 08:00 2017/01/08 17:00
4 2017/01/09 07:00 2017/01/09 09:00
I know the calculation for duration between times (=TEXT(B1-A1,"h:mm")), but I'm struggling to determine if a date-time is before 08:00 or after 16:30.
I've played around with TIMEVALUE("8:00:00"), TIMEVALUE("8:00:00 AM") and TIME(8,00,00) in my calculation, but the it did not work,
e.g. =IF(A1<=TIMEVALUE("8:00:00 AM"),"Yes","No") returns "No" even for A4 (2017/01/09 07:00)
I'm struggling with the calculation and the logic on this one. So far i'm using,
=IF(MOD(F2,1)<8/24,8/24-MOD(F2,1),0)+IF(MOD(G2,1)>16.5/24,MO‌​D(G2,1)-16.5/24,0)
Try this:
=IF(TIMEVALUE(A1)<=TIMEVALUE("8:00:00 AM"),"Yes","No")
Stick with treating time as time; only treat time as text when you have bad data (time as text is bad data) and then only enough to get it to be time as time.
To get the number of hours and minutes before 08:00 AM,
=MAX(0, TIME(8, 0, 0)-TIMEVALUE(TEXT(A2, "hh:mm;0;#")))
To get the number of hours and minutes after 16:30 AM,
=MAX(0, TIMEVALUE(TEXT(B2, "hh:mm;0;#"))-TIME(16, 30, 0))
In the following image you can see that I have left one group as true time and the forced the following group to be time as text¹. Both return correct results.
¹ Unless specifically formatted otherwise, numbers and dates/times are right-aligned in a cell; text is left-aligned.

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