Nested/Multiple IF functions in excel, how can I? - excel

I am trying to implement an excel spreadsheet to keep track of my working hours.
I am entitled to unpaid breaks at my work, however these vary depending on the length of shift as follows.
0 - 3.75 hours = 0 min break
4 - 5.75 hours = 15 min break
6 - 7.75 hours = 30 min break
8 - 8.75 hours = 45 min break
9 - 11.75 hours = 90 min break
I would like to calculate my working hours minus breaks in excel.
I think I need to use if statement similar to the following (taking E2 as my hours):
=if(E2<=3.75, E2, if(4<=E2<=5.75, E2-30, if(6<=E2...etc...
But it never seems to work, can someone help me get this working???
Thanks.

You may try this:
=IF(E2<=3.75,0,IF(AND(E2>=4,E2<=5.75),15,IF(AND(E2>=6,E2<=7.75),30,IF(AND(E2>=8,E2<=8.75),45,IF(AND(E2>=9,E2<=11.75),90,"")))))
EDIT : 1
=IF(E2<=3.75,E2,IF(AND(E2>=4,E2<=5.75),(E2*1440-15)/1440,IF(AND(E2>=6,E2<=7.75),(E2*1440-30)/1440,IF(AND(E2>=8,E2<=8.75),(E2*1440-45)/1440,IF(AND(E2>=9,E2<=11.75),(E2*1440-90)/1440,"")))))
EDIT : 2
=IF(E2<=3.75,E2,IF(AND(E2>=4,E2<=5.75),E2-0.25,IF(AND(E2>=6,E2<=7.75),E2-0.5,IF(AND(E2>=8,E2<=8.75),E2-0.75,IF(AND(E2>=9,E2<=11.75),E2-1.5,"")))))
See image for reference.

Related

Excel Change "x h x m" Format into minutes only

I am Trying to change the Excel cells which contain hours and minutes with "h" and "m" into minutes only. The Problem here that the letters h & m are in the cell.
What i need it to look like:
2h 30m --> 150
4h --> 240
51m --> 51
Please help me out. thanks in advance.. !!
Try:
Formula in B1:
=1440*TEXTAFTER(":0:"&SUBSTITUTE(SUBSTITUTE(A1,"m",""),"h",":"),":",-2)
You could try something like this:
=IF(ISNUMBER(SEARCH("h";A1));VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1;" ";"");"h";":");"m";""))*24*60;VALUE(SUBSTITUTE(A1;"m";"")))
Notice there is an IF to separate cases when there is only minutes from cases when there is also hours (calculus is different)
So find the "h" to get the hours like so:
=left(A2,find("h",A2,1)-1)
Then multiply the result by 60 (as 60 minutes in an hour)
For minutes:
=mid(A2,find("h",a2,1)+1,2)
This assumes that minutes is double digit as per your example, you could make it more precise by finding the position of the "m" and calculating the difference.
Then sum the result to get minutes.
Another legacy formula:-
=1440*SUBSTITUTE(SUBSTITUTE(IF(COUNTIF(A1,"*h*"),"","0h")&A1,"h",":"),"m","")

Excel - sumproduct for tiered pricing model

I have below data set starting in A1:
start tier end tier price
1 5 5
6 7 4
8 10 3
11 111 2
112 1
The usage is in D1 and is equal to 10. I want to calculate the value based on usage and decreasing price. I have got two sumproducts but having no luck.
First one:
=SUMPRODUCT(--($D$1>A2:A6), ($D$1-B2:B6), C2:C6)
Second one:
=SUMPRODUCT(--($D$1>{0,6,8,11,112}),--($D$1-{5,7,10,111,1000}),{5,-1,-1,-1,-1})
Both miscalculate.
I prefer the first one as no hard coding is involved.
It seems to me that you have made a mistake in the #paul's solution comment and the result should be 42 - if the usage is 10 then it divides into 5 * 5, 2 * 4 and 3 * 3.
If I'm right, you can try the following formula:
=SUMPRODUCT(($D$1>=A2:A6)*((B2:B6-A2:A6)+1+($D$1-B2:B6<0)*($D$1-B2:B6))*C2:C6)
Use SUMPRODUCT and LOOKUP:
=SUMPRODUCT(LOOKUP(ROW($ZZ$1:INDEX($ZZ:$ZZ,D1)),{1,6,8,11,112},{5,4,3,2,1}))
With the lookup table:
=SUMPRODUCT(LOOKUP(ROW($ZZ$1:INDEX($ZZ:$ZZ,D1)),A:A,C:C))
Are you sure you want to use SUMPRODUCT for this? RANGELOOKUP might be more suitable.
Given the data:
End Tier Price
0 5
5 4
7 3
10 2
111 1
You can find the price for a quantity (e.g. 18 units) using =VLOOKUP(18,A2:B6,2,TRUE)

Determine number of days within a date range across multiple years

I hope I can explain this properly. I need to calculate occupancy time for a population of migratory animals. Occupancy is defined as the period, in days, between the first and last sighting of an individual in a given year between 1999 and 2015. A sample of the type of sighting data I am dealing with is pasted below. I need to be able to calculate the number of days between sightings within a year and NOT between the first and last sightings or between each sighting. Further, I need a value of "1" if the animal was only seen once in that year, rather than a value of "0".
So, for example, based on individual 37 in the data below, the results table would look something like
1999 - 0 2000 - 11 2001 - 40 2002 - 2 2003 - 0
1999 - 0
2000 - 11
2001 - 40
2002 - 1
2003 - 0
2004 - 52
2005 - 1
...and so on.
I have tried this series of equations but it does not return the right values for the years in which an animal was seen only once and it ignores any years prior to the first year seen.
in cell K2:
=DATE(YEAR(MIN($B$2:$J$2)),1,1)
in cell L2:
=IF(N(K2)=0,"",MAX(MAX(IF($B$2:$J$2=K2,$B$2:$J$2)),0))
in cell M2:
=IF(N(K2)=0,"",IF(MAX($B$2:$J$2)>DATE(YEAR(K2)+1,1,1),DATE(YEAR(K2)+1,1,1),""))
so if more than 1 year between sightings then it should be 0?
and is that more than "365 days a year" or if it is spotted 1 time 1. december and 1 time 1. januar then its still only spotted 1 time pr year...
I was able to find a solution using this equation in the cells immediately following the last sighting.
=IF(COUNTIFS($B2:$V2,">="&DATE(BE$1,1,1),$B2:$V2,"<="&DATE(BE$1,12,31))>0,LOOKUP(DATE(BE$1,12,31),$A2:$V2)-INDEX($A2:$V2,MATCH(DATE(BE$1,1,0),$A2:$V2)+1)+1,0)

Stata/Excel time formatting issue

I have a variable minutes that I am having trouble cleaning/standardizing.
It is imported from Excel in a Date Time format, but I just want the minutes. For example, if a player plays 30 minutes and 34 seconds, it's displayed as 30:34 or 30:34:00. However, it's stored in Excel as 6:34 AM (30:34 is interpreted as military time), or 12:34 AM, depending on whether it is 30:34 or 30:34:00. Thus it ends up getting imported into Stata as 6:34 or 12:34, when the value I want is what's displayed (30:34 or 30:34:00). Is there a way for you to format a number in Excel to just be the value that is displayed?
Once it's imported into Stata it's impossible to standardize, because you cannot differentiate a player that plays 30:34 (when it displays 30:34:00) from a player that plays 6:34 (they will both show 6:34).
Please make a greater effort when posting questions. People in capacity to help might ignore the question because it is difficult to understand, because you provide no code (and thus show no effort), because the problem is not reproducible, and more.
Suppose an MS Excel sheet like the following
Then the following should get you started:
clear
set more off
import excel timetest.xls, cellrange(C2:C4) firstrow
gen hour = hh(time)
gen sec = mm(time)
gen realmin = hour + 24
order realmin, before(sec)
list
resulting in
. list
+-------------------------------------------+
| time hour realmin sec |
|-------------------------------------------|
1. | 01jan1900 06:34:00 6 30 34 |
2. | 01jan1900 00:00:00 0 24 0 |
+-------------------------------------------+
See help datetime. This is a must if working with dates and/or time.
Note that adding 24 won't work for any data set.
A general solution takes the form of
clear
set more off
import excel timetest.xls, cellrange(C2:C5) firstrow
gen hour = hh(time)
gen sec = mm(time)
gen t = dofc(time)
format t %td
gen dayselap = t - td(01jan1900) + 1
gen realmin = hour + (24 * dayselap)
drop hour t dayselap
order time realmin
list
For example, this data in MS Excel:
time
30:34:00
24:00:00
58:04:00
65:00:00
will produce
. list
+------------------------------------+
| time realmin sec |
|------------------------------------|
1. | 01jan1900 06:34:00 30 34 |
2. | 01jan1900 00:00:00 24 0 |
3. | 02jan1900 10:04:00 58 4 |
4. | 02jan1900 17:00:00 65 0 |
+------------------------------------+
(There might be an issue with leap years that you are encouraged to research yourself.)
An alternative solution may involve converting the values to text (within MS Excel) and then managing the text within Stata.
I am not at all familiar with Stata.
The problem is that the actual data in Excel is being entered incorrectly. Excel stores dates and times as days + fractions of a day, and then displays it according to the formatting.
So:
30:34 is really 0 hours 30 minutes 34 seconds and is stored as 2.1226851851851854E-2 which is the computation: 30/(24*60)+34/(24*60*60)
However 30:34:00 is really 30 hours, 34 minutes 0 seconds and is stored as 1.273611111111111 which is the computation 30/24+34/(24*60)
Depending on whether it is entered properly as 0:30:34, or improperly as 30:34 determines the value that is stored.
In Excel, you could possibly pre-process the values if you have some test to tell if the data was entered correctly or not.
For example:
=IF(time_unreasonable,A1/60,A1)
would convert the values if they had been incorrectly entered as hours:minutes rather than minutes:seconds.
Other solutions may also be feasible, perhaps even in Stata.

Rounding Up Minutes above 8, 23, 38, 53 to the nearest quarter hour

Here’s a challenge for someone. I’m trying to round session times up to the nearest quarter hour (I report my total client hours for license credentialing)
8 minutes or above: round up to 15
23 minutes or above: round up to 30
38 minutes or above: round up to 45
53 minutes or above: round up to 60
Ex: in the first quarter hour, minutes below 8 will be their exact value: 1=1, 2=2, 3=3.
When 8 is entered, it is automatically rounded up to 15 (the same holds true for the rest of the hour: ex: 16=16, 17=17, 18=18, 19=19, 20=20, 21=21, 22=22. But 23 through 29 are all rounded up to 30.
Ideally, I could enter both hours and minutes in a single column, ex: 1.54
However, I realize that it may be necessary to create a separate column for hours and minutes in order to make this work (i.e., so that my formula is only concerned with rounding up minutes. I can add my hours and minutes together after the minutes are rounded.) Thus:
Column A = Hours (3 hours maximum)
Column B = Minutes
Column C = Minutes Rounded up to nearest ¼ hour
Column D = Col A + Col C
In column B I would like to enter minutes as 1 through 60 (no decimal- i.e., in General, not Time format)
38 minutes in column B would automatically be rounded up to 45 minutes in column C
Does anyone have any ideas? How can I do this using the fewest number of columns?
[A Previously posted question - "Round up to nearest quarter" - introduces the concept of Math.Ceiling. Is this something I should use? I couldn't wrap my head around the answer).
With Grateful Thanks,
~ Jay
How's this go?
DECLARE #time DATETIME = '2014-03-19T09:59:00'
SELECT CASE
WHEN DATEPART(mi, #time) BETWEEN 8 AND 15 THEN DATEADD(mi, 15-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 23 AND 30 THEN DATEADD(mi, 30-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 38 AND 45 THEN DATEADD(mi, 45-DATEPART(mi, #time), #time)
WHEN DATEPART(mi, #time) BETWEEN 53 AND 59 THEN DATEADD(mi, 60-DATEPART(mi, #time), #time)
ELSE #time
END
Assume "sessions" is your table (CTE below contains 2 sample records), with session start time & end time stored (as noted in comments above, just store these data points, don't store the calculated values). You might be able to do the rounding as below. (not sure if this is what you want, since it either rounds up or down... do you not want to round down?)
;WITH sessions AS (
SELECT CAST('20140317 12:00' AS DATETIME) AS session_start, CAST('20140317 12:38' AS DATETIME) AS session_end
UNION ALL
SELECT CAST('20140317 12:00' AS DATETIME), CAST('20140317 12:37:59' AS DATETIME) AS session_end
)
SELECT *, DATEDIFF(MINUTE, session_start, session_end) AS session_time
, ROUND(DATEDIFF(MINUTE, session_start, session_end)/15.0, 0) * 15.0 AS bill_time
FROM sessions;
EDIT:
Hi Jay, I don't think you mentioned it is an Excel problem! I was assuming SQL. As Stuart suggested in a comment above, it would be helpful if you modified your question to indicate it is for Excel, so that others can possibly get help from this dialog in the future.
With Excel, you can do it with two columns that contain the session start date and time (column A) and session end date and time (column B), plus two formulas:
Column C (Actual Minutes) = ROUND((B1-A1) * 1440,0)
Column D (Billing Minutes) = (FLOOR(C1/15, 1) * 15) + IF(MOD(C1,15) >= 8, 15, MOD(C1,15))
This is what my table looks like:
3/18/2014 12:00 3/18/2014 12:38 38 45
3/18/2014 14:00 3/18/2014 14:37 37 37

Resources