Home » excel » math – Subtotal average, of data linked to a particular month in Excel 2016

# math – Subtotal average, of data linked to a particular month in Excel 2016

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a large spreadsheet and I am struggling to get an average, no formula I have used thus far from google or otherwise, has worked.

The average is an average of time, in particular, hours between a start and end time, converted into days.

• In column E9:E1833, I have start time as dd/mm/yyy hh:mm
• In column J9:J1833, I have the time in days, from start to finish as [h]:mm (some are empty)
• I have an auto filter setup to change with job type / area / status etc, and the result needs to change as data is filtered.

The following formula works, but does not recalculate:

`{=AVERAGE(IF(MONTH(E9:E1833)=1,(J9:J1833)))}`

The following formula works, but gives an incorrect result:

`=AVERAGE(IF(MONTH(E1833)=1,SUBTOTAL(101,(J9:J1833))))`

I have tried Offset, with similar results, and many of the “off-the-shelf” solutions produces `#Div/0`, `#REF`, `#Value` or fail to allow the entry altogether.

Any help would be much appreciated, preferably not in VBA, as I don’t want to be lumbered with this spreadsheet forever. Thanks in advance.

How to&Answers:

Since you are going to be filtering, and want the result to change as you filter, just use the `AGGREGATE` function:

``````=AGGREGATE(1,3,J:J)
``````

EDIT: Animation added in response to question

The values in the Values column can be times, although you may have to format the result of the `AGGREGATE` function (cell/numberformat) to show the result as `[h]:mm`

Note: GIF will play twice. To repeat, refresh the page ### Answer：

The following formula seems to work.

``````{=AVERAGE(IF(SUBTOTAL(9,(OFFSET(J9:J1833,ROW(J9:J1833)-MIN(ROW(J9:J1833)),0,1)))>0,(IF(MONTH(E9:E1833)=3,SUBTOTAL(9,(OFFSET(J9:J1833,ROW(J9:J1833)-MIN(ROW(J9:J1833)),0,1)))))))}
``````

The `SUBTOTAL(9,OFFSET` part of the function returns an array where filtered numbers are 0, and visible numbers return their values. If you just averaged that array, you would get the wrong number because you would be averaging all those zeros. So the `IF` statement just creates an array that returns a `FALSE` for zero, which is not averaged.

An additional caution that gave me a headache as I was playing around, was you are also filtering on `MONTH()=1`. The issue with that is, blank cells return a January date, which means they will not be filtered by `MONTH()=1`