Counting streak in a table column

SWHarmon79

New Member
Joined
Aug 18, 2018
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I want to count the current streak of wins or losses. I have a workbook with two worksheets. One worksheet is a large dataset of games and I want to count the current streak based on a couple of criteria. Below are screenshots of part of both sheets. As an example from the screenshot below CRD = 1 ATL = 2 and if there were a losing streak it would show a negative number.

Screenshot 2023-12-21 160350.png


Screenshot 2023-12-21 160749.png

I want to count the current streak based on the team in column H and the week in A2. I can count all wins or losses in the column using =COUNTA(Data[Win])-MATCH(1,INDEX(1/(Data[Win]=1),0)) But I want to only count the current streak for the selected team and week. I have tried all sorts of combinations but I can't seem to get anything to work.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I assume that you want the streak starting at the given week in A2, and go backwards. Try:

Book1
ABCDEFGHI
1WeekTeamWinWeekTeamStreak
21CRD013ATL2
32CRD0CRD1
43CRD1
54CRD0
65CRD0
76CRD0
87CRD0
98CRD0
109CRD0
1110CRD1
1211CRD0
1312CRD0
1413CRD1
1515CRD0
1616CRD0
1717CRD0
1818CRD0
191ATL1
202ATL1
213ATL0
224ATL0
235ATL1
246ATL0
257ATL1
268ATL0
279ATL0
2810ATL0
2912ATL1
3013ATL1
3114ATL0
3215ATL0
3316ATL0
3417ATL0
35
Sheet3
Cell Formulas
RangeFormula
I2:I3I2=LET(t,FILTER($E$2:$E$100,($B$2:$B$100<=$G$2)*($C$2:$C$100=H2)),r,ROWS(t),wl,INDEX(t,r),lw,1-wl,x,IFERROR(XMATCH(lw,t,0,-1),0),IF(wl,r-x,x-r))


Adjust the ranges to your sheets.
 
Upvote 0
Solution
I assume that you want the streak starting at the given week in A2, and go backwards. Try:

Book1
ABCDEFGHI
1WeekTeamWinWeekTeamStreak
21CRD013ATL2
32CRD0CRD1
43CRD1
54CRD0
65CRD0
76CRD0
87CRD0
98CRD0
109CRD0
1110CRD1
1211CRD0
1312CRD0
1413CRD1
1515CRD0
1616CRD0
1717CRD0
1818CRD0
191ATL1
202ATL1
213ATL0
224ATL0
235ATL1
246ATL0
257ATL1
268ATL0
279ATL0
2810ATL0
2912ATL1
3013ATL1
3114ATL0
3215ATL0
3316ATL0
3417ATL0
35
Sheet3
Cell Formulas
RangeFormula
I2:I3I2=LET(t,FILTER($E$2:$E$100,($B$2:$B$100<=$G$2)*($C$2:$C$100=H2)),r,ROWS(t),wl,INDEX(t,r),lw,1-wl,x,IFERROR(XMATCH(lw,t,0,-1),0),IF(wl,r-x,x-r))


Adjust the ranges to your sheets.
Thank you that worked great. Are there any resources that you can recommend that will help me understand that formula?
 
Upvote 0
Well, you can always Google the individual functions within it, and the Evaluate Formula tool on the Formulas tab is helpful too, which lets you go through each step in the formula. But I'll give you a high level overview of it.

The LET function allows you to create intermediate results that you can use multiple times in the formula. LET(a, A1+5, . . . for example assigns the value of A1+5 to the variable a, and you can use a later on. So:

LET(t,FILTER($E$2:$E$100,($B$2:$B$100<=$G$2)*($C$2:$C$100=H2)), . . .

assigns the value of the FILTER function to t. The FILTER function filters the E column, only keeping the rows that are less than or equal to the week you want (G2), and have the team you want (H2). So this leaves you the win/loss record of the team in question up to the current week.

The r,ROWS(t), section then tells you how many rows there are in the table we just extracted.

The wl,INDEX(t,r), section gives you the value (1 or 0) of the last entry in the table, so the type of game (a win or a loss) that the streak is.

The lw,1-wl, part gives you the opposite of the streak. We'll use this to search for the end of the streak.

The x,IFERROR(XMATCH(lw,t,0,-1),0), section looks for the end of the streak. The lw is the opposite which we're looking for, the t is the table we extracted, the 0 means we want an exact match, and the -1 means we're searching from the bottom going backwards. The IFERROR part comes into play if XMATCH can't find a match. This means that the streak started with the first game of the table. The 0 value will make the last step work out in that case.

And finally, the ,IF(wl,r-x,x-r)) part calculates the length of the streak. The last row r, minus the row where the streak ends x, gives you the streak length. If the current streak type is losses (wl = 0), then it calculates x-r instead, which gives you the same length, but the sign is negative.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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