# Moving Average Best 5 of last 6 values

#### jimrward

##### Well-known Member
I have a rows of data which contain variable numbers of columns, some with values some without.

What i need to do is take the average of the best 5 from the last six non zero values, providing there are more than 5 columns, if there are only 5 then just the average.

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

#### big_mac

##### Board Regular
If your information (numbers) begin in cell C2 and extend to column I this formula placed in Cell J2 should meet your needs. other wise I didn't understand your question.

J2:
=SUM(LARGE(C2:I2,{1,2,3,4,5}))/5
it will pick the best 5 and average them

#### Seti

##### Well-known Member
big_mac,

I don't think your solution addresses the need to take the largest 5 from the last 6 non-zero values.

#### eliW

##### Well-known Member
what do you mean by "best 5"? how do you determin best?

#### big_mac

##### Board Regular
Seti,
I do see the error of my calculation.
I am averaging the LARGEST 5
but not of the last 6 non-zero values.
if there are 11 columns displayed as 100,100,100,100,100,50,50,50,50,50,50
the average of the last 6 non-zero should be 50 not 100

eliW,
Forgive my evaluation of LARGE as best, but I normally think Large is best. Please forgive my error in word choice. I have not seen the original poster reply to either of our attempts at a possible solution yet.

#### Oaktree

##### MrExcel MVP
Try:

Code:
``M2 =SUM(LARGE(INDIRECT("R"&ROW()&"C"&LARGE(((A2:L2>0)*COLUMN(A2:L2)),ROW(INDIRECT("1:"&MIN(COUNTIF(A2:L2,">0"),6)))),0),ROW(INDIRECT("1:"&MIN(COUNTIF(A2:L2,">0"),5)))))/MIN(COUNTIF(A2:L2,">0"),5)``

Where A:L is your (largest) range of number inputs.

M2 is an array formula and must be confirmed with CTRL+SHIFT+ENTER instead of just ENTER (doing so correctly will result in Excel putting { }'s around your formula in the formula bar).

Then, copy M2 and paste down through the end of your range.

#### jimrward

##### Well-known Member
for my purposes I require to look at the last 6 non zero values and select the highest 5

so i could have something like (SUM(A1:F1)-MIN(A1:F1))/5 in simple terms if this was fixed range, now if there were 8 values I would require something like (SUM(D1:I1)-MIN(D1:I1))/5 disregarding A1:C1

so the formula needs to be dynamic and if possible take into account zero values so for instance if in the above F1 was zero the starting cell would be C1

#### jimrward

##### Well-known Member
cheers oaktree

thats seems to solve the problem, i was getting all messed up with CELL and OFFSET along with INDIRECT and the like and almost getting there but not quite

#### Domenic

##### MrExcel MVP
To average the 5 largest numbers out of the last 6 non-zero numbers, try the following formula which needs to be confirmed with CONTROL+SHIFT+ENTER, not just ENTER...

=AVERAGE(LARGE(IF(COLUMN(A1:I1)>=LARGE(IF(A1:I1>0,COLUMN(A1:I1)),6),IF(A1:I1>0,A1:I1)),{1,2,3,4,5}))

Hope this helps!

#### Oaktree

##### MrExcel MVP
=AVERAGE(LARGE(IF(COLUMN(A1:I1)>=LARGE(IF(A1:I1>0,COLUMN(A1:I1)),6),IF(A1:I1>0,A1:I1)),{1,2,3,4,5}))

Hi, Domenic. I think this fails (#NUM! error) if there are fewer than 6 non-zero numbers. The "providing there are more than 5 columns, if there are only 5 then just the average" part of the OP's request led me to the beast I posted above in event there were rows where there were fewer than 6 (but >=1) cases...

Replies
12
Views
383
Replies
1
Views
356
Replies
5
Views
132
Replies
4
Views
154
Replies
13
Views
597

1,195,849
Messages
6,011,957
Members
441,657
Latest member
Diupsy

### 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.

### Which adblocker are you using?

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

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