Home » excel » excel – averaging every nth rows and excluding values

excel – averaging every nth rows and excluding values

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to average every 5 rows but also to exclude in the average values that are less than 50. This is the command to average every 5 rows.

=AVERAGE(OFFSET($L$3,(ROW()-ROW($P$2))*5,,5))

This is the command to exclude values less than 50

=AVERAGEIF(L3:L8,">50")

How do I combine those two in one command?

How to&Answers:

Thanks to a colleague of mine, the following works like a gem.

=IFERROR(AVERAGEIF(OFFSET($L$3,(ROW()-ROW($P$2))*5,,5),">50"),0)

Answer:

As long as you have Excel 2016, a SUMPRODUCT formula will work.

=SUMPRODUCT((MOD(ROW($A$1:$A100),5)=0)*($A$1:$A$100<50)*$A$1:$A$100)/SUMPRODUCT((MOD(ROW($A$1:$A100),5)=0)*($A$1:$A$100<50)*1)

I assumed your data was in A1:A100 so update that as needed. And in the MOD formula, I used 5 for ever 5th row. If you need to change that, change the 5 in MOD formulas. Lastly, the formula does not include the value 50 since you stated you wanted less than 50.

Answer:

‘Tiny’ Differences

Correction

=IFERROR(AVERAGEIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">50"),0)

Visualize

Let’s first visualize what you’re actually doing.

enter image description here

For every five rows in column L you are displaying the average of the values, if they are greater than or equal to 50. (in this example) in column G:

For L3:L8 in G2,
for L9:L13 in G3,
for L14:L18 in G4 etc.

Issues

  • The 1st issue is that the formula is written exclusively for the
    2nd row. If you want the first result to be displayed in the first row, the formula will result in a REF! error.

    If you want to display the first result in the 1st row you have to
    change L$2 to L$1:

    =IFERROR(AVERAGEIF(OFFSET(L$3,(ROW()-ROW(L$1))*5,,5),">50"),0)
    

    or for the 3rd row you have to change L$2 to L$3:

    =IFERROR(AVERAGEIF(OFFSET(L$3,(ROW()-ROW(L$3))*5,,5),">50"),0)
    
  • The 2nd issue is that you are doing something in column L and for
    no obvious reason you are using column P in your formula. You could
    have used any column Z, AN or CG, but your doing stuff in
    column L, so use L.

  • The 3rd issue is that you have locked the columns $L which means where
    ever you put the formula in a single row, the result will be the same. If
    you don’t lock them, you can copy the formula e.g. to the right and
    it will display the results for columns M, N, O etc.:

    enter image description here

Other Formulas

=SUM(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5))
=COUNT(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5))
=AVERAGE(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5))
=SUMIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">50")
=COUNTIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">50")

AVERAGEIF is available in Excel from version 2007, but for older versions the following formula can be used instead:

=IF(COUNTIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">"&50)=0,0,SUMIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">"&50)/COUNTIF(OFFSET(L$3,(ROW()-ROW(L$2))*5,,5),">"&50))

It first checks if COUNTIF results in 0. If it does it displays 0, otherwise it divides SUMIF with COUNTIF.