Calculate average for first value(s) after a pre-defined text marker

smide

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




In column B (B2:B600) I have a product list (about 25 different products) and in column E their prices.
In column D I have a list of markers for each product. Markers are S - sold, W - waiting and A - away.


In column G (from cell G2 and downwards) I have list off ALL potential products.


I need a formula to find average of first price occurrence for each product IF the marker for previous occurrence for that product was A (away) and to put those results/averages in Column H.


example.


(for simplicity only for product 'apple')

ABCDEFGH
1
2appleS12apple12.5
3sugar6sugar....
4appleA14orange....
5orange28....
6sugar5
7appleW9
8sugar32
9orange11
10appleA8
11appleS16
12sugar20
13orange3
14sugar19
15appleW9
16sugar5
17........

<tbody>
</tbody>

cell H2 is average value for apple (first price after marker A for previous apple product): =average (E7,E11) = average(9,16) = 12.5
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Here is a monstrous array formula (to be entered using Ctrl+Shift+Enter, not just Enter) that returns the desired result for the posted dataset.
Caveat: "apple A" must not be the last entry for "apple".
Hope somebody can offer a shorter and more elegant solution.

=SUM(IF(INDEX($D$2:$D$600,N(IF(,,SMALL(IF($B$2:$B$600=G2,ROW($B$2:$B$600)-ROW($B$2)+1),ROW(INDIRECT("1:"&COUNTIF($B$2:$B$600,G2)))))))="A",INDEX(INDEX($E$2:$E$600,N(IF(,,SMALL(IF($B$2:$B$600=G2,ROW($B$2:$B$600)-ROW($B$2)+1),ROW(INDIRECT("1:"&COUNTIF($B$2:$B$600,G2))))))),N(IF(,,ROW(INDIRECT("1:"&COUNTIF($B$2:$B$600,G2)))+1)))))/COUNTIFS($B$2:$B$600,G2,$D$2:$D$600,"A")
 
Upvote 0
If I am understanding correctly, the cells to be averaged in your example would be E7 and E11.
Would it be possible for you to have data as shown below (only change I have made is D2 from "S" to "A")?
If that is possible, should cell E14 also be included in the average, even though its own marker is "A"?


Book1
BCDE
2appleA12
3sugar6
4appleA14
5orange28
6sugar5
7appleW9
8sugar32
9orange11
10appleA8
11appleS16
12sugar20
13orange3
14sugar19
15appleW9
16sugar5
Sheet1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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