Calculations on Last (x) Number of rows

ormy28

New Member
Joined
Dec 10, 2007
Messages
20
Office Version
  1. 365
Hi

I have a spreadsheet that contains football scores contained in a table. Columns A and B contain the home and away teams, and C to F show the number of goals scored in each half by both the home and away teams. A little difficult to explain in writing, but hopefully the below makes it a bit clearer!

ABCDEFGHIJKL
1Home TeamAway TeamHome Team 1stHalfAway Team 1stHalfHome Team 2nd HalfAway Team 2nd HalfHome 2nd Half Y/NAway 2ndHalf Y/NTeamLast 5 No of Games (2ndH)Last 5 No of Goals (2ndH)
2BoltonLeicester1021YYBolton37
3CarlisleBolton1101NY
4BoltonHuddersfield2111YY
5NewcastleBolton0100NN
6TorquayBolton1102NY
7BoltonGrimsby1000NN
8BoltonExeter1231YY
9QPRBolton1112YY
Sheet2
Cell Formulas
RangeFormula
G2:H9G2=IF(E2>0,"Y","N")


The figures in columns K and L show:
K: The number of times the team in J2 have scored in the 2nd half, over the last 5 matches.
L: The number of goals the team in J2 have scored in the 2nd half, over the last 5 matches.

I have calculated the figures in columns K and L manually, but would like to know if there is a formula that would calculate these automatically. I assume an array formula is required. I have dug out an old thread from a few years back where I asked something similar, and have adapted that solution to calculate the figure for column L. Although this appears to calculate where both teams have scored in the 2nd half, rather than just the team in J2, I believe it might be a starting point:

{=SUM(((((Table4[[#Data],[Home Team]]=J2)+(Table4[[#Data],[Away Team]]=J2))*ROW(Table4[[#Data],[Home Team]]))>LARGE(((Table4[[#Data],[Home Team]]=J2)+(Table4[[#Data],[Away Team]]=J2))*ROW(Table4[[#Data],[Home Team]]),6))*((Table4[[#Data],[Home Team 2nd Half]]>0)*(Table4[[#Data],[Away Team 2nd Half]]>0)))}

I believe the first part in the non-bold text might be correct, but am having difficulty with the last section in bold. Any help would be greatly appreciated.

Thanks.
 
All the responses so far have been very interesting, and an education for someone who isn't great with arrays.
But do they all work? You will have seen some discussion among the helpers about certain functions being available or not available to some people. It would be useful to know whether they are available to you. Reason being, those people making suggestions involving, for example, the TAKE function may not wish to spend their time developing suggestions for your follow-up question(s) if you do not have access to that function.

You will also have seen suggestions just for Bolton and other suggestions for a list of teams from your table. It would be helpful to know which of those circumstances you actually want.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi again. Apologies for the delay, an insanely busy week means I have only just been able to get back to this.

Thank you again for your replies. Although I didn't originally ask, the methods of listing each individual team are very useful and will save time.

I do have access to the TAKE functions, so I have been able to use these ok. All the suggestions from jasonb75, Eric W, cmowla and Peter_SSs work for the last 5 matches overall.

However, I perhaps should have been clearer in my original question... I need to see the number of matches and the number of goals scored in each individual teams last 5 matches. Therefore, Leicester would return 1 in column K and 1 in column L.

bebo021999 solution appears to do this. If there is a way to tweak this that would also show the number of goals conceded by each team in their last 5 matches, that would be great.

Thanks.
 
Upvote 0
Would you have the entire league in one table then? I think the others have all based their suggestions on that assumption. I was working on the basis of the table having all games for a specific team in line with the example.
 
Upvote 0
It isn't pretty, but it gets the job done. (I thought it would be easiest to make a "new table" in columns N - P to start out with which helps to get the last n games for each team. From there, it was simply getting indexes of the new table!)
  • Must carry down formulas in columns J - M.
  • Allows you to change the # of games to see the stats for in H2.
  • Ignore columns N - P, but the formula/table needs to be there!
Good Dynamic Array Fomrula Exercises.xlsx
ABCDEFGHIJKLMNOP
1Home TeamAway TeamHome Team 1st HalfAway Team 1st HalfHome Team 2nd HalfAway Team 2nd HalfLook at the last (input below) number of games.TeamLast 5 Games (2nd Half)Last 5 No of Goals (2nd Half)Last 5 Games Conceded (2nd Half)Last 5 No of Goals Conceded (2nd Half)Team2nd HalfIndex
2BoltonLeicester10215Bolton2434Torquay228
3QPRBolton1101Carlisle2623Torquay112
4BoltonHuddersfield2111Exeter1113Torquay210
5NewcastleBolton0100Grimsby0000QPR118
6TorquayBolton1121Huddersfield3533QPR04
7TorquayHuddersfield0113Leicester3435Newcastle126
8BoltonGrimsby1000Newcastle1100Newcastle08
9BoltonExeter1231QPR1123Leicester232
10QPRBolton1112Torquay3537Leicester121
11BoltonLeicester1021    Leicester13
12CarlisleBolton1151Huddersfield125
13BoltonHuddersfield2111Huddersfield313
14NewcastleBolton0110Huddersfield17
15TorquayBolton1123Grimsby031
16BoltonGrimsby1000Grimsby015
17LeicesterCarlisle1021Exeter035
18BoltonExeter1200Exeter117
19Carlisle133
20Carlisle522
21Bolton034
22Bolton030
23Bolton329
24Bolton027
25Bolton124
26Bolton123
27Bolton220
28Bolton219
29Bolton316
30Bolton014
31Bolton111
32Bolton09
33Bolton16
34Bolton15
35Bolton22
Condensed 2
Cell Formulas
RangeFormula
J1J1="Last "&H2&" Games (2nd Half)"
K1K1="Last "&H2&" No of Goals (2nd Half)"
L1L1="Last "&H2&" Games Conceded (2nd Half)"
M1M1="Last "&H2&" No of Goals Conceded (2nd Half)"
I2:I10I2=UNIQUE(SORT(VSTACK(Table4[Home Team],Table4[Away Team])))
J2:J11J2=IF(I2<>"",SUM(IF(INDEX($N$2#,TAKE(TEXTSPLIT(TEXTJOIN(",",1,IF($N$2#=I2,SEQUENCE(ROWS($N$2#)),"")),","),,$H$2),2)<>0,1,0)),"")
K2:K11K2=IF(I2<>"",SUM(INDEX($N$2#,TAKE(TEXTSPLIT(TEXTJOIN(",",1,IF($N$2#=I2,SEQUENCE(ROWS($N$2#)),"")),","),,$H$2),2)),"")
L2:L11L2=IF(I2<>"",LET(i,INDEX($N$2#,TRANSPOSE(TEXTSPLIT(TEXTJOIN(",",,TAKE(TEXTSPLIT(TEXTJOIN(",",,IF($N$2#=I2,SEQUENCE(ROWS($N$2#)),"")),","),,$H$2)),",")),3),SUM(IF(INDEX($N$2#,TEXTSPLIT(TEXTJOIN(",",,IF(INDEX($N$2#,,3)=TRANSPOSE(IF(MOD(i,2)=0,i+1,i-1)),SEQUENCE(ROWS(INDEX($N$2#,,1))),"")),","),2)>0,1,0))),"")
M2:M11M2=IF(I2<>"",LET(i,INDEX($N$2#,TRANSPOSE(TEXTSPLIT(TEXTJOIN(",",,TAKE(TEXTSPLIT(TEXTJOIN(",",,IF($N$2#=I2,SEQUENCE(ROWS($N$2#)),"")),","),,$H$2)),",")),3),SUM(INDEX($N$2#,TEXTSPLIT(TEXTJOIN(",",,IF(INDEX($N$2#,,3)=TRANSPOSE(IF(MOD(i,2)=0,i+1,i-1)),SEQUENCE(ROWS(INDEX($N$2#,,1))),"")),","),2))),"")
N2:P35N2=LET(ht,Table4[Home Team],SORT(HSTACK(VSTACK(ht,Table4[Away Team]),VSTACK(Table4[Home Team 2nd Half],Table4[Away Team 2nd Half]),VSTACK(2*(ROW(ht)-1),2*(ROW(ht)-1)+1)),{1,3},-1))
Dynamic array formulas.
 
Upvote 0
See if these do what you want (check the table name)
In column M - Goals conceded in last (up to) 5 games - I have used a different notation which makes the formula considerably shorter, but perhaps harder to read/understand. If it does what you want but wanted the fuller version, M2 would be
Excel Formula:
=LET(z,IF(Table5[Home Team]=J2,Table5[Away Team 1stHalf]+Table5[Away Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Home Team 1stHalf]+Table5[Home Team 2nd Half],"")),SUM(TAKE(FILTER(z,z<>""),-5)))

ormy28.xlsm
ABCDEFIJKLM
1Home TeamAway TeamHome Team 1stHalfAway Team 1stHalfHome Team 2nd HalfAway Team 2nd HalfTeamLast 5: No of Games (2ndH)Last 5: No of Goals (2ndH)Last 5: Goals Conceded
2BoltonLeicester1021Bolton376
3CarlisleBolton1101Carlisle002
4BoltonHuddersfield2111Exeter114
5NewcastleBolton0100Grimsby001
6TorquayBolton1102Huddersfield113
7BoltonGrimsby1000Leicester113
8BoltonExeter1231Newcastle001
9QPRBolton1112QPR113
10Torquay003
Sheet3
Cell Formulas
RangeFormula
J2:J10J2=SORT(UNIQUE(VSTACK(Table5[Home Team],Table5[Away Team])))
K2:K10K2=LET(z,IF(Table5[Home Team]=J2,Table5[Home Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Away Team 2nd Half],"")),y,TAKE(FILTER(z,z<>""),-5),COUNT(FILTER(y,y>0)))
L2:L10L2=LET(z,IF(Table5[Home Team]=J2,Table5[Home Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Away Team 2nd Half],"")),SUM(TAKE(FILTER(z,z<>""),-5)))
M2:M10M2=LET(t,Table5,z,IF(INDEX(t,0,1)=J2,INDEX(t,0,4)+INDEX(t,0,6),IF(INDEX(t,0,2)=J2,INDEX(t,0,3)+INDEX(t,0,5),"")),SUM(TAKE(FILTER(z,z<>""),-5)))
Dynamic array formulas.
 
Upvote 0
Solution
See if these do what you want (check the table name)
In column M - Goals conceded in last (up to) 5 games - I have used a different notation which makes the formula considerably shorter, but perhaps harder to read/understand. If it does what you want but wanted the fuller version, M2 would be
Excel Formula:
=LET(z,IF(Table5[Home Team]=J2,Table5[Away Team 1stHalf]+Table5[Away Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Home Team 1stHalf]+Table5[Home Team 2nd Half],"")),SUM(TAKE(FILTER(z,z<>""),-5)))

ormy28.xlsm
ABCDEFIJKLM
1Home TeamAway TeamHome Team 1stHalfAway Team 1stHalfHome Team 2nd HalfAway Team 2nd HalfTeamLast 5: No of Games (2ndH)Last 5: No of Goals (2ndH)Last 5: Goals Conceded
2BoltonLeicester1021Bolton376
3CarlisleBolton1101Carlisle002
4BoltonHuddersfield2111Exeter114
5NewcastleBolton0100Grimsby001
6TorquayBolton1102Huddersfield113
7BoltonGrimsby1000Leicester113
8BoltonExeter1231Newcastle001
9QPRBolton1112QPR113
10Torquay003
Sheet3
Cell Formulas
RangeFormula
J2:J10J2=SORT(UNIQUE(VSTACK(Table5[Home Team],Table5[Away Team])))
K2:K10K2=LET(z,IF(Table5[Home Team]=J2,Table5[Home Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Away Team 2nd Half],"")),y,TAKE(FILTER(z,z<>""),-5),COUNT(FILTER(y,y>0)))
L2:L10L2=LET(z,IF(Table5[Home Team]=J2,Table5[Home Team 2nd Half],IF(Table5[Away Team]=J2,Table5[Away Team 2nd Half],"")),SUM(TAKE(FILTER(z,z<>""),-5)))
M2:M10M2=LET(t,Table5,z,IF(INDEX(t,0,1)=J2,INDEX(t,0,4)+INDEX(t,0,6),IF(INDEX(t,0,2)=J2,INDEX(t,0,3)+INDEX(t,0,5),"")),SUM(TAKE(FILTER(z,z<>""),-5)))
Dynamic array formulas.

Works a treat that. Thanks.

And thank you all for your help. Using the TAKE and LET functions will be something I will look into using more frequently in future.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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