=sort({QUERY(Kraft!A2:K500, "SELECT A,G,H WHERE G!='' AND J=''", 1); QUERY(Beacon!A2:H500, "SELECT A,B,F WHERE B!='' AND G=''", 1)},3,true)
Comes out like this
Pro Number 1 Trailer 1 Date 1
Pro Number 2 Trailer 2 Date 2
Pro Number 3 Trailer 3 Date 3
Pro Number 4 Trailer 4 Date 4
I would like to append the sheet name, or even simply text (sheet names shouldnt change much, so I can manually update the queries as needed)
Kraft Pro Number 1 Trailer 1 Date 1
Beacon Pro Number 2 Trailer 2 Date 2
Kraft Pro Number 3 Trailer 3 Date 3
Beacon Pro Number 4 Trailer 4 Date 4
Also I should note that there will be about 10 queries in total
Try this
=SORT({
TRANSPOSE(SPLIT(REGEXREPLACE(REPT(","&"Kraft",
COUNTA(QUERY(Kraft!A2:K500, "SELECT A,G,H WHERE G!='' AND J=''", 1))),"^.",""),",")),
QUERY(Kraft!A2:K500, "SELECT A,G,H WHERE G!='' AND J=''", 1);
TRANSPOSE(SPLIT(REGEXREPLACE(REPT(","&"Beacon",
QUERY(Beacon!A2:H500, "SELECT A,B,F WHERE B!='' AND G=''", 1)),"^.",""),",")),
QUERY(Beacon!A2:H500, "SELECT A,B,F WHERE B!='' AND G=''", 1)},4,true)
try:
=SORT({
QUERY(Kraft!A2:K500,
"select 'Kraft', A,G,H where G is not null and J='' label'Kraft'''", 1);
QUERY(Beacon!A2:H500,
"select 'Beacon',A,B,F where B is not null and G='' label'Beacon'''", 1)}, 4, 1)
update:
=SORT({
IFERROR(QUERY(Kraft!A2:K500,
"select 'Kraft', A,G,H where G is not null and J='' label'Kraft'''", 1), {"","","",""});
IFERROR(QUERY(Beacon!A2:H500,
"select 'Beacon',A,B,F where B is not null and G='' label'Beacon'''", 1), {"","","",""})}, 4, 1)
Related
My question was inspired by this post in that I'm wondering if it's possible to create a formula to stack a dynamic amount of arrays based on a list (see below for clarification).
Sample Starting Data From Three Sources
ID
Amount
India
9
Delta
4
Hotel
8
ID
Amount
Alpha
1
Echo
5
Foxtrot
6
ID
Amount
Bravo
2
Gulf
7
Charlie
3
Desired final result:
ID
Amount
Alpha
1
Bravo
2
Charlie
3
Delta
4
Echo
5
Foxtrot
6
Gulf
7
Hotel
8
India
9
I can get the final result by using a query function as shown in this spreadsheet with a formula referencing the appropriate cells with fileID and range:
=Query({IMPORTRANGE(E2,F2);
IMPORTRANGE(E3,F3);
IMPORTRANGE(E4,F4)},"Select * where Col1 is not null order by Col1",1)
if you want to play with it in your own sheet, you could use this hard-coded function which is the same as above:
=Query({IMPORTRANGE("1WtI56_9mhyArMn_j_H4pZg8E0QdIBaKoJfAr-fDAoE0","'Sheet1'!A:B");
IMPORTRANGE("1HamomAuLtwKJiFEtRKTuEkt--YDTtWChUavetBcAcBA","'Sheet1'!A2:B");
IMPORTRANGE("1WtI56_9mhyArMn_j_H4pZg8E0QdIBaKoJfAr-fDAoE0","'Sheet2'!A2:B")},"Select * where Col1 is not null order by Col1",1)
My Question:
Is there a way to leverage a formula to generate this result based on the number of file ids and ranges in columns E and F? So if a fourth ID and range were added, the desired result in columns a and b would be shown? I suspect Lambda would work, but I am not as strong with it as I should be.
Unsuccessful attempt:
=lambda(someIDs,SomeRanges,IMPORTRANGE(someIds,SomeRanges))(filter(E2:E,E2:E<>""),filter(F2:F,F2:F<>""))
REALLY Bad Attempts:
=contact(Player()*1800-CoffeeBribe*Not(Home))
=company(theMaster(emailed)*(false))<>🐇
All helpful answers will be upvoted if not accepted. Thanks.
if ranges would be the same:
=LAMBDA(x, QUERY(REDUCE({"ID", "Amount"}, x,
LAMBDA(a, c, {a; IMPORTRANGE(c, "Sheet1!A2:B")})),
"where Col1 is not null", 1))
(E2:INDEX(E:E, MAX((E:E<>"")*ROW(E:E))))
if ranges are not the same:
=INDEX(LAMBDA(x, y, QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(MAP(x, y,
LAMBDA(e, f, QUERY("♣"&FLATTEN(QUERY("♥"&TRANSPOSE(
IMPORTRANGE(e, f)),,9^9)),,9^9))),,9^9),
"♣")), "♥"), "where Col1 <> ' ' order by Col2", 1))(
E2:INDEX(E:E, MAX((E:E<>"")*ROW(E:E))),
F2:INDEX(F:F, MAX((F:F<>"")*ROW(F:F)))))
or:
=LAMBDA(x, QUERY(REDUCE({"ID", "Amount"}, x,
LAMBDA(a, b, {a; IMPORTRANGE(b, OFFSET(b,,1))})),
"where Col2 is not null", 1))
(E2:INDEX(E:E, MAX((E:E<>"")*ROW(E:E))))
in old days it would be solved by generating it:
={""; INDEX("={"&TEXTJOIN("; ", 1, "IMPORTRANGE("""&
FILTER(E2:E, E2:E<>"")&""", """&FILTER(F2:F, F2:F<>"")&""")")&"}")}
REDUCE accepts and returns arrays. We can use it to stack ranges. INDEX/COUNTA can be used to get the range needed without blanks. OFFSET can be used to get the next column's value.
=QUERY(
REDUCE(
{"Id","Amount"},
E2:INDEX(E2:E,COUNTA(E2:E)),
LAMBDA(
a,e,
{a;IMPORTRANGE(e,OFFSET(e,0,1))}
)
),
"Select * where Col1 is not null order by Col1",
1
)
I have 2 sheet2 on the same file:
the first collect the answers of different trial judge and the second should make the total
Like this:
First sheet
Name q1 q2 q3 total judge_id
Bob 1 5 8 14 1
Jeff 2 4 2 8 1
Bob 3 1 4 8 2
Bob 5 3 2 10 3
Jeff 6 1 8 15 3
Second sheet
judge_id 1 2 3 tot
Bob 14 8 15 37
Jeff 8 # 15 23
How can I sum only the row on a 'person' in particolar?
There is a fast way to do it without open Google Script?
try:
=ARRAYFORMULA({QUERY({A2:F},
"select Col1,sum(Col5) where Col1 is not null group by Col1
pivot Col6 label Col1'judge_id'"), {"tot"; MMULT(QUERY(QUERY({A2:F},
"select sum(Col5) where Col1 is not null group by Col1 pivot Col6"),
"offset 1", )*1, SEQUENCE(COUNTUNIQUE(F2:F), 1, 1, ))}})
Here's my take on it:
={
QUERY(A1:F,"SELECT A, SUM(E) WHERE A IS NOT NULL GROUP BY A PIVOT F LABEL A 'Judge ID'"),
QUERY(A1:F,"SELECT SUM(E) WHERE A IS NOT NULL GROUP BY A")
}
A B C
1 Timestamp Hours Worked Total Hours
2 1/2/2022 17:33:41 6
3 1/3/2022 19:59:41 2 January =
4 1/7/2022 13:20:51 1
5 1/9/2022 12:49:02 3 February =
6 1/15/2022 12:04:21 3
7 2/16/2022 15:58:10 9 March =
8 2/22/2022 11:57:31 3
9 2/24/2022 5:45:12 5
10 2/2/2022 17:33:41 4
11 2/3/2022 19:59:41 2
12 2/7/2022 13:20:51 1
13 3/9/2022 12:49:02 3
14 3/15/2022 12:04:28 3
15 3/16/2022 15:58:10 7
16 3/22/2022 11:57:31 3
17 3/24/2022 5:45:12 5
I would like to sum the hours by date/month. I can sum the hours by
column, =sum(b2:b). But how do I sum the hours in Column B by
date/month of column A?
In another post, a member referenced the 'Google Sheets function list'.
I have found that to be very helpful on other things I have been doing.
But I am still at a lose for this problem.
John
You could use the query function with SQL:
=QUERY({(data)}, "select sum(Col2) group by Col1 label Col1 'Date', Col2 'Sum'")
That would return a new table, and you can adjust it by changing the second parameter, representing the SQL string. You can view Google's docs for the query function here. In addition you can find their docs for the query language here.
The simplest solution, without any formula, is to build a pivot table.
use:
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY({TEXT(A2:A, "yyymm\×mmmm"), B2:B},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label sum(Col2)''")), "(.*×)", ))
Suppose I have a table like so:
one
ID
three
a
2
one
b
7
two
c
6
three
a
9
four
b
3
five
c
1
six
a
5
seven
b
10
eight
c
8
nine
a
4
ten
I want to GROUP BY one, get MAX of ID and then get the associated value from three.
I can do the first part like so:
=QUERY(A1:C11, "SELECT A, MAX(B) GROUP BY A")
To get:
one
max ID
a
9
b
10
c
8
But I want to get:
one
max ID
three
a
9
four
b
10
eight
c
8
nine
I am trying to do this all with one QUERY. I know I could use a VLOOKUP for the 3rd column but I'm hoping there is way to do with one QUERY.
From the Query Language Reference documentation, it is explicity stated in the rules of the GROUP BY clause that every column in the SELECT must be a grouped column -or- wrapped by an aggregation function. This is why it is not possible to include an ungrouped, unaggregated column in your specific query.
You can do the workaround as per player0's answer, but if you want to use QUERY() andVLOOKUP() in a single formula you can use this as well:
=ARRAYFORMULA({{QUERY(A1:C,"SELECT A, max(B) where A is not null group by A")},{VLOOKUP(FILTER(F:F,LEN(F:F)),SORT(B1:C,1,TRUE),2)}})
Sample:
This should also work. You can & the columns together pre-query, then split them out afterwards.
=ARRAYFORMULA(QUERY(SPLIT(QUERY({A:A,TEXT(B:B,"000000000")&"|"&C:C&"|"&A:A},"select MAX(Col2) where Col1<>'' group by Col1",1),"|"),"select Col3,Col1,Col2"))
use:
=SORTN(SORT(A2:C, 2, 0), 9^9, 2, 1, 1)
update:
={QUERY(source!A:E,
"select B,C,max(A) where D is not null group by B,C", 1),
{"value"; ARRAYFORMULA(IF(INDEX(QUERY(QUERY(source!A:E,
"select B,C,max(A) where D is not null group by B,C", 1),
"offset 1", 0),,1)<>"",
VLOOKUP(INDEX(QUERY(QUERY(source!A:E,
"select B,C,max(A) where D is not null group by B,C", 1),
"offset 1", 0),,3), source!A:E, 5, 0), ))}}
I have 3 sheets that have the exact same format
Sheet1
A B C D
George 10 2 8
Nick 15 89 0
Mike 13 1 50
Lucas 9 -5 12
Sheet2
A B C D
Nick 1 9 5
Mike 1 10 6
George 11 22 5
Lucas 10 5 2
Panos 55 0 1
Sheet3
A B C D
Panos 0 9 1
George 1 2 5
Nick 7 2 1
Lucas 1 5 1
I want to query the range {'Sheet1'!A1:D5; 'Sheet2'!A1:D5; 'Sheet3'!A1:D5}
And get something like MAX(Col2:Col4) Group By Col1
Which would return something like:
George 22
Nick 89
Mike 50
Lucas 12
Panos 55
I tried:
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(Col2:Col4) Group by Col1 Label MAX(Col2:Col4) '' " ),2, FALSE)
and
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(MAX(Col2),MAX(Col3), MAX(Col4)) Group by Col1 " ),2, FALSE)
Both didn't work. Any ideas?
Please try:
=query(sort(transpose(query({Sheet1!A1:D5;Sheet2!A1:D5;Sheet3!A1:D5},"select max(Col2), max(Col3), max(Col4) pivot Col1"))),"select Col1, max(Col2) group by Col1 label(Col1) ''")
To sum up your question, It requires finding the MAX across the columns to the right as well as down. As such, QUERY does NOT have such 2D function.
So, Use a Helper column E&F in each sheet:
Max of B&C:
E2:
=ARRAYFORMULA(IF(B2:B>C2:C,B2:B,C2:C))
Max of B,C&D:
F2:
=ARRAYFORMULA(IF(D2:D>E2:E,D2:D,E2:E))
Now, Use Query:
Query:
=ARRAYFORMULA(QUERY({Sheet1!A2:F;Sheet2!A2:F;Sheet3!A2:F}, "Select Col1,max(Col5) where Col1 is not null group by Col1 order by max(Col5) desc"))
Notes:
Change ranges to suit
You could also simply use MAX for each row without the ARRAYFORMULA
Theoretically, For a single cell solution, You could enter this formula to find the max of 3 real numbers
Another approach perhaps a bit simpler but needing two queries
=sort(unique(({Sheet1!A1:A5;Sheet2!A1:A5;Sheet3!A1:A5})))
to get the names starting in (say) F2
Then this to get the maximum values for each name in (say) G2 and pulled down
max(query({Sheet1!A$1:D$5;Sheet2!A$1:D$5;Sheet3!A$1:D$5},"select max(Col2),max(Col3),max(Col4) where Col1='"&F2&"'"))