Formula for AVERAGEIF please

thegreengrocer

New Member
Joined
Mar 2, 2010
Messages
15
I have a huge amount of data (on the left) from which i would like to extract the averages to the table on the right.

the formula to go in K4 would give me the average of the Buy1 numbers in column B:B, but only if these numbers had the ID of '3' in column A:A. the formula would want to ignore 0 if possible.

many thanks in advance.

Excel Workbook
ABCDEFGHIJKLMNOP
1DataTable
2
3IDBuy1Buy2Buy3Buy4Buy5Buy6IDBuy1Buy2Buy3Buy4Buy5Buy6
437474710003
536976750004
637177690005
746970720006
8469710000
94717874000
10467750000
115697177000
126736972000
136747572000
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I have a huge amount of data (on the left) from which i would like to extract the averages to the table on the right.

the formula to go in K4 would give me the average of the Buy1 numbers in column B:B, but only if these numbers had the ID of '3' in column A:A. the formula would want to ignore 0 if possible.

many thanks in advance.

Excel Workbook
ABCDEFGHIJKLMNOP
1DataTable
2
3IDBuy1Buy2Buy3Buy4Buy5Buy6IDBuy1Buy2Buy3Buy4Buy5Buy6
437474710003
536976750004
637177690005
746970720006
8469710000
94717874000
10467750000
115697177000
126736972000
136747572000
Sheet1
If you are on Excel 2007 or later...

K4, copy across and down:
Code:
=AVERAGEIFS(
   INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0)),
   INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0)),">0",
   $A$3:$A$13,$J4)

On all systems...

Control+shift+enter, not just enter, copy across, and down:
Code:
=AVERAGE(
    IF($A$3:$A$13=$J4,
    IF(ISNUMBER(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))),
    IF(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0)) > 0,
      INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))))))
 
Upvote 0
hi, many thanks for your response, whilst i am most appreciative i have to tell you that formula is just returning the average of 71.111 which is the average for the whole column. the answer in K4 should return 71.333. i am using excel 2000 if this affects things, sorry to be a pain but is there any chance you could check the formula please as we appear to be nearly there? thank's once again.
 
Upvote 0
hi, many thanks for your response, whilst i am most appreciative i have to tell you that formula is just returning the average of 71.111 which is the average for the whole column. the answer in K4 should return 71.333. i am using excel 2000 if this affects things, sorry to be a pain but is there any chance you could check the formula please as we appear to be nearly there? thank's once again.

Oops. A mistake in range spec... It should be:
Rich (BB code):
=AVERAGE(IF($A$4:$A$13=$J4, 
   IF(ISNUMBER(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))),
   IF(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0)) > 0,
    INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))))))

Another way of expressing the same calc would be...
Rich (BB code):
=AVERAGE(
    IF($A$4:$A$13=J4,
    IF($B$3:$G$3=K$3,
    IF(ISNUMBER($B$4:$G$13),
    IF($B$4:$G$13 > 0,
      $B$4:$G$13)))))
 
Upvote 0
thank you very much for your time thats works a treat now. just as a matter of interest where a zero occurs in the left table the formula returns #DIV/0!, is there any way to make it return a zero or blank in this instance. no probs if not, thanks again, very pleased.
 
Upvote 0
thank you very much for your time thats works a treat now.

You are welcome.

just as a matter of interest where a zero occurs in the left table the formula returns #DIV/0!, is there any way to make it return a zero or blank in this instance. no probs if not, thanks again, very pleased.

Control+shift+enter, not just enter:
Rich (BB code):
=LOOKUP(9.99999999999999E+307,CHOOSE({1,2},0,
   AVERAGE(IF($A$4:$A$13=$J4, 
    IF(ISNUMBER(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))),
    IF(INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0)) > 0,
     INDEX($B$4:$G$13,0,MATCH(K$3,$B$3:$G$3,0))))))))

This will return a 0 in lieu of the #DIV/0! error.
 
Upvote 0
hi again, added more rows of data to the table on the left this morning, then went into your formula to try to expand the cell range (instead of 13 rows it now has 2000). i simply tried changing all the 13's within the formula to 2000 but this will not work. as the formula reverts to just giving the average of the whole column rather than the average for just a given ID. would you mind at all suggesting which numbers in the formula to change in order to expand the cell range to 2000+ rows? thanks once again.
 
Upvote 0
hi again, added more rows of data to the table on the left this morning, then went into your formula to try to expand the cell range (instead of 13 rows it now has 2000). i simply tried changing all the 13's within the formula to 2000 but this will not work. as the formula reverts to just giving the average of the whole column rather than the average for just a given ID. would you mind at all suggesting which numbers in the formula to change in order to expand the cell range to 2000+ rows? thanks once again.

Control+shift+enter, not just enter...
Rich (BB code):
=LOOKUP(9.99999999999999E+307,CHOOSE({1,2},0,
   AVERAGE(IF($A$4:$A$2000=$J4, 
    IF(ISNUMBER(INDEX($B$4:$G$2000,0,MATCH(K$3,$B$3:$G$3,0))),
    IF(INDEX($B$4:$G$2000,0,MATCH(K$3,$B$3:$G$3,0)) > 0,
     INDEX($B$4:$G$2000,0,MATCH(K$3,$B$3:$G$3,0))))))))

Or we can the refences to the data area dynamic, such that the formula always works with the actual data...

Define BigNum as referring to:
Rich (BB code):
=9.99999999999999E+307

Define Lrow by means of Insert | Name | Define as referring to:
Rich (BB code):
=MATCH(BigNum,Sheet1!$A:$A)

Note that the data is assumed to be on Sheet1. Adjust to suit if needed.

Define Data as referring to:
Rich (BB code):
=Sheet1!$A$3:INDEX(Sheet1!$G:$G,Lrow)

Note that this defines the data area from column A to G. If the data also change in horizontal direction, we need to tweak the foregoing definition accordingly.

Now, we can modify the essential formula...

Control+shift+enter, not just enter...
Rich (BB code):
=LOOKUP(BigNum,CHOOSE({1,2},0, AVERAGE(IF(INDEX(Data,0,1)=$J4, 
   IF(ISNUMBER(INDEX(Data,0,MATCH(K$3,INDEX(Data,1,0),0))), 
   IF(INDEX(Data,0,MATCH(K$3,INDEX(Data,1,0),0)) > 0, 
    INDEX(Data,0,MATCH(K$3,INDEX(Data,1,0),0))))))))
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top