Find the mean and median based on criteria

Goddess

Board Regular
Joined
Dec 3, 2015
Messages
112
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I have a set of data where I need to find the mean and median, based on certain criteria.

FruitYearPriceFruitYearMeanMedianOverall
Apple11/4/202220Apple2015
Pear23/3/2015352016FruitMeanMedian
Grape9/12/2019612017Apple
Apple19/7/2021212018Grape
Apple20/9/2021852019Pear
Pear26/3/2018622020
Apple21/3/2016312021
Pear20/6/2016112022
Grape3/4/201725Grape2015
Grape11/9/2017852016
Grape14/11/2016642017
Pear24/9/2018122018
Pear5/8/2019582019
Pear28/7/2022872020
Apple5/5/2020422021
Pear6/12/2021652022
Grape22/2/202265Pear2015
2016
2017
2018
2019
2020
2021
2022


Basically, I need to find the mean and median price for "Apple", "Pear", and "Grape" based on the year purchased, and then the overall for all 3 fruits (cells in yellow). Can anyone help with the formula? Thanks!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi, would this get you where you need to be ?

Book1
ABCDEFGHIJKL
1
2FruitYearPriceFruitYearMeanMedianOverall
3Apple11/04/202220Apple201500
4Pear23/03/20153520163131FruitMeanMedian
5Grape09/12/201961201700Apple36.536.5
6Apple19/07/202121201800Grape61.111111164
7Apple20/09/202185201900Pear48.833333347.5
8Pear26/03/20186220204242
9Apple21/03/20163120215353
10Pear20/06/20161120222020
11Grape03/04/201725Grape201500
12Grape11/09/20178520166464
13Grape14/11/201664201758.3333365
14Pear24/09/201812201800
15Pear05/08/20195820196161
16Pear28/07/202287202000
17Apple05/05/202042202100
18Pear06/12/202165202200
19Grape22/02/201765Pear20153535
2020161111
21201700
2220183737
2320195858
24202000
2520216565
2620228787
Sheet1
Cell Formulas
RangeFormula
G3:G10G3=IFERROR(SUM(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19))),0)
H3:H10H3=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19))),0)
G11:G18G11=IFERROR(SUM(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19))),0)
H11:H18H11=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19))),0)
G19:G26G19=IFERROR(SUM(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19))),0)
H19:H26H19=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19))),0)
K5K5=SUM(G3:G10)/COUNTIF(G3:G10,">0")
L5L5=MEDIAN(FILTER(H3:H10,H3:H10>0))
K6K6=SUM(G11:G18)/COUNTIF(G11:G18,">0")
L6L6=MEDIAN(FILTER(H11:H18,H11:H18>0))
K7K7=SUM(G19:G26)/COUNTIF(G19:G26,">0")
L7L7=MEDIAN(FILTER(H19:H26,H19:H26>0))
 
Upvote 0
apologies, I saw you have Office 2019, so the FILTER formula in Overal medians won't work for you, so heres an alternative for those formulas :

Book1
ABCDEFGHIJKL
1
2FruitYearPriceFruitYearMeanMedianOverall
3Apple11/04/202220Apple201500
4Pear23/03/20153520163131FruitMeanMedian
5Grape09/12/201961201700Apple36.536.5
6Apple19/07/202121201800Grape61.111111164
7Apple20/09/202185201900Pear48.833333347.5
8Pear26/03/20186220204242
9Apple21/03/20163120215353
10Pear20/06/20161120222020
11Grape03/04/201725Grape201500
12Grape11/09/20178520166464
13Grape14/11/201664201758.3333365
14Pear24/09/201812201800
15Pear05/08/20195820196161
16Pear28/07/202287202000
17Apple05/05/202042202100
18Pear06/12/202165202200
19Grape22/02/201765Pear20153535
2020161111
21201700
2220183737
2320195858
24202000
2520216565
2620228787
Sheet1
Cell Formulas
RangeFormula
G3:G10G3=IFERROR(SUM(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19))),0)
H3:H10H3=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F3,IF(A$3:A$19=E$3,C$3:C$19))),0)
G11:G18G11=IFERROR(SUM(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19))),0)
H11:H18H11=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F11,IF(A$3:A$19=E$11,C$3:C$19))),0)
G19:G26G19=IFERROR(SUM(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19)))/COUNT(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19))),0)
H19:H26H19=IFERROR(MEDIAN(IF(YEAR(B$3:B$19)=F19,IF(A$3:A$19=E$19,C$3:C$19))),0)
K5K5=SUM(G3:G10)/COUNTIF(G3:G10,">0")
L5L5=MEDIAN((INDEX(IF(H3:H10<>0,H3:H10,""),0)))
K6K6=SUM(G11:G18)/COUNTIF(G11:G18,">0")
L6L6=MEDIAN((INDEX(IF(H11:H18<>0,H11:H18,""),0)))
K7K7=SUM(G19:G26)/COUNTIF(G19:G26,">0")
L7L7=MEDIAN((INDEX(IF(H19:H26<>0,H19:H26,""),0)))
 
Upvote 0
Solution
Hi Rob,

Thanks so much. For a moment it didn't work, until I realised I need to click shift+ctrl+enter for the array formula to work! :)

Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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