STREAK

=STREAK(a,v,o,x)

a
array
v
value
[o]
orientation: if omitted or 0, byrow (clm vector result), if 1 , bycol (row vector result)
[x]
max argument: if omitted => entire vector, if 1 max(vector)

Largest consecutive distribution of a given value, BYROW or BYCOL

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
STREAK Largest consecutive distribution of a given value, byrow or bycol
This is my take on a cool function: STREAK
Added new functionality, max argument [x]
Main calculation core of the function: IF(i, v + i, 0)
Excel Formula:
=LAMBDA(a, v, [o], [x],
    LET(
        b, IF(a = v, 1, 0),
        s, LAMBDA(x, MAX(SCAN(0, x, LAMBDA(v, i, IF(i, v + i, 0))))),
        i, IF(o, BYROW(b, LAMBDA(x, s(x))), BYCOL(b, LAMBDA(x, s(x)))),
        IF(x, MAX(i), i)
    )
)
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1
2=CHAR(RANDARRAY(20,20,65,67,1))=STREAK(B3#,"a")
3CCBABABCBCBCABCABCBA1
4AAABBAAABCBCABABACBA3
5ACCBCBBBBABBCBABCBCC1
6BABCABABCAAABACAAABA3
7ACAAACCCCBAABCBBABCB3
8CACCABACBBBBBCCCABCB1
9CCAACBAAAABCAAABBACC4
10BAACBACAABCCBAAABCCC3
11BBABBCCABBBABABABACA1
12ABCAAACACACBCAABABCA3
13CCCBCCAAABCABCAACABA3
14BCCBAAAACABCAABBAAAB4
15BCBABBAAABCABBCAAACB3
16BBACBACABBAABACBABBB2
17BBBABABABABBCCCBBAAA3
18BBBABBACBBCCCACBCABC1
19AAABABBABCCCCABCBACC3
20AACAABABBABCBAACACAC2
21CBAABAAABACBBCAABBCB3=STREAK(B3#,"a",,1)
22BBABCAACACACCCAABAAC24
23
2422323239222224323313
25=STREAK(B3#,"a",1)
269
27=STREAK(B3#,"a",1,1)
28
Sheet1
Cell Formulas
RangeFormula
B2,Y21,W2B2=FORMULATEXT(B3)
B3:U22B3=CHAR(RANDARRAY(20,20,65,67,1))
W3:W22W3=STREAK(B3#,"a")
Y22Y22=STREAK(B3#,"a",,1)
B24:U24B24=STREAK(B3#,"a",1)
B25,U27B25=FORMULATEXT(B24)
U26U26=STREAK(B3#,"a",1,1)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3:U22Expression=B3="a"textNO
 
Upvote 0
I wonder, if instead of providing an argument "a", one could optionally also leave that out, and it returns the frequency distribution as an array, (instead of a vector), either by row, or by column?
 
I think BYCOL and BYROW are the wrong way round - below is my edit. I've also replaced the last argument from x, to m, for readability, since the inner lambdas have their own 'x'...

Excel Formula:
STREAK = LAMBDA(a, v, [o], [m],
    LET(
        b, IF(a = v, 1, 0),
        s, LAMBDA(x, MAX(SCAN(0, x, LAMBDA(v, i, IF(i, v + i, 0))))),
        i, IF(o, BYCOL(b, LAMBDA(x, s(x))), BYROW(b, LAMBDA(x, s(x)))),
        IF(m, MAX(i), i)
    )
);
 
Yes st001, you are right !! They were switched. Thank you!!
Regarding max I am sticking with "x", intuitively I am using m for min and x for max.
And there is no problem with "x", inside closed LAMBDAS arguments can take any other previous variable name.
Excel Formula:
=LAMBDA(a, v, [o], [x],
    LET(
        b, IF(a = v, 1, 0),
        s, LAMBDA(x, MAX(SCAN(0, x, LAMBDA(v, i, IF(i, v + i, 0))))),
        i, IF(o, BYCOL(b, LAMBDA(x, s(x))), BYROW(b, LAMBDA(x, s(x)))),
        IF(x, MAX(i), i)
    )
)
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1
2=CHAR(RANDARRAY(20,20,65,67,1))=STREAK(B3#,"a")
3BAAAABBACAAAAAAABBBB7
4BBCBBAACBBABCCACBBBA2
5BCACCCCCBCABACCACBCC1
6BACAAACABACCBCACCABB3
7CBBCCBABBCBACAACACCB2
8ACACACABBABCABCCABAC1
9BCABBAACBABBACAAAABC4
10CABCACAACACBCBCACAAA3
11BBAAACACCCACBCBCAACB3
12BBBBAACBACABCBCCACCB2
13ACCCCCBABBAABAAABACB3
14BBCBBAAABAACBCACACAA3
15ACACBACACCBCCACCAABC2
16CAABBACCBBBACBACABCC2
17CABCCCBABCAACAAABCCC3
18AACACCBABACAAAAAAABC7
19BABCCBBBABCACCABCBAC1
20CCABBBCBBCBACACAACAB2
21BBCBCABBBAACCBCCCACC2=STREAK(B3#,"a",,1)
22CCAABBBACCACBACBACBB27
23
2414213353134522423321
25=STREAK(B3#,"a",1)
265
27=STREAK(B3#,"a",1,1)
28
Sheet1
Cell Formulas
RangeFormula
B2,Y21,W2B2=FORMULATEXT(B3)
B3:U22B3=CHAR(RANDARRAY(20,20,65,67,1))
W3:W22W3=STREAK(B3#,"a")
Y22Y22=STREAK(B3#,"a",,1)
B24:U24B24=STREAK(B3#,"a",1)
B25,U27B25=FORMULATEXT(B24)
U26U26=STREAK(B3#,"a",1,1)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3:U22Expression=B3="a"textNO
 
Book1
ABCDEFGHIJKLMNOPQRSTUV
1Concept for an alternative solution, very versatile, delivers all information about streaks lenghts and their frequencies with ease
2Works for arrays < 32,767rws x 32,767clms (CONCAT Excel limit)
3
4sample vector
5BAAAAAAAAAAAAAAC
6
7=--(B5:U5="a")
801110110111011110110
9
10=CONCAT(B8#)
1101110110111011110110
12
13=TEXTSPLIT(B11,0,,1)
1411111111111111
15
16=LEN(B14#)
1732342all information about frequencies/lengths is here.
182x2 elements
192x3 elements
201x4 elements
21
22Based on that we can develop any function we want to do any more or less complex analysis we want only with some arrays cosmetics.
23
Sheet2
Cell Formulas
RangeFormula
B7,B16,B13,B10B7=FORMULATEXT(B8)
B8:U8B8=--(B5:U5="a")
B11B11=CONCAT(B8#)
B14:F14B14=TEXTSPLIT(B11,0,,1)
B17:F17B17=LEN(B14#)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B5:U5Expression=B5="a"textNO
 
Extracting only max frequencies is too simple.
There are 2 important parameters when it comes to a complete study of streaks distributions, their lengths, and their frequencies.
This function returns the frequency distribution for each streak's length.
STREAKS(ar,[v],[o]) (2 embedded lambdas and 1 REDUCE to assemble the results)
ar: array
[v]: single value or array of values, if omitted, all unique values of "ar" are considered
[o]: orientation, if omitted, byrow, if 1, bycol
Excel Formula:
=LAMBDA(ar, [v], [o],
    LET(
        a, IF(ar = "", "", ar),
        u, SORT(UNIQUE(TOCOL(a))),
        s, LAMBDA(x, LET(s, SCAN(0, x, LAMBDA(v, i, IF(i, v + i, 0))), f, FILTER(s, s), q, FREQUENCY(f, SEQUENCE(MAX(f))), TEXTJOIN(",", , DROP(q, -1) - DROP(q, 1)))),
        r, LAMBDA(x, LET(e, --(a = x), IF(o, BYCOL(e, LAMBDA(x, s(x))), BYROW(e, LAMBDA(x, s(x)))))),
        b, IF(ISOMITTED(v), u, TOCOL(v)),
        d, REDUCE(0, SEQUENCE(ROWS(b)), LAMBDA(v, i, LET(x, INDEX(b, i), IF(o, VSTACK(v, r(x)), HSTACK(v, r(x)))))),
        IFERROR(IF(o, HSTACK(b, DROP(d, 1)), VSTACK(TOROW(b), DROP(d, , 1))), 0)
    )
)
STREAK.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1"o", omitted => streaks byrowv,omitted=> all values
2=STREAKS(B4:U23)=STREAKS(B4:U23,{"C","B"})=STREAKS(B4:U23,"A")
3arABCCBA
4ABBAABCBCBCBCBCACBBB2,15,1,1665,1,12,1
5CABBCCCBACBBAABCABCB3,14,24,0,14,0,14,23,1
6ABAABCCCABCBBACABCAA4,24,13,0,13,0,14,14,2
7AAAACAACCBBACACCBBAC3,1,0,10,23,23,20,23,1,0,1
8BACBBCBBABBCCBCABBBC32,3,14,14,12,3,13
9AAACCAAAAABBBACABAAA2,0,2,0,11,0,11,11,11,0,12,0,2,0,1
10ABBBBCCBBBBAACCCCBAC2,11,0,0,21,1,0,11,1,0,11,0,0,22,1
11ACCBBACBBBCBABCAABAB4,14,1,13,13,14,1,14,1
12CACCBCABCCACACBCBCCB455,35,354
13AABCACCACBCCAABBABAB4,24,12,22,24,14,2
14BACCCCAAACCCAABBCBCA2,1,12,12,0,1,12,0,1,12,12,1,1
15BCAACCCBBBBCBBABBACA3,11,2,0,13,0,13,0,11,2,0,13,1
16BCABBBBBBCCBBCBCBBCC12,2,0,0,0,13,23,22,2,0,0,0,11
17BAABCAACBABABBCCCACA4,24,13,0,13,0,14,14,2
18BABABAAAACABCCACBABC5,0,0,163,13,165,0,0,1
19BBCAABAABCCBCABBBBBC1,23,1,0,0,13,13,13,1,0,0,11,2
20CBCAABACBAABBCCBBBBB1,23,1,0,0,13,13,13,1,0,0,11,2
21CBCCBABBBAAABBBCCBAC2,0,13,0,22,22,23,0,22,0,1
22CCBCBBCCACCCCAAABAAB1,1,13,11,2,0,11,2,0,13,11,1,1
23BCBCABABACCBCCABBABC56,13,23,26,15
24
25Results interpretation
26
272,0,2,0,12 "A" streaks of len=1
280 "A" streaks of len=2
292 "A" streaks of len=3
300 "A" streaks of len=4
311 "A" streak of len=5
32
STREAKS 1
Cell Formulas
RangeFormula
W2,AA2,AD2W2=FORMULATEXT(W3)
W3:Y23W3=STREAKS(B4:U23)
AA3:AB23AA3=STREAKS(B4:U23,{"C","B"})
AD3:AD23AD3=STREAKS(B4:U23,"A")
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B4:U23Expression=B4="c"textNO
B4:U23Expression=B4="b"textNO
B4:U23Expression=B4="a"textNO
 
STREAK.xlsx
ABCDEFGHIJKLM
1"o",1, streaks bycol
2=STREAKS(C15:L44,,1)
3A2,2,1,15,1,14,25,1,16,0,13,48,13,1,23,1,0,0,15,2
4B2,32,41,1,1,15,25,1,14,0,16,12,2,0,14,2,14,2,1
5C5,0,0,16,21,3,0,0,13,43,43,3,15,2,14,1,15,0,0,11,3,1
6
7=STREAKS(C15:L44,{"C","B"},1)
8C5,0,0,16,21,3,0,0,13,43,43,3,15,2,14,1,15,0,0,11,3,1
9B2,32,41,1,1,15,25,1,14,0,16,12,2,0,14,2,14,2,1
10
11=STREAKS(C15:L44,"A",1)
12A2,2,1,15,1,14,25,1,16,0,13,48,13,1,23,1,0,0,15,2
13
14
15arCBCCAAACBB
16CBCBCABCCB
17CAAACCACBA
18CCAAACBBBC
19ABBCBACBCC
20ACAACACCBB
21BBCBCBAACB
22BBCBAACCCB
23CCCABBBACA
24ACCCCCABCA
25AACBAAABAB
26AAABCCBBCC
27ACCCBBCBAC
28CACCBCCAAC
29BBAAACAAAA
30BCABBACAAB
31CBBCBABCAC
32ABBCBCBCBC
33BACAACAACA
34ACBAAACAAA
35AABAAAABBB
36ABBCCBCABB
37BBBCBCBAAC
38CAABCBAABC
39BACACBCBAB
40BACBABCBAA
41ACBCBCCCBB
42CCBCCCAABA
43AABACCCBBC
44ACABBABCCA
45
STREAKS 2
Cell Formulas
RangeFormula
B2,B11,B7B2=FORMULATEXT(B3)
B3:L5B3=STREAKS(C15:L44,,1)
B8:L9B8=STREAKS(C15:L44,{"C","B"},1)
B12:L12B12=STREAKS(C15:L44,"A",1)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C15:L44Expression=C15="c"textNO
C15:L44Expression=C15="b"textNO
C15:L44Expression=C15="a"textNO
 
Extracting data from STREAKS 4D array(values, byrow, lengths, frequencies)
ROWSTREAK(a,[v],[n],[lf]) Calls STREAKS
a: array
[v]: single value or array of values, if omitted, all unique values of "ar" are considered
[n]: numeric value length/frequency, if omitted max(length)/max(frequency) triggered by "lf" argument
[lf]: length/frequency argument, if omitted=> frequencies, if 1=> lengths, if n=0 and lf=2=>lengths of max frequencies
Excel Formula:
=LAMBDA(a, [v], [n], [lf],
    LET(
        b, STREAKS(a, v),
        m, MAP(
            DROP(b, 1),
            LAMBDA(x,
                LET(
                    f, --TEXTSPLIT(x, ","),
                    y, MAX(f),
                    ml, COLUMNS(f),
                    l, SEQUENCE(, ml),
                    IF(n, IF(lf, INDEX(f, n), TEXTJOIN(",", , IF(f = n, l, ""))), IF(lf, IF(lf = 1, ml, y & ";" & XMATCH(y, f)), y))
                )
            )
        ),
        e, IFERROR(VSTACK(TAKE(b, 1), m), 0),
        IFERROR(--e, e)
    )
)
STREAK.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
1
2defined name "ar" = B4:AO8
3
4arABCAAABCAAAACCBCBCBABBBACAAACACBCABCACAC
5AABBACBCBBCCCBAABCBBCCCCABAABACBCAACCABB
6ABCABAABCAAABCCCABAAACCCACAABACABBBABAAB
7CCCBAABCABBBBCABACBCABCCCCABCACCACBBABCA
8ABBCCBCCAABBBBAAABBACABBBCCBACCBABACAAAB
9
ROWSTREAK
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B4:AO8,AW4:AW8Expression=B4="c"textNO
B4:AO8,AW4:AW8Expression=B4="b"textNO
B4:AO8,AW4:AW8Expression=B4="a"textNO
 
STREAK.xlsx
ABCDEFGHIJKLMNOPQRST
1all values, max frequenciesall values, max lengthsall values, max frequencies and their lengths
2v, omitted=>all valuesv, omitted=>all valuesv, omitted=>all values
3n, omitted,maxn, omitted,maxn, omitted,max
4lf, omitted=> frequencieslf,1=> lengthslf,2=> max frequencies; min corespondent lengths
5=STREAKS(ar)=ROWSTREAK(ar)=ROWSTREAK(ar,,,1)=ROWSTREAK(ar,,,2)
6ABCABCABCABC
77,0,2,17,0,111,177114327;17;111;1
84,46,45,1,1,14652244;16;15;1
97,3,28,0,14,0,27843337;18;14;1
109,17,1,0,17,1,1,19772449;17;17;1
116,1,25,2,1,12,46543426;15;14;2
12
13lengths for frequencies=1frequencies for length=1
14v, omitted=>all valuesv, omitted=>all values
15n, 1n, 1
16lf, omitted=> frequencieslf,1=> lengths
17=ROWSTREAK(ar,,1)=ROWSTREAK(ar,,1,1)
18ABCABC
194327711
202,3,4465
213784
2222,42,3,4977
2323,4652
24
25lengths for frequencies=2frequencies for length=2
26v, omitted=>all valuesv, omitted=>all values
27n, 2n, 2
28lf, omitted=> frequencieslf,1=> lengths
29=ROWSTREAK(ar,,2)=ROWSTREAK(ar,,2,1)
30ABCABC
313001
32441
3333300
34111
35321124
36
37lengths for frequencies=4frequencies for length=4
38v, omitted=>all valuesv, omitted=>all values
39n, 4n, 4
40lf, omitted=> frequencieslf,1=> lengths
41=ROWSTREAK(ar,,4)=ROWSTREAK(ar,,4,1)
42ABCABC
43100
441,22001
451000
46011
472010
48
ROWSTREAK 1
Cell Formulas
RangeFormula
B5,K41,F41,K29,F29,K17,F17,P5,K5,F5B5=FORMULATEXT(B6)
B6:D11B6=STREAKS(ar)
F6:H11F6=ROWSTREAK(ar)
K6:M11K6=ROWSTREAK(ar,,,1)
P6:R11P6=ROWSTREAK(ar,,,2)
F18:H23F18=ROWSTREAK(ar,,1)
K18:M23K18=ROWSTREAK(ar,,1,1)
F30:H35F30=ROWSTREAK(ar,,2)
K30:M35K30=ROWSTREAK(ar,,2,1)
F42:H47F42=ROWSTREAK(ar,,4)
K42:M47K42=ROWSTREAK(ar,,4,1)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
ar=ROWSTREAK!$B$4:$AO$8B6, F6, F18, F30, F42, K6, K18, K30, K42, P6
 
Sublime work XLambda!
I am kind of thinking that cell P8, should be: 4; 1, 2 - for joint maxes?
 

Forum statistics

Threads
1,224,071
Messages
6,176,201
Members
452,714
Latest member
streamer1234

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