How to rank a cell value within the same cell - google-sheets

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))

Related

Google sheets query multiple rows in one by multiple criteria

For example:
Line 1 : 0 abc 16.10.2022 18.10.2022 1500 0 79
Line 2 : 425 abc 17.10.2022 19.10.2022 799 15 145
Line 3: “empty” abc 17.10.2022 20.10.2022 600 0 34
Line 4: 588 “empty” 01.10.2022 12.10.2022 800 30 15
Line 5: 588 dca 02.10.2022 08.10.2022 300 0 35
Output: 2 lines
425 abc 17.10.2022(max date 1) 19.10.2022(max date 2) 2899(sum1) 15(sum2) 258(sum3)
588 dca 02.10.2022(max date 1) 08.10.2022 (max date 2) 1100(sum 1) 30(sum 2) 50(sum3)
Any ideas? Empty =“”.
I am use filter for first 2 columns, and after that vloop, but filter and unique doesn’t give me perfect result (some data is lost).
use:
={QUERY(QUERY(FILTER(A1:G, REGEXMATCH(B1:B, TEXTJOIN("|", 1, IF((B1:B<>"")*(A1:A=""), B1:B, )))),
"select max(Col1),Col2,max(Col3),max(Col4),sum(Col5),sum(Col6),sum(Col7)
where Col1 is not null
group by Col2"), "offset 1", );
QUERY(QUERY(FILTER(A1:G, REGEXMATCH(A1:A&"", TEXTJOIN("|", 1, IF((A1:A<>"")*(B1:B=""), A1:A, )))),
"select Col1,max(Col2),max(Col3),max(Col4),sum(Col5),sum(Col6),sum(Col7)
where Col1 is not null
group by Col1"), "offset 1", )}
could be written as:
=LAMBDA(a, b, c, d, e, {
QUERY(QUERY(FILTER(c, REGEXMATCH(b, TEXTJOIN("|", 1, IF((b<>"")*(a=""), b, )))),
"select max(Col1),Col2"&e&"Col2"), d, );
QUERY(QUERY(FILTER(c, REGEXMATCH(a&"", TEXTJOIN("|", 1, IF((a<>"")*(b=""), a, )))),
"select Col1,max(Col2)"&e&"Col1"), d, )})
(A1:A, B1:B, A1:G, "offset 1",
",max(Col3),max(Col4),sum(Col5),sum(Col6),sum(Col7) where Col1 is not null group by ")

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), "♦")), "♠♥"))

Count how many times appears text across columns

I'm looking for the formula to count how many times appears a color, for example, "red", from B2:D7 if A2:A7 is "B".
.
=COUNTA(IFERROR(QUERY(A2:D,
"select A
where A = 'B'
and (B = 'RED'
or C = 'RED'
or D = 'RED')", 0)))
=ARRAYFORMULA(COUNTA(IFERROR(QUERY(TRANSPOSE(TRIM(QUERY(TRANSPOSE(FILTER(
IF(B2:D="RED", 1, ), A2:A="B")),,999^99))), "where Col1 is not null", 0))))
=ARRAYFORMULA(COUNT(IFERROR(FILTER(
IF(B2:D="RED", 1, ), A2:A="B"))))

Search 2 columns and return all matches in a transposed row

I'm trying to take this QUERY:
=IFERROR(TRANSPOSE(QUERY(Items!$A$2:$D,"SELECT D WHERE A = '"&A2&"' and B = '"&B2&"' and C = '"&C2&"' and C is not NULL and D is not NULL", 0)),)
And turn it into a formula that I can enter one time at the top of a sheet and have it apply to all cells in the column below.
I've tried manipulating various other formulas and answers I've found/received here on SO, but getting nothing but errors.
My sheet
=ARRAYFORMULA(IFERROR(VLOOKUP(Estimate!A2:A&Estimate!B2:B,
TRIM(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
{INDEX(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)),
Items!A2:A&Items!B2:B&"♦"&Items!C2:C, )), 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(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)),
Items!A2:A&Items!B2:B&"♦"&Items!C2:C, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0)),
QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(Items!A2:A&Items!B2:B))*(LEN(Items!C2:C)),
Items!A2:A&Items!B2:B&"♦♥"&Items!C2:C, )), 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)))
Thank you for that enormous formula! And to think I was trying to accomplish this using a simple FILTER. Would this be the appropriate modification to include another column (column C) in the search criteria?
=ARRAYFORMULA(IFERROR(VLOOKUP('task-itemAssociationsDV'!A2:A&'task-itemAssociationsDV'!B2:B&'task-itemAssociationsDV'!C2:C,
TRIM(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
{INDEX(QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(taskData!A2:A&taskData!B2:B&taskData!C2:C))*(LEN(taskData!D2:D)),
taskData!A2:A&taskData!B2:B&taskData!C2:C&"♦"&taskData!D2:D, )), 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(taskData!A2:A&taskData!B2:B&taskData!C2:C))*(LEN(taskData!D2:D)),
taskData!A2:A&taskData!B2:B&taskData!C2:C&"♦"&taskData!D2:D, )), 1, 1), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0)),
QUERY(IFERROR(SPLIT(SORT(UNIQUE(IF((LEN(taskData!A2:A&taskData!B2:B&taskData!C2:C))*(LEN(taskData!D2:D)),
taskData!A2:A&taskData!B2:B&taskData!C2:C&"♦♥"&taskData!D2:D, )), 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)))

Resources