Excel formula for between time frames - excel

not sure if stack also accepts/allows general excel questions but figured I'd ask anyway. Looking for help with the excel formula that checks the time in one column and returns the shift (day, night or overnight) in another column. I have the night and overnight shifts outputting correctly but for some reason the day shift does not?
Here are the two excel formulas:
=IF(AND(HOUR(Table8[#Time]) >7, HOUR(Table8[#Time] <15)),"1st shift")
=IFS(AND(HOUR([#Time]) >= 7, HOUR([#Time] <= 15)), "1st shift", AND(HOUR([#Time]) >= 15, HOUR([#Time]) <= 23), "2nd shift", OR(HOUR([#Time]) >= 23, HOUR([#Time]) <= 7), "3rd shift")

The parenthesis is the issue here
Check it out like this
=IFS(AND(HOUR([#Time]) >= 7, HOUR([#Time]) <= 15), "1st shift", AND(HOUR([#Time]) >= 15, HOUR([#Time]) <= 23), "2nd shift", OR(HOUR([#Time]) >= 23, HOUR([#Time]) <= 7), "3rd Shift")

you put the bracket at the wrong place for the second check for the 1st shift
This is correct:
=IFS(AND(HOUR([#Time]) >= 7, HOUR([#Time]) <= 15), "1st shift", AND(HOUR([#Time]) >= 15, HOUR([#Time]) <= 23), "2nd shift", OR(HOUR([#Time]) >= 23, HOUR([#Time]) <= 7), "3rd shift")
HOUR([#Time] <= 15) is wrong

Related

Countdown to a specific date & time in VBA

time = DateSerial(2020, 10, 13)
I can use DateSerial and a Do-While Loop in VBA to make a countdown to 13th October 2020 midnight with the above code. How can I make it countdown to 10 am on 13th October 2020?
Thank you.
Add 10 hours using TimeSerial:
time = DateSerial(2020, 10, 13) + TimeSerial(10, 0, 0)

Excel IF Function (3 Values)

Please help me out with this one in Excel.
I used this one but does not work
=IF(Q67 < 6, "D", IF(Q67 > 6 < 11, "M", IF(Q67 > 10, "E", "")))
This should go like this:
E- Greater than 11
D - Less than 6
M - Greater than 5 but not equal to 11
Your answers will be appreciated. Thank you :)
First issue, the ranges overlap...
So, assuming my correction is valid you could try:
=if(Q67>11,"E",if(Q67<6,"D","M"))
Try,
=IF(Q67 < 6, "D", IF(Q67 < 11, "M", "E"))
'testing if Q67 is not blank as well
=IF(Q67="", "", IF(Q67 < 6, "D", IF(Q67 < 11, "M", "E")))
If Q67 is not less than 6 it fails the first test and moves on to the second. You don't need to check if Q67 is greater or equal to 6 at that point because if it wasn't greater or equal to 6 it wouldn't be there in the first place.
However, it may be important to understand how two conditions work.
=IF(Q67 < 6, "D", IF(AND(Q67 >= 6, Q67 < 11), "M", IF(Q67 >= 11, "E", "")))

Using IF AND and multiple conditions

I need to create a formula to identify the tier based on the following conditions:
Result Conditon1 Condition2
tier_1 >=61 >=300001
tier_2 >=25 - <=60 >=100001 - <=300000
tier_3 <=24 >=0 - <=100000
I have created individual formulas but I can't put them together
=if(and(B19>=61,B1823>=300001),"Tier 1")
=IF(and(B18>=100001,B18<=300000,B19>=25,B19<=60),"Tier 2")
=if(and(B19>=0,B19<=100000,B18<=24),"Tier 3")
Your help is greatly appreciated.
Thank you,
Carlos
The usage of the Excel IF function is:
=IF(some_condition, "true", "false")
You may nest your current IF calls to get the desired logic:
=IF(AND(B19 >= 61, B1823 >= 300001), "Tier 1",
IF(AND(B18 >= 100001, B18 < =300000, B19 >= 25, B19 <= 60), "Tier 2",
IF(AND(B19 >= 0, B19 <= 100000, B18 <= 24), "Tier 3", "No Tier")))
Note that in the innermost/final IF call, I provided an else value of No Tier. This is because there has to be an else value. If you instead want everything which does not match tiers 1 and 2 to default to tier 3, then you can remove this final IF and just provide Tier 3 as the else value.

AVERAGEIFS: Weighted rankings

I have a formula for calculating rankings of certain events across different organizations:
=AVERAGEIFS(C2:C100, B2:B100, "A", C2:C100, ">0") * IF(COUNTIF(B2:B100, "A") < 3, 0.7, IF(COUNTIF(B2:B100, "A") < 10, 0.9, IF(COUNTIF(B2:B100, "A") > 30, 1.1, IF(COUNTIF(B2:B100, "A") > 50, 1.2, 1))))
This formula works, but for some reason, using data that I have, I know the AVERAGEIFS value for "A" should be multiplied by 1.2, as there are more than 50 instances of "A" in Col. B. The result the above formula gives multiplies by 1.1.
I've also tried to expand the above formula to incorporate additional factors that could affect the ranking:
=AVERAGEIFS(C2:C100, B2:B100, "A", C2:C100, ">0") * IF(COUNTIF(B2:B100, "A") < 3, 0.7, IF(COUNTIF(B2:B100, "A") < 10, 0.9, IF(COUNTIF(B2:B100, "A") > 30, 1.1, IF(COUNTIF(B2:B100, "A") > 50, 1.2, 1)))) * IF((AND(B2:B100, "A"), COUNTIF(S2:S100, "yes")>1), 1.1, 1))
Running this second formula gives me an error message: "The formula you typed contains an error."
Not sure what's going wrong in the second formula, as I've continued to build on the first formula above.
Since 30 is less than 50 your formula (which short-circuits) never gets as far as what is more than 30 as a separate condition. Maybe something like:
=AVERAGEIFS(C2:C100, B2:B100, "A", C2:C100, ">0") * IF(COUNTIF(B2:B100, "A") < 3, 0.7, IF(COUNTIF(B2:B100, "A") < 10, 0.9, IF(COUNTIF(B2:B100, "A") > 50, 1.2,1))))
This relies in the default factor of 1 for values of 10 to under 30.
Might be easier to see what is happening with:
LOOKUP(COUNTIF(B2:B100,"A"),{0,3,10,30,50},{0.7,0.9,1,1.1,1.2})
after the asterisk.

Declare value based on date range without year

I have about 10000 records in an Excel sheet (although I can import into Access 2010 if easier) and I need to set a value in a separate column if the dates fall between certain planting seasons. Year is irrelevant.
Thus if a date falls between a certain range, the column is filled in with its appropriate planting season.
Spring = 3/16 - 5/15
Summer 5/16-8/15
Fall 8/15-10/31
Everything else is Null
3/20/2015 and 4/16/2013 are both "Spring" in the seasons column
6/28/2011 and 8/1/2015 are both Summer, etc.
Ideas as to how I can do this? As I said I can do it in either Excel or Access, whichever approach is easier.
You can use the DateSerial Function in an Access query. For example your spring season for planting date could be expressed as ...
planting_date BETWEEN DateSerial(Year(planting_date), 3, 16) AND DateSerial(Year(planting_date), 5, 15)
You could use similar patterns in a Switch expression to determine the planting season for each planting_date ...
SELECT
y.planting_date,
Switch
(
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 3, 16) AND DateSerial(Year(y.planting_date), 5, 15), 'spring',
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 5, 16) AND DateSerial(Year(y.planting_date), 8, 15), 'summer',
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 8, 16) AND DateSerial(Year(y.planting_date), 10, 31), 'fall',
True, Null
) AS season
FROM YourTable AS y;
If you want to store those season values in a field named planting_season, use the Switch expression in an UPDATE query ...
UPDATE YourTable AS y
SET y.planting_season =
Switch
(
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 3, 16) AND DateSerial(Year(y.planting_date), 5, 15), 'spring',
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 5, 16) AND DateSerial(Year(y.planting_date), 8, 15), 'summer',
y.planting_date BETWEEN DateSerial(Year(y.planting_date), 8, 16) AND DateSerial(Year(y.planting_date), 10, 31), 'fall',
True, Null
);
This formula assumes the first date is in cell A1. Enter the formula in cell B1 (or another empty cell from row 1):
=CHOOSE(MATCH(A1-DATE(YEAR(A1),1,0),{0;75;136;227;305},1),"","Spring","Summer","Fall","")
Now copy downward as far as needed.
That's it.
Note: this works by calculating the day of the year, for example March 16 is the 75th day of the year. Once the 'day of the year' is calculated, a simple binary match is performed on an array of day numbers that correspond to your planting seasons. Finally, the CHOOSE function is used to translate the MATCH results to the name of the season.
To edit your planting schedule, simply adjust this 'days of the year' array: {0;75;136;227;305}
Note: this is an extremely efficient self contained formula. No external helper columns are required and there are no IF functions, nested or otherwise.
Addendum: here is a variation that has a clause that deals with Leap Year:
=CHOOSE(MATCH(A1-DATE(YEAR(A1),1,0),{0;75;136;227;305}+(2=MONTH(DATE(YEAR(A1),2,29))),1),"","Spring","Summer","Fall","")
A few of helper columns to extract month and date would be useful (Assume values are at A1:A1000):
B1 = MONTH(A1)
C1 = DAY(A1)
D1 = DATE(2000, B1, C1)
We intentionally "cheat" about the year for easier comparison.
After that, there's nothing special to do here, just a lot of nested ifs:
IF(AND(D1 >= DATE(2000, 3, 16), D1 <= DATE(2000, 5, 15)), "Spring",
IF(AND(D1 >= DATE(2000, 5, 16), D1 <= DATE(2000, 8, 15)), "Summer",
IF(AND(D1 >= DATE(2000, 8, 16), D1 <= DATE(2000, 10, 31)), "Fall", "Winter")))

Resources