Home » excel » average with multiple conditions on excel

# average with multiple conditions on excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I generally ask Python or VBA questions, but this has been annoying me for the past 4.5 hours and i need it to finish a macros and i’m just getting div/0 error.

In excel i have five columns:
1. Date when the item entered the queue (A)
2. time when the item entered the queue (B)
3. Date when the item left the queue (C)
4. time when the item left the queue (D)
5. time the item was in the queue (E)

I need the average of time of the items that entered and left the queue between 16:00 yesterday and 16:00 today. Also for it to ignore numbers < than 1 (because if an item has been there for over 24 hours the default value is negative).

I tried tried this and no results 🙁

=AVERAGEIFS(E1:E4,A1:A4,”=today()-1″,B1:B4,”>16:00″,C1:C4,”=today()”,D1:D4,”<16:00″)

Any ideas would be appreciated! Thanks in advance 🙂

How to&Answers:

Your formula is saying this:

``````=AVERAGEIFS(E1:E4,A1:A4,"=today()-1",B1:B4,">16:00",C1:C4,"=today()",D1:D4,"<16:00")
``````

Take the average of E1:E4 if…

1. A1:A4 are equivalent to the string “=today()-1”
2. B1:B4 are equivalent to the string “>16:00”
3. C1:C4 are equivalent to the string “=today()”
4. D1:D4 are equivalent to the string “>16:00”

What you want it to say is:

Take the average of E1:E4 if…

1. A1:A4 are equivalent to the result of the formula `=today()-1`
2. B1:B4 are less than 16 hours
3. C1:C4 are equivalent to the result of the formula `=today()`
4. D1:D4 are less than 16 hours

Let’s tackle 2 and 4 first.

In Excel, 1 day (24 hours) = 1. Times are stored as decimal values as a percentage of a day:

1 day = 1
1 day = 24 hours
1 hour = 1 / 24
16 hours = 16/24 = 2/3

So instead of `B1:B4,">16:00"` you should rewrite the formula as:

``````B1:B4,">"&2/3
``````

The `&` concatenates the greater than sign, and 2/3 will give the decimal value equivalent to 16:00. In the end you will get a string equal to “>.6666666666”

(You can also just use `B1:B4,">.6666666666666"` if you’d like)

For items 1 and 3, you don’t need to use quotes at all. So instead of `A1:A4,"=today()-1"` you can just use the following:

``````A1:A4,today()-1
C1:C4,today()
``````

Putting this all together, the following formula should work:

``````=AVERAGEIFS(E1:E4,A1:A4,today()-1,B1:B4,">"&2/3,C1:C4,today(),D1:D4,"<"&2/3)
``````

### Answer：

If you are looking for “items that entered and left the queue between 16:00 yesterday and 16:00 today” isn’t it possible that some of those entered the queue today (or left the queue yesterday)? If so you would need to check for items that entered today as well, surely? To do that is difficult with `AVERAGEIFS` function – it might be easier to use an “array formula” like this:

`=AVERAGE(IF(A1:A4+B1:B4>=TODAY()-1/3,IF(C1:C4+D1:D4<TODAY()+2/3,E1:E4)))`

confirmed with CTRL+SHIFT+ENTER

I’m assuming that the formula doesn’t have to explicitly deal with your negative values because those will only occur for items which have been there for more than 24 hours and the IF functions will exclude those anyway…

If you want to do the same with AVERAGEIFS you’d need to create two additional columns with the entry/exit dates/times added, e.g. if column G is the sum of columns A and B and column H is the sum of columns C and D you could use this version

`=AVERAGEIFS(E1:E4,G1:G4,">="&TODAY()-1/3,H1:H4,"<"&TODAY()+2/3)`

Note that I used >= and < deliberately to avoid double-counting/non-counting across several days