Find average of last three occurences (data in two columns)

smide

Board Regular
Joined
Dec 20, 2015
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello.


In columns A (A2:A600) and B (B2:B600) I have products and in columns C (C2:C600) and D (D2:D600) their prices.
When product is in column A his price is in column C (same row) and when product is in column B his price is in column D.


In column E I have a list of all products (in cells E2:E50).


With array forumla in cell F2:
=IF(E2="","",LOOKUP(9.99999999999999E+307,IF($A$2:$A$600=E2,$C$2:$C$600,IF($B$2:$B$600=E2,$D$2:$D$600)))) , I can find last value/price for each product but I need average of last three prices.


example.

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Product list[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD]orange[/TD]
[TD]apple[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]7[/TD]
[TD]orange[/TD]
[TD="align: center"]7.33[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD]tomato[/TD]
[TD]peach[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]14[/TD]
[TD]peach[/TD]
[TD]...[/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]cabbage[/TD]
[TD]orange[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]9[/TD]
[TD]...[/TD]
[TD]...[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]orange[/TD]
[TD]plum[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]apple[/TD]
[TD]peach[/TD]
[TD="align: center"]16[/TD]
[TD="align: center"]11[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD]plum[/TD]
[TD]orange[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"]8[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD]...[/TD]
[TD]....[/TD]
[TD]...[/TD]
[TD]...[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

explanation: Last three orange prices are 8 (D7), 5 (C5), 9 (D4) -- =(8+5+9)/3 = 7.33


*Any solution with helper columns would be fine also.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe like...

Excel 2010
ABCDEFGHI
1Product listAverage*HelperHelper
2orangeapple47orange7.3301
3tomatopeach114peach13.6710
4cabbageorange89tomato1.0011
5orangeplum56cabbage8.0011
6applepeach1611apple10.0011
7plumorange88plum7.0011
8peachplum15711
9peachapple15711
10
Sheet2
Cell Formulas
RangeFormula
F2=IFERROR(SUMPRODUCT(($A$2:$B$600=E2)*($C$2:$D$600)*($H$2:$I$600=1))/MIN(3,COUNTIF($A$2:$B$600,E2)),"")
H2=IF(A2="","",IF(COUNTIF($A2:$B$600,A2)<=3,1,0))
I2=IF(B2="","",IF(COUNTIF($A2:$B$600,B2)<=3,1,0))


Helper columns copy down to row 600 Average column copy down to row 50.
I have assumed that if less than 3 entries for a product then return average for eg 1 or 2 ???
The only obvious potential error I see for what I have done is if you were to have the same product in both A and B of the same row, if that row is the second to last that contains that product!! ?????
Hope that helps.
 
Upvote 0
When my range (A2:B600) contains empty cells for some reason it does not work...


example.
If my range is populated from A2 to B236 (both products and prices) then formula has to be =IFERROR(SUMPRODUCT(($A$2:$B$236=E2)*($C$2:$D$236)*($H$2:$I$236=1))/MIN(3,COUNTIF($A$2:$B$236,E2)),"") , otherwise I'm getting blank cell as result...:confused:


Using excel 2007.
 
Upvote 0
I'm sorry but at the moment I'm not seeing why that should be.
Below is a shot of my test, with some blanks, with some text entry for product and price and with some formulas generating product and price.
All appears good as I see it.
Excel 2010
ABCDEFGHI
1Product listAverage*HelperHelper
2orangeapple47orange4.6701
3tomatopeach114peach11.3310
4cabbageorange89tomato1.0010
5orangeplum56cabbage8.0011
6applepeach1611apple10.0011
7plumorange88plum7.0011
8
9peachapple15711
10
11
12peach81
13
14orange11
15
Sheet2


Please report back if you can add anything and I will do the same.
 
Upvote 0
I have assumed that you have enough data that there will be at least 3 values for each item. If not the formula will error & you would need to advise what result you want in that case.
Like Snakehips I have also assumed that the same product will not appear twice on the same row.

Column G is my helper to produce the column F results. If you did want to do it without a helper column then you can use the much larger formula in column H.

The highlights were just to help me check a couple visually.

Excel Workbook
ABCDEFGH
1Product listAverage3rd Last IndexAverage
2orangeapple47orange10.6666671410.66666667
3tomatopeach114peach121912
4cabbageorange89tomato7.333333327.333333333
5orangeplum56cabbage737
6applepeach1611apple101610
7plumorange88plum151515
8peachplum157
9peachapple157
10orangeapple82
11tomatopeach64
12cabbageorange36
13orangeplum55
14applepeach48
15plumorange59
16peachplum64
17peachapple39
18
19plum18
20orangepeach1514
21plumplum617
22peach10
23applepeach912
24orange8
25tomato15
26cabbage10
27
28apple12
29
Last 3
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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