How to find moving average of last 4 datapoints, excluding zero

Carni

New Member
Joined
Jan 10, 2016
Messages
3
Hi all

I've googled this a bunch and found a few responses that come close, but none of them seem to work.

Basically, I have data that looks like this:

(Column BH)
Expected goals during that game
[TABLE="width: 64"]
<tbody>[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0.973543[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0.817328[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0.976807[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0.764169[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0.398993[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD="align: right"]0[/TD]
[/TR]
</tbody>[/TABLE]

On the column immediately to the right, I want the the cell to give me the average of the last 4 datapoints, that aren't zero. So next to the first zero above, I want the value 0.882961771, which is the average of the first 4 datapoints that aren't zero (0.973543, 0.817328, 0.976807, 0.764169).

Then I want to copy the formula down so that the average is only providing me with the last 4 datapoints up to that point. So the cell immediately next to the 2nd non-zero datapoint (0.817328), would average the four latest non-zero datapoints until then, which is (0.976807, 0.764169, 0.398993, + 1more outside of the range above).

Any ideas?
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

With your data in BH2:BH28 ... in cell BI7 you can have the following array formula:

Code:
=IFERROR(AVERAGE(LOOKUP(LARGE(IF((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0))>0,ROW((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0)))),{1,2,3,4}), ROW((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0))), (BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0)))),"")

Then you can copy formula to cell BI11

Hope this will help
 
Upvote 0
Hi,

With your data in BH2:BH28 ... in cell BI7 you can have the following array formula:

Code:
=IFERROR(AVERAGE(LOOKUP(LARGE(IF((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0))>0,ROW((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0)))),{1,2,3,4}), ROW((BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0))), (BH7:OFFSET(BH7,LARGE((BH7:BH28>0)*(ROW(BH7:BH28)),SUM((BH7:BH28>0)*1)-3)-ROW(),0)))),"")

Then you can copy formula to cell BI11

Hope this will help

Works perfectly! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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