Related
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), )))))
I am trying to rank a set of numbers (in a column), but I want that rank to appear next to the number in the same cell.
For example, if the number is "21" and that ranks "3rd" – I would want the cell to read "21 (3)". BONUS POINTS if I can format the (3) to be smaller/superscript.
WHAT I'VE TRIED:
While the cell I am working in already has an equation in it, I will simplify it. Let's say A23 = 15 and A24 = 6. I have tried the following equation but it keeps giving me an error. The cell I am working in is H103. The range for ranking is H103:H114.
=SUM(A23+A24) & "(" & RANK(H103,$H$103:$H$114) & ")"
Even if I remove the parentheses or rework that a bit, I am still left with an error. Just trying to find any way I can have 2 formulas, with different results in the same cell; one of which showing the rank of that particular cell.
HERE IS COPY OF MY SHEET — This is a duplicated Google Sheet, so feel free to play around in there if you'd like. I am focusing on ranking H103:H114.
paste in cell H103:
=ARRAYFORMULA(TEXT(VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!B:C"),
"select B,sum(C) where B is not null group by B", 0), 2, 0)+
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!E:D"),
"select E,sum(D) where E is not null group by E", 0), 2, 0), "#.00")&" "&CHAR(8317)&
VLOOKUP(RANK(VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!B:C"),
"select B,sum(C) where B is not null group by B", 0), 2, 0)+
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!E:D"),
"select E,sum(D) where E is not null group by E", 0), 2, 0),
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!B:C"),
"select B,sum(C) where B is not null group by B", 0), 2, 0)+
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!E:D"),
"select E,sum(D) where E is not null group by E", 0), 2, 0), 0),
{1, CHAR(185); 2, CHAR(178); 3, CHAR(179);
4, CHAR(8308); 5, CHAR(8309); 6, CHAR(8310);
7, CHAR(8311); 8, CHAR(8312); 9, CHAR(8313);
10, CHAR(185)&CHAR(8304); 11, CHAR(185)&CHAR(185); 12, CHAR(185)&CHAR(178)}, 2, 0)&
CHAR(8318))
paste in cell I103:
=ARRAYFORMULA(VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!B:C"),
"select B,sum(C) where B is not null group by B", 0), 2, 0)+
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!E:D"),
"select E,sum(D) where E is not null group by E", 0), 2, 0)-C103:C114)
paste in cell L103:
=ARRAYFORMULA(VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!B:C"),
"select B,sum(C) where B is not null group by B", 0), 2, 0)+
VLOOKUP(B103:B114, QUERY(INDIRECT($A$100&"!E:D"),
"select E,sum(D) where E is not null group by E", 0), 2, 0)-J103:J114)
conditional formatting for H column:
red color custom formula:
=REGEXMATCH(H103, CHAR(8317)&CHAR(185)&CHAR(178)&CHAR(8318))
green color custom formula:
=REGEXMATCH(H103, CHAR(8317)&CHAR(185)&CHAR(8318))
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", ))
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)))
I want to get MAX running total. Not just MAX from the column, but max running total updated on each new entry row. And so that it will not overwrite previous entry.
So far I came up with this:
=ArrayFormula(if($C$3:$C="","",MAX("<="&row($C$3:$C),$C$3:$C,A2)))
but it overwrites all the entries above. Giving me just total.
Here's sample doc.
You might try, in A2 and copied down to suit:
=max(A1,C$1:C2)
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
{QUERY(QUERY(TRANSPOSE(QUERY(TRANSPOSE({
QUERY( Q2:Q, "limit "&COUNTA(Q2:Q)),
QUERY(OFFSET(Q2:Q, -1, 0), "limit "&COUNTA(Q2:Q)-1)}),
"select "®EXREPLACE(JOIN(, IF(LEN(Q2:Q),
"max(Col"&ROW(Q2:Q)-ROW(Q2)+1&"),", )), ".\z", "")&"")),
"select Col2"),
"limit "&COUNTA(Q2:Q)),
{""; QUERY(TRANSPOSE(QUERY(TRANSPOSE({
QUERY( Q2:Q, "limit "&COUNTA(Q2:Q)),
QUERY(OFFSET(Q2:Q, -1, 0), "limit "&COUNTA(Q2:Q)-1)}),
"select "®EXREPLACE(JOIN(, IF(LEN(Q2:Q),
"max(Col"&ROW(Q2:Q)-ROW(Q2)+1&"),", )), ".\z", "")&"")),
"select Col2
limit "&COUNTA(Q2:Q)-1)}}),
"select "®EXREPLACE(JOIN(, IF(LEN(Q2:Q),
"max(Col"&ROW(Q2:Q)-ROW(Q2)+1&"),", )), ".\z", "")&"")),
"select Col2"))