Alternating Cycle (T)op/(B)ottom Sequence - google-sheets

Needing either a single array formula or one dragged down the column that will show alternating sequence per a rule set. Ruleset and detailed example are within the attached link here.
https://docs.google.com/spreadsheets/d/1Sdi1jFpBKF2RJeqWNjLybVcwR0G6hvozZZTvcTK1xis/edit?usp=sharing

let cell E79 be:
=IF(E79>55, "T",
IF(E79<45, "B",
LOOKUP(MIN(MAX(INDEX(E2:E78,
MATCH(1,INDEX((E2:E78>55)+(E2:E78<45)*(E2:E78<>""), ), 0)), 45), 55),
{45,55}, {"B", "T"})))
paste this in cell E78 and drag it upwards:
=IF(((E78<55)*(E78>45))*((E79<55)*(E79>45)),,
IF((QUERY({F79:F},"where Col1 is not null limit 1", 0)="T")*(E78<=55)*(E79>=45), "B",
IF((QUERY({F79:F},"where Col1 is not null limit 1", 0)="B")*(E78> 45)*(E79< 55), "T", )))
paste this in G2 cell:
=ARRAYFORMULA(INDEX(SORT({VLOOKUP(ROW(
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), )))), IF(INDEX(SORT({
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), ))), ROW(
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), ))))}, 2, 0),,1)<>"", {ROW(
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), )))),INDEX(SORT({
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), ))), ROW(
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), ))))}, 2, 0),,1)}), 2, 1), ROW(
INDIRECT("F2:F"&MAX(IF(F2:F<>"", ROW(F2:F), ))))}, 2, 0),,1))
and paste this in H2 cell:
=ARRAYFORMULA(
IF(G2:G="T", IFERROR(VLOOKUP(C2:C, {QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(
SPLIT(TRANSPOSE(SPLIT("♦"&QUERY(IF(G2:G="T", C2:C, )&
IF(F2:F<>"", "♦", ),,999^99), "♦")), " ")*1, "where Col1 is not null", 0)),
"select "&TEXTJOIN(",", 1, IF(LEN(FILTER(F2:F, F2:F="T")),
"max(Col"&ROW(F2:F)-ROW(F2)+1&")", ))&"")), "select Col2"),
FILTER(F2:F, F2:F="T")}, 2, 0)),
IF(G2:G="B", IFERROR(VLOOKUP(D2:D, {QUERY(TRANSPOSE(QUERY(TRANSPOSE(QUERY(
REGEXREPLACE(TO_TEXT(
SPLIT(TRANSPOSE(SPLIT("♦"&QUERY(IF(G2:G="B", D2:D, )&
IF(F2:F<>"", "♦", ),,999^99), "♦")), " ")*1), "^0$", "999999999")*1,
"where Col1 is not null", 0)), "select "&TEXTJOIN(",", 1,
IF(LEN(FILTER(F2:F, F2:F="B")),
"min(Col"&ROW(F2:F)-ROW(F2)+1&")", ))&"")), "select Col2"),
FILTER(F2:F, F2:F="B")}, 2, 0)), )))
spreadsheet demo

=ARRAYFORMULA(IF(IFERROR(VLOOKUP(INDIRECT("C2:C"&COUNTA(C2:C)),
MIN(FILTER(C2:C, D2:D<45, D2:D<>"")), 1, 0))<>"", "B", ))

Related

Looking for the most profitable range (mathematical names: "maximum subarray problem" or "maximum consecutive subsequence sum")

To be able to find the most profitable range, I add the lowest value I want to the highest value I want, with that I create a table like this example:
https://docs.google.com/spreadsheets/d/17zpapBeC5wYxyU6SjbqcbnV4_QP4gooxj0PxdCywDk0/edit?usp=sharing
Cell's formulas examples:
Between 0 and 0:
=IFERROR(SUM(FILTER($B$1:$B,($A$1:$A<=D2)*($A$1:$A>=$E$1))))
Between 5 and 10:
=IFERROR(SUM(FILTER($B$1:$B,($A$1:$A<=D12)*($A$1:$A>=$J$1))))
=MAX(E2:O12)
Max Profit = £185.00
=INDEX(A1:O1,ARRAYFORMULA(MIN(IF(E2:O12=MAX(E2:O12),COLUMN(E2:O12)))))
Value Min for Max Profit = 4
=INDEX(D1:D12,ARRAYFORMULA(MAX(IF(E2:O12=MAX(E2:O12),ROW(E2:O12)))))
Value Max for Max Profit = 10
When there are hundreds of values¹ in A and B, this table gets very big and heavy, even causing crashes like my current original data spreadsheet.
Is there any way using a only one formula or script code to found Max Profit | Value Min for Max Profit | Value Max for Max Profit without doing each range one by one needing to use thousands of cells each with a specific formula?
Notes:
hundreds of values¹ → my original spreadsheet currently contains 1471 rows of data in A with the results in B. So to be able to do this analysis, I need to put 2,163,841 formulas like =IFERROR(SUM(FILTER($B$1:$B,($A$1:$A<=D2)*($A$1:$A>=$E$1)))) in the cells to create the table and find the most profitable range.
max:
=INDEX(MAX(IF(SEQUENCE(MAX(A:A)+1)>=SEQUENCE(1, MAX(A:A)+1),
SUMIF(SEQUENCE(MAX(A:A)+1), "<="&SEQUENCE(MAX(A:A)+1), B:B)*
SEQUENCE(1, MAX(A:A)+1, 1, )-QUERY(QUERY(
(SEQUENCE(MAX(A:A)+1)<SEQUENCE(1, MAX(A:A)+1))*B1:B,
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX(A:A)+1)&")")),
"offset 1", ), )))
value min:
=INDEX(REGEXEXTRACT(MAX(IF(SEQUENCE(MAX(A:A)+1)>=SEQUENCE(1, MAX(A:A)+1),
SUMIF(SEQUENCE(MAX(A:A)+1), "<="&SEQUENCE(MAX(A:A)+1), B:B)*
SEQUENCE(1, MAX(A:A)+1, 1, )-QUERY(QUERY(
(SEQUENCE(MAX(A:A)+1)<SEQUENCE(1, MAX(A:A)+1))*B1:B,
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX(A:A)+1)&")")),
"offset 1", )+(SEQUENCE(1, MAX(A:A)+1)*10^-10)&9, )*1)&"", "0(\d+)9$")-1)
value max:
=INDEX(REGEXEXTRACT(MAX(IF(SEQUENCE(MAX(A:A)+1)>=SEQUENCE(1, MAX(A:A)+1),
SUMIF(SEQUENCE(MAX(A:A)+1), "<="&SEQUENCE(MAX(A:A)+1), B:B)*
SEQUENCE(1, MAX(A:A)+1, 1, )-QUERY(QUERY(
(SEQUENCE(MAX(A:A)+1)<SEQUENCE(1, MAX(A:A)+1))*B1:B,
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX(A:A)+1)&")")),
"offset 1", )+(SEQUENCE(MAX(A:A)+1)*10^-10)&9, )*1)&"", "0(\d+)9$")-1)
update:
=INDEX(TEXTJOIN(", ", 1, UNIQUE(FLATTEN(
IF(IF(SEQUENCE(MAX(A:A)+1)>=SEQUENCE(1, MAX(A:A)+1),
SUMIF(SEQUENCE(MAX(A:A)+1), "<="&SEQUENCE(MAX(A:A)+1), B:B)*
SEQUENCE(1, MAX(A:A)+1, 1, )-QUERY(QUERY(
(SEQUENCE(MAX(A:A)+1)<SEQUENCE(1, MAX(A:A)+1))*B1:B,
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX(A:A)+1)&")")),
"offset 1", ), )=MAX(IF(SEQUENCE(MAX(A:A)+1)>=SEQUENCE(1, MAX(A:A)+1),
SUMIF(SEQUENCE(MAX(A:A)+1), "<="&SEQUENCE(MAX(A:A)+1), B:B)*
SEQUENCE(1, MAX(A:A)+1, 1, )-QUERY(QUERY(
(SEQUENCE(MAX(A:A)+1)<SEQUENCE(1, MAX(A:A)+1))*B1:B,
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(MAX(A:A)+1)&")")),
"offset 1", ), )), SEQUENCE(1, MAX(A:A)+1, 0), )))))

Add a new row when sum of rows reaches value

How would I go about adding a new row beneath the value where max 48 in the below example is reached in Google Sheets?
Edit: Demo Google Sheet added: https://docs.google.com/spreadsheets/d/1jKX-AOfbFAEvks_Q8P8jegFek4ZocZJY113TfaY6jcQ/edit#gid=2041258691
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(QUERY(IF(A5:A<>"",
"♦"&A5:A&"♠"&B5:B&IFERROR(VLOOKUP(A5:A, REGEXREPLACE(""&SORTN(FILTER({A5:A,
IF(LEN(A5:A), QUERY(ROUNDUP((ROUND(MMULT(TRANSPOSE((ROW(B5:B)
<= TRANSPOSE(ROW(B5:B)))*B5:B), SIGN(B5:B))*5/48, 1))-1), "offset 1", 0), )},
MOD(IF(LEN(A5:A), QUERY(ROUNDUP((ROUND(MMULT(TRANSPOSE((ROW(B5:B)
<= TRANSPOSE(ROW(B5:B)))*B5:B), SIGN(B5:B))*5/48, 1))-1), "offset 1", 0), ), 5)=0,
IF(LEN(A5:A), QUERY(ROUNDUP((ROUND(MMULT(TRANSPOSE((ROW(B5:B)
<= TRANSPOSE(ROW(B5:B)))*B5:B), SIGN(B5:B))*5/48, 1))-1), "offset 1", 0), )<>0),
999^99, 2, 2, 1), "^\d+", "♦♥"), 2, 0)), ),,999^99), "♦")), "♠♥"))

SUM contiguous values in column

Is it possible to SUM groups of contiguous values in a column without manually grouping them one by one the way I have in this picture?
sample sheet
=ARRAYFORMULA(SUBSTITUTE(TRANSPOSE(SPLIT(CONCATENATE({"♥"&MMULT(
SPLIT(TRANSPOSE(SPLIT(TEXTJOIN(" ", 1, IF(INDIRECT("A2:A"&MAX(IF(A2:A<>"",
ROW(A2:A), )))="", "♦", A2:A)), "♦")), " ")*1, ROW(INDIRECT("A1:A"&COLUMNS(
SPLIT(TRANSPOSE(SPLIT(TEXTJOIN(" ", 1, IF(INDIRECT("A2:A"&MAX(IF(A2:A<>"",
ROW(A2:A), )))="", "♦", A2:A)), "♦")), " ")*1)))^0),IF(
SPLIT(TRANSPOSE(SPLIT(TEXTJOIN(" ", 1, IF(INDIRECT("A2:A"&MAX(IF(A2:A<>"",
ROW(A2:A), )))="", "♦", A2:A)), "♦")), " ")<>"", "♥♠", )}), "♥")), "♠", ))

How can I JOIN the ARRAY results of this formula with a ","?

I've tried to add JOIN to this formula in every place I can think of, but none seem to be correct.
=ARRAYFORMULA(IFERROR(VLOOKUP(D2:D&E2:E,
TRIM(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
{INDEX(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select Col1,count(Col1) where Col1 is not null group by Col1 pivot Col2", 0),,1), IF(
ISNUMBER(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0)),
QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦♥"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2 limit 0", 0), )})
,,999^99))), "♥"))), {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, 0)))
The results are multiple columns/rows. I want to keep the rows, but JOIN the column values with a ",".
my sheet
like this perhaps:
=ARRAYFORMULA(SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(SUBSTITUTE(IFERROR(VLOOKUP(D2:D&E2:E,
TRIM(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
{INDEX(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select Col1,count(Col1) where Col1 is not null group by Col1 pivot Col2", 0),,1), IF(
ISNUMBER(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0)),
QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(tasksAssociations!C2:C&tasksAssociations!D2:D))*(LEN(tasksAssociations!G2:G)),
tasksAssociations!C2:C&tasksAssociations!D2:D&"♦♥"&tasksAssociations!G2:G, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2 limit 0", 0), )})
,,999^99))), "♥"))), {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}, 0)), " ", "♫")),,999^99))), " ", ", "), "♫", " "))

Turning formula into ArrayFormula Part2

Following on from this question
Turning formula into ArrayFormula
I failed to make it clear that the value cells may also contain multiple values in CSV format. I tried adjusting it to:
=ARRAYFORMULA(IF(LEN(A4:A), IFERROR(VLOOKUP(SPLIT(A4:A,","),
QUERY(TRIM(TRANSPOSE(SPLIT($B$1, ","))),
"select Col1,count(Col1) group by Col1", 0), 2, 0), 0), ))
But no luck. The test sheet is here.
https://docs.google.com/spreadsheets/d/12xATTwuc-e6kXn_OF2Uibg6EgtSGbInI6Q_-vueknXg/edit#gid=0
My original formula is in B4 which works but needs copying down which is what I want to avoid.
=ARRAYFORMULA(IFERROR(VLOOKUP(A4:A, QUERY({QUERY(TRIM(SPLIT(TRANSPOSE(SPLIT(
QUERY(TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(A4:A), "♠"&A4:A&"♦"&TRIM(IFERROR(
SPLIT(A4:A, ","))), )), , 999^99)), , 999^99), "♠")), "♦")), "select Col1", 0),
ARRAY_CONSTRAIN(IFERROR(VLOOKUP(QUERY(TRIM(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(
QUERY(TRANSPOSE(IF(LEN(A4:A), "♠"&A4:A&"♦"&TRIM(IFERROR(SPLIT(A4:A, ","))), ))
, , 999^99)), , 999^99), "♠")), "♦")), "select Col2", 0), QUERY(IFERROR(VLOOKUP(
QUERY(TRIM(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(A4:A),
"♠"&A4:A&"♦"&TRIM(IFERROR(SPLIT(A4:A, ","))), )), , 999^99)), , 999^99), "♠")), "♦")),
"select Col2", 0), QUERY(TRIM(TRANSPOSE(SPLIT(B1, ","))),
"select Col1,count(Col1) group by Col1", 0), {1, 2}, 0)),
"select Col1,sum(Col2) group by Col1", 0), 2, 0), 0), COUNTA(QUERY(TRIM(SPLIT(
TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(A4:A),
"♠"&A4:A&"♦"&TRIM(IFERROR(SPLIT(A4:A, ","))), )), , 999^99)), , 999^99), "♠")), "♦")),
"select Col1", 0)), 1)}, "select Col1,sum(Col2) group by Col1" ,0), 2, 0)))

Resources