Best way to calculate multiple averages? (See sample mini-sheet)

wrecclesham

Board Regular
Joined
Jul 24, 2019
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Book1
BCD
2Average
3Apples65
4Apples62
5Apples1647.7
6Oranges17
7Oranges6139.0
8Peaches17
9Peaches72
10Peaches24
11Peaches6243.8
12Tomatoes9696.0
Sheet1
Cell Formulas
RangeFormula
D5D5=AVERAGE(C3:C5)
D7D7=AVERAGE(C6:C7)
D11D11=AVERAGE(C8:C11)
D12D12=AVERAGE(C12)

I have a similar spreadsheet to the one above, containing several thousand rows of data.

How could I calculate the green column automatically?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Excel Formula:
=IF(COUNTIF(B$2:B3,B2)=COUNTIF(B$2:B2,B2),AVARAGEIFS($C$2:$C$12,$B$2:$B$12,B2),"")
 
Upvote 0
Solution
Excel Formula:
=IF(COUNTIF(B$2:B3,B2)=COUNTIF(B$2:B2,B2),AVARAGEIFS($C$2:$C$12,$B$2:$B$12,B2),"")

Sorry for the dumb question but where would I actually use that formula?

I get a "#NAME?" error when I paste it into a blank cell.
 
Upvote 0
Flashbond's formula contained a typo (incorrect spelling of AVERAGEIFS). You could simply correct their formula, or use an alternative (of which there are many).
Book1
BCD
2Average
3Apples65 
4Apples62 
5Apples1647.66667
6Oranges17 
7Oranges6139
8Peaches17 
9Peaches72 
10Peaches24 
11Peaches6243.75
12Tomatoes9696
Sheet1
Cell Formulas
RangeFormula
D3:D12D3=IF(B3=B4,"",AVERAGEIFS(C$3:C3,B$3:B3,B3))
 
Upvote 0
Sorry for the dumb question but where would I actually use that formula?

I get a "#NAME?" error when I paste it into a blank cell.
I misspelled AVARAGE. It must be AVERAGE.
Anyway, use @jasonb75 's solution. Much more practical. I am also so dumb sometimes.
 
Upvote 0
Flashbond's formula contained a typo (incorrect spelling of AVERAGEIFS). You could simply correct their formula, or use an alternative (of which there are many).
Book1
BCD
2Average
3Apples65 
4Apples62 
5Apples1647.66667
6Oranges17 
7Oranges6139
8Peaches17 
9Peaches72 
10Peaches24 
11Peaches6243.75
12Tomatoes9696
Sheet1
Cell Formulas
RangeFormula
D3:D12D3=IF(B3=B4,"",AVERAGEIFS(C$3:C3,B$3:B3,B3))
Thanks everyone. I didn't realize it was just a typo.

It works perfectly now, except I get a "#DIV/0!" error for the first group of values that are being averaged (should be 47.67 for Tomatoes).

Any idea how I can modify the code so it can handle the first item correctly?
 
Upvote 0
Anyway, use @jasonb75 's solution. Much more practical. I am also so dumb sometimes.
We've all had days when you feel like that (I had a seriously dumb moment in a thread last night :oops: ).

@wrecclesham noting that you have 'several thousand' rows of real data, you might find that this method works more efficiently.
You will need to pay close attention to the position of the cells used in the formula and the $ symbols for the mix of absolute and relative references.
If you are using an up to date version of excel (it would help us if you added your excel version to your forum profile by clicking the 'Account Details' link in my signature at the bottom of this post) then I can make a dynamic version of it using the LET function for you so that you don't need to change it if you add more data.
Book1
BCD
2Average
3Apples65 
4Apples62 
5Apples1647.66667
6Oranges17 
7Oranges6139
8Peaches17 
9Peaches72 
10Peaches24 
11Peaches6243.75
12Tomatoes9696
Sheet1
Cell Formulas
RangeFormula
D3:D12D3=IF(B3=B4,"",AVERAGE(C3:INDEX($C$2:$C$12,IFERROR(MATCH(1E+100,D$2:D2)+1,1))))
 
Upvote 0
We've all had days when you feel like that (I had a seriously dumb moment in a thread last night :oops: ).

@wrecclesham noting that you have 'several thousand' rows of real data, you might find that this method works more efficiently.
You will need to pay close attention to the position of the cells used in the formula and the $ symbols for the mix of absolute and relative references.
If you are using an up to date version of excel (it would help us if you added your excel version to your forum profile by clicking the 'Account Details' link in my signature at the bottom of this post) then I can make a simple dynamic version of it using the LET function for you.
Book1
BCD
2Average
3Apples65 
4Apples62 
5Apples1647.66667
6Oranges17 
7Oranges6139
8Peaches17 
9Peaches72 
10Peaches24 
11Peaches6243.75
12Tomatoes9696
Sheet1
Cell Formulas
RangeFormula
D3:D12D3=IF(B3=B4,"",AVERAGE(C3:INDEX($C$2:$C$12,IFERROR(MATCH(1E+100,D$2:D2)+1,1))))
365 all day, every day!
 
Upvote 0
You could try Data Subtotal

T202211b.xlsm
AB
1
2ProductAmount
6Apples Average47.67
9Oranges Average39.00
14Peaches Average43.75
16Tomatoes Average96.00
17Grand Average49.20
18
7b
Cell Formulas
RangeFormula
B6B6=SUBTOTAL(1,B3:B5)
B9B9=SUBTOTAL(1,B7:B8)
B14B14=SUBTOTAL(1,B10:B13)
B16B16=SUBTOTAL(1,B15:B15)
B17B17=SUBTOTAL(1,B3:B15)
 
Upvote 0
I think that I was a little too slow in removing the word 'simple' from the offer of making it dynamic, the formula is much the same as the one in my last reply but you won't need to edit it if you add more data to the source, just fill down as needed.

Whilst the formula looks more complicated, it uses simpler and more efficient methods to process the data. You may, or may not notice a difference in calculation time for the formula when compare to one using averageifs depending on a number of factors.

One thing that I should point out is that all of the suggestions provided assume that (for example) all Apples will be in consecutive rows, if they are separated then each group of Apples will have its own average.

Excel Formula:
=IF(B3=B4,"",LET(l,MATCH("zzz",B:B),r,C$2:INDEX(C:C,l),AVERAGE(C3:INDEX(r,IFERROR(MATCH(1E+100,D$2:D2)+1,1)))))
B3=B4 should refer to the item of interest (e.g. Apples) in the same row as the formula, and the row below.
B:B should refer to the entire column of items.
C$2 should refer to the cell immediately above the first value in the column of values to average.
C:C should refer to the entire column of values.
C3 should refer to the value cell in the same row as the formula.
D$2:D2 should refer to the cell above the one where you enter the first formula before filling down.

Hopefully that all makes sense.
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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