Moving Average Best 5 of last 6 values

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,877
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
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.
 
=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...

Hi Oaktree!

Thanks for pointing that out. I really appreciate it. I missed it completely. :oops: In this case, maybe my formula can be corrected as follows...

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

or

=AVERAGE(LARGE(IF(COLUMN(A1:I1)>=LARGE(IF(A1:I1>0,COLUMN(A1:I1)),MIN(6,COUNTIF(A1:I1,">0"))),IF(A1:I1>0,A1:I1)),ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A1:I1,">0")))))

Note that the first formula will only return an average when there's at least 5 non-zero numbers, whereas the second one will only return an average when there's at least one non-zero number.

Hope this helps!
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi all

remember this cracking formula you came up with for best 5 out of 6, when there are no values it gives #ref! error, i have a cell which counts the values address is S8, how can i change the formula to include a condition such as
=if( s8>0, arrayformula,0)

{=SUM(LARGE(INDIRECT("R"&ROW()&"C"&LARGE(((D8:Q8>0)*COLUMN(D8:Q8)),ROW(INDIRECT("1:"&MIN(COUNTIF(D8:Q8,">0"),6)))),0),ROW(INDIRECT("1:"&MIN(COUNTIF(D8:Q8,">0"),5)))))/MIN(COUNTIF(D8:Q8,">0"),5)}
 
Upvote 0
Why not just put the array formula in T8 and use:

=IF(S8>0,T8,0) or similar?
 
Upvote 0
that is the route I took in the end, as anything else i tried with the array formula ended in tears due to its overwhelming complexity for my small brain.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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