Google sheets query multiple rows in one by multiple criteria - google-sheets

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

Related

Google Sheets arrayformula vlookup transpose matching values and return all unique values in another sheet

I need an arrayformula to lookup all matching cells in another sheet and transpose and return all corresponding unique values.
Here is my sheet:
https://docs.google.com/spreadsheets/d/1uqeM6M9MAPehgyqyRLmH9mgg3Jh_RUSxADMisarht5Y/edit?usp=sharing
I've tried this but it doesn't work:
=ARRAYFORMULA(IFERROR(VLOOKUP(TRANSPOSE(A2:A), Sheet2!A2:B, 2, 0)))
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A,
{SORT(UNIQUE(INDIRECT("Sheet2!A2:A"&COUNTA(Sheet2!A2:A)+1))),
SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(ISNUMBER(QUERY(QUERY(UNIQUE(Sheet2!A2:B),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0), "offset 1", 0)),
"♦"&QUERY(UNIQUE(Sheet2!A2:B),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2 limit 0", 0), ))
,,999^99))), "♦")}, {1,2,3,4,5,6}, 0))

How do I count the number of times a column consecutively decreases in value

I have a spreadsheet where a column contains a person's weight. I want to count how many times in a row their weight has decreased in consecutive entries for both the current weight and all time, so:
108
107
106
105
104
106
104
103
Should return 3 as it's decreased three times in a row at the end, and also 5 as it decreased 5 times in a row at the start. Those values are in a column not a row in the sheet. How do I do this?
paste in D2 cell:
=ARRAYFORMULA(IF(LEN({A2; A2:A}), IF(B2:B901 < {1000; B2:B900}, 1, ), ))
paste in E2 cell:
=ARRAYFORMULA(IF(D2:D901=1,
MMULT(N(ROW(D2:D901)>=TRANSPOSE(ROW(D2:D901))), N(D2:D901=1))-HLOOKUP(0,
MMULT(N(ROW(D2:D901)> TRANSPOSE(ROW(D2:D901))), N(D2:D901=1)), MATCH(
VLOOKUP(ROW(D2:D901), IF(N(D2:D901<>D1:D900), ROW(D2:D901), ), 1, 1),
VLOOKUP(ROW(D2:D901), IF(N(D2:D901<>D1:D900), ROW(D2:D901), ), 1, 1), 0), 0), ))
and make sure your sheet has a minimum of 901 rows
I managed to do the current streak without a helper column, it also calculates the days the streak has continued for (which wasn't what I asked but is better for me). Not sure if answering my own question is the right response here, I'm not experienced with this website.
=iferror(sum(query(query({Log!A2:A, ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, Log!A2:A - offset(Log!A2:A, -1,,counta(Log!A2:A)+1 ),0 ), )), ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, if(len(Log!B2:B), Log!B2:B < offset(Log!B2:B, -1,,counta(Log!B2:B)+1 ), true),TRUE ), )), Log!B2:B}, "select * where Col1 is not null order by Col1 desc limit " & match(False, query(query({Log!A2:A, ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, Log!A2:A - offset(Log!A2:A, -1,,counta(Log!A2:A)+1 ),0 ), )), ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, if(len(Log!B2:B), Log!B2:B < offset(Log!B2:B, -1,,counta(Log!B2:B)+1 ), true),TRUE ), )), Log!B2:B}, "select * where Col1 is not null order by Col1 desc"), "select Col3"), 0)-1), "select Col2")), 0)
And to count the max streak:
=max(transpose(index(query({0}, "select " & join(",", unique(transpose(split(substitute(join(" + ", query({Log!A2:A, ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, if(Log!B2:B < offset(Log!B2:B, -1,,counta(Log!B2:B)+1,), Log!A2:A - offset(Log!A2:A, -1,,counta(Log!A2:A)+1 ), 0),0 ), )), ArrayFormula(if(len(Log!A2:A), if(row(Log!A2:A) > 2, if(len(Log!B2:B), Log!B2:B < offset(Log!B2:B, -1,,counta(Log!B2:B)+1 ), false),false ), )), Log!B2:B}, "select Col2 where Col1 is not null order by Col1 desc")), " + 0 + ", ","), ","))))), 2)))

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

Transpose Unique Values from Multiple Columns

I'm trying to generate unique values in D1:F but my formula is not working. For example, row 1 has
A B C
milk milk 44
the output should be:
D E
milk 44
here is my formula and my sheet
=ARRAYFORMULA(unique(A1:C))
UNIQUE works only in one dimension (row or column)
=QUERY(UNIQUE({A:A;B:B;C:C}), "where Col1 is not null", 0)
=ARRAYFORMULA(SPLIT(REGEXREPLACE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE({QUERY(QUERY(
UNIQUE(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(LEN(A2:A), "♠"&A2:A&"♦"&B2:D, )),,999^99)),,999^99)), "♠")), "♦")),
"select Col1, count(Col1) where Col1 is not null group by Col1 pivot Col2", 0),
"select Col1 offset 1",0),
IF(QUERY(QUERY(UNIQUE(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(LEN(A2:A), "♠"&A2:A&"♦"&B2:D, )),,999^99)),,999^99)), "♠")), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0), "offset 1",0)<>"",
QUERY(QUERY(UNIQUE(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(LEN(A2:A), "♠"&A2:A&"♦♀"&B2:D&",", )),,999^99)),,999^99)), "♠")), "♦")),
"select count(Col1) where Col1 is not null group by Col1 pivot Col2", 0), "limit 0",1),)})
,,999^99))), ", ♀", ", "), ",$", ), "♀"))

Scan column names, pick the ones containing specific text, and run functions on them (SUM, MAX etc)

In Google Sheets, I have a spreadsheet that looks like this
A1 A2 A3 B1 B2 B3 C1 C2 C3
55 23 21 15 18 15 61 51 51
51 15 15 81 98 13 51 74 65
15 87 89 99 32 72 15 58 51
18 64 15 81 32 15 81 78 98
87 81 32 51 15 35 15 81 12
I want to create a separate sheet that will find the sum/max/average/min of all the "A" named columns.
Is there a way to make a function in a cell that will (lets' pick one for now) average all the cells in the same row whose column names match a certain text pattern (e.g. "A*")?
=ARRAYFORMULA({
"sum", SUM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(TO_TEXT(A1:I)),
"where Col1 contains 'A'", 0)), "where Col1 !='' offset 1", 0)*1);
"max", MAX(QUERY(TRANSPOSE(QUERY(TRANSPOSE(TO_TEXT(A1:I)),
"where Col1 contains 'A'", 0)), "where Col1 !='' offset 1", 0)*1);
"average", AVERAGE(QUERY(TRANSPOSE(QUERY(TRANSPOSE(TO_TEXT(A1:I)),
"where Col1 contains 'A'", 0)), "where Col1 !='' offset 1", 0)*1);
"min", MIN(QUERY(TRANSPOSE(QUERY(TRANSPOSE(TO_TEXT(A1:I)),
"where Col1 contains 'A'", 0)), "where Col1 !='' offset 1", 0)*1)})
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
QUERY(TRANSPOSE(QUERY(TRANSPOSE(TO_TEXT(Raw!$A1:$AR)),
"where Col1 contains '"&REGEXEXTRACT(C1, ".")&"'", 0)),
"where Col1 !='' offset 1", 0)*1),
"select "&REGEXREPLACE(JOIN( , IF(LEN(Raw!$C2:$C),
"max(Col"&ROW(Raw!$C2:$C)-ROW(Raw!$C2)+1&"),", )), ".\z", "")&"")),
"select Col2"))
You can use an array formula like this:
ArrayFormula(average(if(left(A1:I1,1)="A",A2:I)))
Or if preferred use pattern matching for more flexibility:
=ArrayFormula(average(if(regexmatch(A1:I1,"^A"),A2:I)))
(case sensitive).

Resources