Sumif but only last n occurrences

PBingo

New Member
Joined
Aug 19, 2022
Messages
5
Office Version
  1. 2007
Platform
  1. Windows
Columns A-H represents a list of soccer results in date order, showing the full time home and away goals for the respective teams. Cell H2 allows a team to be selected whilst H1 inputs the number of games, counting up from the bottom that you want to sum the goals scored by the selected team in their last n games, either home or away. H3 is the calculation. In the example shown, Man City scored a total of 4 goals (1+3) in the last 2 games they played.

What I wanted was a way to have the value in H3 displayed for the home and away teams in each row, rather than having to type a single team selection in H2. I eventually managed adapt the formula from H3 to do that in columns J and K. Hence cell J45 shows the same 4 goal total from Man City’s last 2 games as cell H3. However, what appears to happen is that all of Man City’s entries in columns J and K change to the same value (4) when their latest match is added to the spreadsheet. Obviously this happens for all the teams not just Man City. The last value for a team (the one closest to the bottom of the sheet) is correct but all other instances will be wrong. For a comparison I have added columns M and N which show what the correct goal totals should be for each teams last 2 games progressing down the sheet in date order. The last 10 games in J-K match the totals in M-N.

Is it possible to change the formula in J-K so that when a new row of results is added it only updates the latest goal total for a particular team and not all previous values? I have tried many variations but with no success. Any assistance would be greatly appreciated.

Book2.xlsx
ABCDEFGHIJKLMN
1DateHomeTeamAwayTeamFTHGFTAGNo. Of Games2Total goals last n games from formulaTotal goals last 2 games actual
231/01/2018ChelseaBournemouth03Selected TeamBrighton1303
331/01/2018EvertonLeicester21Total Goals61221
431/01/2018Man CityWest Brom304130
531/01/2018NewcastleBurnley112311
631/01/2018SouthamptonBrighton111611
703/02/2018ArsenalEverton511151
803/02/2018BournemouthStoke213121
903/02/2018BrightonWest Ham316231
1003/02/2018BurnleyMan City113424
1103/02/2018LeicesterSwansea112521
1203/02/2018Man UnitedHuddersfield205220
1303/02/2018West BromSouthampton231124
1404/02/2018Crystal PalaceNewcastle112220
1504/02/2018LiverpoolTottenham226322
1605/02/2018WatfordChelsea412141
1710/02/2018EvertonCrystal Palace311242
1810/02/2018Man CityLeicester514262
1910/02/2018StokeBrighton111624
2010/02/2018SwanseaBurnley105321
2110/02/2018TottenhamArsenal103135
2210/02/2018West HamWatford202234
2311/02/2018HuddersfieldBournemouth412343
2411/02/2018NewcastleMan United102522
2511/02/2018SouthamptonLiverpool021634
2612/02/2018ChelseaWest Brom301142
2724/02/2018BournemouthNewcastle223233
2824/02/2018BrightonSwansea416552
2924/02/2018BurnleySouthampton113111
3024/02/2018LeicesterStoke112122
3124/02/2018LiverpoolWest Ham416263
3224/02/2018WatfordEverton102113
3324/02/2018West BromHuddersfield121216
3425/02/2018Crystal PalaceTottenham012312
3525/02/2018Man UnitedChelsea215124
3601/03/2018ArsenalMan City031408
3703/03/2018BurnleyEverton213131
3803/03/2018LeicesterBournemouth112323
3903/03/2018LiverpoolNewcastle206262
4003/03/2018SouthamptonStoke001111
4103/03/2018SwanseaWest Ham415252
4203/03/2018TottenhamHuddersfield203232
4303/03/2018WatfordWest Brom102121
4404/03/2018BrightonArsenal216161
4504/03/2018Man CityChelsea104141
4605/03/2018Crystal PalaceMan United232525
Sheet1
Cell Formulas
RangeFormula
J2:K46J2=SUM(IF(ROW($A$2:$A$46)>=LARGE(IF($B$2:$C$46=B2,ROW($A$2:$A$46)),MIN($H$1,COUNTIFS($B$2:$C$46,B2))),IF($B$2:$C$46=B2,$D$2:$E$46)))
H3H3=SUM(IF(ROW($A$2:$A$46)>=LARGE(IF($B$2:$C$46=H$2,ROW($A$2:$A$46)),MIN($H$1,COUNTIFS($B$2:$C$46,H$2))),IF($B$2:$C$46=H$2,$D$2:$E$46)))
Press CTRL+SHIFT+ENTER to enter array formulas.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
Excel Formula:
=SUM(IF(ROW($A$2:$A2)>=LARGE(IF($B$2:$C2=B2,ROW($A$2:$A2)),MIN($H$1,COUNTIFS($B$2:$C2,B2))),IF($B$2:$C2=B2,$D$2:$E2)))
 
Upvote 0
How about
Excel Formula:
=SUM(IF(ROW($A$2:$A2)>=LARGE(IF($B$2:$C2=B2,ROW($A$2:$A2)),MIN($H$1,COUNTIFS($B$2:$C2,B2))),IF($B$2:$C2=B2,$D$2:$E2)))
Thanks Fluff

Looks like I was close but no cigar whereas you were bang on the money! I probably would have been at it for another month without seeing that. I know what I am going to be doing this weekend now.

Once again, many thanks.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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