I am trying to find the average for the last 3 instances only. I am using the AVERAGEIF statement and it will calculate the average for the entire range but I need it to only calculate for that last 3 instances it finds (or less if there is less than 3 available). I need the entire column for G and H to have the average for the last 3 games that the Team played.

This is what I have:

```
=AVERAGEIF(B3:C17,B17,D3:E17)
```

You can do this with array formulas (They have to be entered using the keys `Ctrl`+`Shift`+`Enter`)…

Basic steps are:

- Find the row (including and above current) that is the third highest row number containing the team name (or use row 1 otherwise)
- Use the
`INDIRECT`

ranges in your`AVERAGEIF`

from B-*that_row*to C-*current_row*and D_*that_row*to E-*current_row*

So in cell `F17`

you would have the formula

```
{=AVERAGEIF(INDIRECT("B"&LARGE(IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1),3)&":"&CELL("address",C17)),B17,INDIRECT("D"&LARGE(IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1),3)&":"&CELL("address",E17)))}
```

We repeat some of the logic, because we have two ranges (criteria range and average range).

`IF(--($B$3:B17=B17)+($C$3:C17=B17),ROW($B$3:B17),1)`

means that if column B or (using`+`

) column C has the value of in B17, give me the row number, otherwise 1 (our <3 case… we could make this 3, the first row of team names)`LARGE(...,3)`

will give us the third highest of this array –> the third highest row number having our team name`INDIRECT("B"&...&":"&CELL("address",C17))`

is going to give us the range using our third highest row number to the current row, columns B and C- then we do exactly the same thing as you were doing in
`AVERAGEIF`

but using this`INDIRECT`

range and the equivalent for columns D and E

Fun question! Good luck. And remember to use `Ctrl`+`Shift`+`Enter` to enter it!

**EDIT** The above was giving an `#NUM!`

error for the first two rows – that was because the `LARGE`

function was trying to get the third largest in an array of 2! Also noticed that there were some cases where the column letter needed to be absolute (i.e. `$`

) for copying to the *Away* column. So the updated formula:

```
{=AVERAGEIF(INDIRECT("B"&LARGE(IF(--($B$3:$B17=B17)+($C$3:$C17=B17),ROW($B$3:$B17),1),MIN(3,ROW()-2))&":"&CELL("address",$C17)),B17,INDIRECT("D"&LARGE(IF(--($B$3:$B17=B17)+($C$3:$C17=B17),ROW($B$3:$B17),1),MIN(3,ROW()-2))&":"&CELL("address",$E17)))}
```

- Replaced the
`3`

with`MIN(3,ROW()-2)`

so that we get 3 if there are, but 1 or 2 if we are in one of the first two data rows

### Answer：

OK I posted this prematurely and attempted to delete it when I realised it wouldn’t work. It should work now…. providing you add another condition which is the game dates in column A. Remember that this is an array formula so hit ctrl+shift+enter. Dates in column A; teams in column B; stats in column D. This formula can reside somewhere permanent on the sheet so you can enter the team name (shown as F13 here) to get the three most recent stats.

=AVERAGE(VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),1),A3:D24,4),VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),2),A3:D24,4),VLOOKUP(LARGE(IF(B3:B24=F13,A3:A24),3),A3:D24,4))

Tags: excelexcel, vba