Calculate SUMIF based on changing variable

David0227

New Member
Joined
Mar 1, 2020
Messages
14
Office Version
  1. 2011
  2. 2010
  3. 2007
Platform
  1. Windows
  2. MacOS
Hi,

In the 'Home Team' column & 'Away Team' area (Column D & E) the current rows' results SUM all the previous results for that team starting with the row directly above all the way up E.g. Row 15 Column D = SUMIF(B$5:B15,B16,D$5:D15) and Row 15 column E = =sUMIF(C$5:C15,C16,D$5:D15)

I'd like to be able to use the 'Number of Results to Count from bottom up' field (Row 1 column B) which is currently set to 5 (but will change to any number I want) to only count from bottom up the last 5 results (or whatever number I change it to ) results. So in the example below in row 15 column 4, the value would be 0 as it'd only sum Team 1'ss last 5 results from bottom up in Column C which just happens to stop in row 6 in this instance



Column AColumn BColumn CColumn DColumn E
Row 1Number of Results to Count
from bottom up
5
Row 2
Row 3Home TeamAway TeamMatch Result (1 - Home Win, 0 - Draw, -1 Away Win)Home TeamAway Team
Row 4Team 1Team 21
Row 5Team 2Team 3000
Row 6Team 3Team 1-100
Row 7Team 4Team 3100
Row 8Team 1Team 2011
Row 9Team 2Team 100-1
Row 10Team 4Team 3111
Row 11Team 3Team 21-11
Row 12Team 4Team 112-1
Row 13Team 2Team 3-102
Row 14Team 3Team 4-100
Row 15Team 1Team 401-1
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Welcome to Mr Excel!

I think that I have this right, the formula is progressive, so for Team 1, the last 5 in Row 15 will look back to Row 6, in Row 12 it will look back to Row 4. If there are less than 5 previous results for a team, then it will take the maximum available.

For posting examples, it is better if you use the xl2bb add-in (click the green button on the reply box toolbar). When you post samples as you did in post 1, everything is moved down and right of where it should be, meaning that 'Column B' in the table is 'Column C' in the fomulas, etc which can lead to a lot of confusion.

Book4.xlsx
ABCDE
1Number of Results to Count5
2
3Home TeamAway TeamMatch Result (1 - Home Win, 0 - Draw, -1 Away Win)Home TeamAway Team
4Team 1Team 21
5Team 2Team 3010
6Team 3Team 1-101
7Team 4Team 310-1
8Team 1Team 2001
9Team 2Team 1010
10Team 4Team 3110
11Team 3Team 2111
12Team 4Team 1120
13Team 2Team 3122
14Team 3Team 4-133
15Team 1Team 4012
Sheet3
Cell Formulas
RangeFormula
D5:E15D5=IFERROR(SUMPRODUCT((ROW(A$4:A4)>=AGGREGATE(14,6,ROW(A$4:A4)/($A$4:$B4=A5),MIN($B$1,COUNTIF($A$4:$B4,A5))))*($A$4:$B4=A5)*$C$4:$C4),0)
 
Upvote 0
Thank you so much - and points noted on etiquette :)
 
Upvote 0
Hi,
I have another question on this one - is it possible to AVERAGE the last X outcomes rather than SUM them all up? I've tried tweaking your formula but I can't get it at all.
Thank you
 
Upvote 0
For a conditional average you need to do sum / count, I think that I have this correct but I suggest doing some manual calculations to check that the results are as expected.
Book2
ABCDE
1Number of Results to Count5
2
3Home TeamAway TeamMatch Result (1 - Home Win, 0 - Draw, -1 Away Win)Home TeamAway Team
4Team 1Team 21
5Team 2Team 3010
6Team 3Team 1-101
7Team 4Team 310-0.5
8Team 1Team 2000.5
9Team 2Team 100.33333330
10Team 4Team 3110
11Team 3Team 210.250.25
12Team 4Team 1110
13Team 2Team 310.40.4
14Team 3Team 4-10.61
15Team 1Team 400.20.5
Sheet7
Cell Formulas
RangeFormula
D5:E15D5=IFERROR(SUMPRODUCT((ROW(A$4:A4)>=AGGREGATE(14,6,ROW(A$4:A4)/($A$4:$B4=A5),MIN($B$1,COUNTIF($A$4:$B4,A5))))*($A$4:$B4=A5)*$C$4:$C4)/SUMPRODUCT((ROW(A$4:A4)>=AGGREGATE(14,6,ROW(A$4:A4)/($A$4:$B4=A5),MIN($B$1,COUNTIF($A$4:$B4,A5))))*($A$4:$B4=A5)),0)
 
Upvote 0
I have a new scenario that I'm really struggling to get right, it's similar to the first question and I'd really appreciate some help.

I'd like cell E13 to average all of Team 2's Ratings from their last 3 games (columns C & D) regardless of whether they are the home team or away team. I'd like the same principal to be applied in F13, using all of their last 3 games regardless of whether they are home or away team. I then want to copy that formula down columns E & F as new results come in.

Additional point is the number of games I want to look back can change depending depending on value in B1.

Thank you

Mr Excel Examples.xlsx
ABCDEF
1Number of Games3
2
3Home TeamAway TeamHome Team RatingAway Team RatingHome Team Running ScoreAway Team Running Score
4Team 5Team 61.21.6
5Team 4Team 52.21
6Team 6Team 53.21.6
7Team 3Team 43.31.9
8Team 5Team 45.23.2
9Team 2Team 36.24.2
10Team 4Team 32.45.2
11Team 1Team 21.86.2
12Team 3Team 22.21.6
13Team 2Team 44.676.23
Sheet1
Cell Formulas
RangeFormula
E13E13=(D12+D11+C9)/3
F13F13=C10+D8+D7/3
 
Upvote 0
I think that this is what you need, the second result in your example appears incorrect due to missing parenthesis.
Book123.xlsm
ABCDEF
1Number of Games3
2
3Home TeamAway TeamHome Team RatingAway Team RatingHome Team Running ScoreAway Team Running Score
4Team 5Team 61.21.6
5Team 4Team 52.2101.2
6Team 6Team 53.21.61.61.1
7Team 3Team 43.31.902.2
8Team 5Team 45.23.21.2666666672.05
9Team 2Team 36.24.203.3
10Team 4Team 32.45.22.4333333333.75
11Team 1Team 21.86.206.2
12Team 3Team 22.21.64.2333333336.2
13Team 2Team 44.6666666672.5
Sheet22
Cell Formulas
RangeFormula
E5:F13E5=IFERROR(SUMPRODUCT((ROW(A$4:A4)>=AGGREGATE(14,6,ROW(A$4:A4)/($A$4:$B4=A5),MIN($B$1,COUNTIF($A$4:$B4,A5))))*($A$4:$B4=A5)*$C$4:$D4)/SUMPRODUCT((ROW(A$4:A4)>=AGGREGATE(14,6,ROW(A$4:A4)/($A$4:$B4=A5),MIN($B$1,COUNTIF($A$4:$B4,A5))))*($A$4:$B4=A5)),0)
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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