简体   繁体   中英

Calculate Average based on multiple condition in Excel

I am back with my new excel question. Lets say I have table like this.

    |  A                           |  B  
------------------------------------------
 1  |  ENV                         |  Value
------------------------------------------
 2  |  ABC - 10/1/2014 1:38:32 PM  |  4
 3  |  XYZ - 10/1/2014 1:38:32 PM  |  6 
 4  |  ABC -  9/1/2014 1:38:32 PM  |  1 
 5  |  XYZ - 10/1/2014 1:38:32 PM  |  10 
 6  |  ABC - 10/1/2014 1:38:32 PM  |  7
 7  |  XYZ -  9/1/2014 1:38:32 PM  |  1 
 8  |  ABC -  9/1/2014 1:38:32 PM  |  10 
 9  |  ABC - 10/1/2014 1:38:32 PM  |  7 
10  |  XYZ - 10/1/2014 1:38:32 PM  |  7 

Now, in Cell C2, I've selected ABC.

So in cell D2, I want the average ( from col B ) of all the "ABC" ( col A ) where Month = 10 ( col A ) and in cell E2, Max ( from col B ) of all the "ABC" where Month = 10 ( col A ).

So, my result in cells D2 and E2 would be 6 and 7 respectively.

I hope my question and example make sense.

UPDATE:
Thank you all for all your help.
Now let's say I am not sure how many rows I'll have on this spreadsheet, so I came up with this formula, but its not working, giving me #DIV/0! error.
*Note: I am using formula to get "ABC" and "10" from cell C2.

=AVERAGEIFS(
(OFFSET($A$1,1,1,COUNTA($B:$B)-1,1)),
OFFSET($A$1,1,0,COUNTA($A:$A)-1,1), (MID(C2,1,(FIND("-",C2))-2)),
OFFSET($A$1,1,0,COUNTA($A:$A)-1,1), (MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))  

Even tried this, but same error:

=SUMPRODUCT(((MID(A2:A10,1,(FIND("-",A2:A10))-1))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(MONTH(DATEVALUE(MID(A2:A10,7,99)))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(B2:B10))/SUMPRODUCT(((MID(A2:A10,1,(FIND("-",A2:A10))-1))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(MONTH(DATEVALUE(MID(A2:A10,7,99)))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1)))))  

Can you help me with this...?

Solution with Intermediary Values

To solve the issue (I tested the average only) I first used 2 intermediary values: this solution is not optimal and there will be many smarter ways to address the issue (eg pivot tables).

ENV                                Value        Intermediary 1  Intermediary 2  

ABC - 10/1/2014 1:38:32 PM          4           ABC             10  
XYZ - 10/1/2014 1:38:32 PM          6           XYZ             10  
ABC -  9/1/2014 1:38:32 PM          1           ABC             9   
XYZ - 10/1/2014 1:38:32 PM          10          XYZ             10  

The first intermediary column contains the first 3 chars of ENV column ( =LEFT(A9,3) ), while the second intermediary column contains the month ( =MID(A9,7,2) ). This works only if your ENV records are fixed size and homogeneous (eg your env name has exactly 3 chars).

With this layout, you can compute the average putting in any cell the following formula:

=AVERAGEIFS(D9:D12, F9:F12,"=ABC", G9:G12, "=10")

Where D9:D12 is the values interval, F9:F12 is the 1st intermediary column and G9:G12 the second intermediary column.

One Shot Compact Solution (Arrays)

An optimized solution can be found relying on arrays . For instance, to calculate the average and the max of an interval based on 2 "vectorial" conditions you can write this one liners:

= MAX(IF((LEFT(A9:A12,3)="ABC")*(MID(A9:A12,7,2)="10"),D9:D12))

= AVERAGE(IF((LEFT(A9:A12,3)="ABC")*(MID(A9:A12,7,2)="10"),D9:D12))

With A9:A12 your original records, and D9:D12 is the values interval.

The advantages of this solution are that you don't need any intermediary column and that you can extend this approach to all the other formulas that don't have ' xxxxxIFS ' (it's the case for MAX ).

NOTE : you have to confirm this formula with CTRL + SHIFT + RETURN or your formula will fail with #VALUE error.

Live Demo

在此处输入图片说明

Live demo available here .

You can start by spiting column A into a date and letters using - Data > Text to Columns with the delimiter " - ". after you have the new two columns (let say F and G) you can use the function "AVERAGEIF" with a condition that check is the value of the cell in "F" is ABC and the Moth(cell in "G") = 10. as for the max, you can do the same with MAX(IF....) for column E.

SUMPRODUCT will allow you to parse the left-most and date characters from your combined string. A pseudo-MAXIF() can be similarly constructed using MAX() and INDEX() .

In D2 use =SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10))/SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10))

In E2 use =MAX(INDEX((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10),,))

Both SUMPRODUCT and INDEX like to choke on anything remotely resembling an error when parsing text so keep the cell range references to what your actual data is and avoid blanks.

Your results should look like the following.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM