Query function with two tabs in google sheets - google-sheets

I have a much larger version of the following tables across two sheets (Cat1, Cat2, respectively)
screengrab
I'd like to get to an output like the following:
City
Category
Boston
Tea
Boston
Beer
Boston
Burger
Where when the matrix = Ready, it shows me city and the category that is ready. I've never used query before, and what I have tried hasn't worked, but this seems like the right tool for the job. Any help is appreciated, TIA!

Try this in cell A1 on a new sheet (delete everything below):
=arrayformula({"City","Category";query(split(flatten({'Cat1'!A2:A&char(9999)&if('Cat1'!B2:D="Ready",'Cat1'!B1:D1,);'Cat2'!A2:A&char(9999)&if('Cat2'!B2:D="Ready",'Cat2'!B1:D1,)}),char(9999)),"where Col2 is not null order by Col1,Col2",0)})
With 12 columns:
=arrayformula({"City","Category";query(split(flatten({'Cat1'!A2:A&char(9999)&if('Cat1'!B2:M="Ready",'Cat1'!B1:M1,);'Cat2'!A2:A&char(9999)&if('Cat2'!B2:M="Ready",'Cat2'!B1:M1,)}),char(9999)),"where Col2 is not null order by Col1,Col2",0)})

Related

Stack multiple queried Importranges with a single word between the two sets of imported data?

I have a master sheet that contains all of the data for other sheets. Column A in the master sheet is the name for who's sheet it will import to. So if the name is "John" then every row that has "John" in column A from the master sheet will be imported into John's sheet. I have the person's name in cell G1 of their own respective sheet and use the following formula.
=ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"'",0),""))
Master sheet looks like this:
Assigned To
Name
State
City
John
Blake G.
Arizona
Phoenix
Andy
Chase C.
Arizona
Phoenix
John
Amy B.
New Mexico
Santa Fe
John
Bill S.
Texas
Austin
John's sheet will look like this:
Name
State
City
Blake G.
Arizona
Phoenix
Amy B.
New Mexico
Santa Fe
Bill
Texas
Austin
I want to separate the data based on what is in Col17 and put a single word between the two data sets. Col17 has either a Y or a N so I can just add AND Col17='N' to the existing formula. I double up the formula, but changed the last condition to be Col17='Y' so that I can pull the two data sets separately and on top of each other.
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='N'",0),""));ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='Y'",0),""))}
What I want to do is add a single word between the two imported sets of data. It should like kinda like this:
Name
State
City
Blake G.
Arizona
Phoenix
Amy B.
New Mexico
Santa Fe
Inactive
Bill
Texas
Austin
I added the word inactive so the middle looks like ""));"Inactive";ARRAY between the two formulas but because of how Query works I get an error saying the array lengths for each formula do not match. Is there alternative to help make this happen or will I need to figure out a workaround with something that isn't Query?
SUGGESTION
Feel free to comment below if ever your question has been misunderstood.
In my understanding, here's your process:
The first formula will list all data that has N value in Col17 .
The second formula will list all data that has Y value in Col17.
This second formula should also have an Inactive title header.
Merge the two formulas separated by the word "Inactive".
If I got it correctly, you could use the label clause to add "Inactive" in your second Query function as seen on this tweaked formula:
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("SHEET_URL","A1:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1 = '"&G1&"' AND Col17 = 'Y'"),0));ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("SHEET_URL","A1:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1 = '"&G1&"' AND Col17 = 'N' label Col2 'Inactive'"),0))}
Demo
Sample result.
E.g. If there's new data added that should be under "Inactive".
Main Sheet
John's Sheet
#SputnikDrunk2 is a very good workaround. Just in case you decide to stack different types of ranges that are not queries, you should consider that they need to have the same amount of columns. So if you add a new row it should have the 12 columns you have from your Queries. One way would be like this:
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='N'",0),""));
{"Inactive","","","","","","","","","","",""};
ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='Y'",0),""))}
Another option to avoid creating so many "","","","" is to use this "trick" that divides a range by 0, and with IFERROR it returns empty cells:
{"Inactive",INDEX (IFERROR(SEQUENCE(1,11)/0,))};
Here's another approach:
=lambda(z,
{filter(choosecols(z,2,3,4,5,6,7,8,9,10,11,12,13),index(z,,1)=A1,index(z,,17)="N");
{"Inactive",makearray(1,11,lambda(r,c,iferror(1/0)))};
filter(choosecols(z,2,3,4,5,6,7,8,9,10,11,12,13),index(z,,1)=A1,index(z,,17)="Y")}
)(A1:Q4)
A1:Q4 part in the formula should be replaced with your importrange Fx
2,3,4,5,6,7,8,9,10,11,12,13 part can also be replaced with shorter sequence(1,12,2,1)

Google Sheets - Trick to have flexible ARRAYFORMULA

first time posting here!
I have the following scenario:
1 Google Sheet with information sorted in tables (Master Data)
1 Google Sheet that =Importrange the data from the Master Data Google Sheet.
I need to import one time, or multiple times, some of the rows that are Imported from the Master Data based on the following criteria:
On the Master Data Google Sheet, a column would be present, showing in which Country/Countries the student lives. If the student lives 1 country, import the row once. If it is in 2,3,4... countries, import the same row it 2,3,4... times.
Right now, I am using the following formula:
=QUERY({IMPORTRANGE(Reference!A8,Reference!$A$2&Reference!B6)},"select Col6 where Col10='"&'Advanced Settings'!B5&"'")
This formula Imports from the Master Data file (Reference!A8), a particular tab (Reference!$A$2) and a particular range in this tab Reference!B6. Finally, it filters the data imported (only the 6th Col of the range, and only if on Col 10 the row has a particular value (Advanced Settings'!B5).
Is there a way to Import the name of the student as many times as countries they live in inside the same Array formula?
Right now, I am just adding more importrange (if there are 3 countries, I will add Importrange three times) with filters, but I would like to make it dynamic for the number of countries, without manual input every time. Also, the number of students imported varies every time so I can't look manually at the number of rows and then add a formula after the last cell of the array formula.
Thanks!
EDIT:
Sample Data and expected result:
Sample Data
Student Name
Gender
Class Level
Home State
Country
Alexandra
Female
4. Senior
CA
UK, US
Andrew
Male
1. Freshman
SD
UK
Anna
Female
1. Freshman
NC
UK, US
Becky
Female
4. Senior
SD
US
Benjamin
Male
4. Senior
WI
UK
Filter on both Class Level (4. Senior) and Country
Name
Reason for appearing (explanation for you)
Alexandra
Appears because Alexandra is Senior, UK
Alexandra
Appears because Alexandra is Senior, US
Becky
Appears because Becky is Senior, US
Benjamin
Appears because Benjamin is Senior, UK
The expected result here is that Alexandra appears twice as she's Senior and both US and UK.
if Reference!B6 is a range and IMPORTRANGE for each country is the same try:
=QUERY({IMPORTRANGE(Reference!A8, Reference!A2&Reference!B6)},
"select Col6
where Col10 matches '"&TEXTJOIN("|", 1, 'Advanced Settings'!B5:B)&"'", )
IF C = Senior and E is splittable, create a array of corresponding As. Then, FLATTEN the array and REDUCE to remove all empty items in the array.
Sample:
=ARRAYFORMULA(
REDUCE(
"Senior List",
FLATTEN(
IF(
C2:C6="4. Senior",
IF(ISTEXT(SPLIT(E2:E6,",")),A2:A6,),
)
),
LAMBDA(a,c,IF(c="",a,{a;c}))
)
)

How to create multiple columns using data from the filter function in Google Sheets

I just started using google sheets to help edit tables for a database and I need some help creating a function. In ColumnA, I've got a list of MovieID's, in ColumnB, there is a list of directors, and in ColumnC, there is a list of unique directors from ColumnB. I'm trying to filter the data and create a set of columns where in one, there is the director's name and in the other, there's the movieID that corresponds to a film the director was involved in.
Ive created a function in F2 to find the ids for a director, and I can just drag the formula down (when the data is transposed, and the C2 value will shift to C3, C4....) But, I want the name of the director to be included in the column to the left of the filtered results (like I've manually done in E), but I can't really figure it out, and I'm not entirely sure on how to use the other functions to get it right.
Any help or guidance on how to achieve this would be greatly appreciated!!
Looks like this will solve your issue
=INDEX(QUERY(SPLIT(FLATTEN(
SPLIT(B2:B,", ",0)&"#"&A2:A),"#"),
" where Col2 is not null order by Col1 "))
If on the other hand there is only one name in cell E2 use
=QUERY(ArrayFormula(IFERROR(SPLIT(IF(REGEXMATCH(B2:B,E2),C2&"-"&A2:A,),"-"))),
"where Col1 is not null",0)

Filter function in Google Sheet - multiple columns

I have a table with names of performers and the activities they performed in which cities. Now, I am trying to find out who all performed in which city using filter formula - Check C15 in below sheet.
https://docs.google.com/spreadsheets/d/19U0vjtABXQ-MltxZoW7v0vjt1L6gM2Lc2VP3Z-BuKwE/edit?usp=sharing
The formula I am trying to use is:
=join(", ",transpose(iferror(filter(A2:A11,REGEXMATCH(C2:G11,"Chicago")))))
If I change G11 to C11 in above formula, I can see some output which is incorrect.
(a) I want the names of performers who performed in Chicago from columns C all the way to G.
(b) Even if I keep it as C11, it gives the name of only the first performer.. and not all those who performed in Chicago in column C
For just Chicago, the output should be:
Nils, Tom in F15
Nils, Tom, Jack, Amanda, Sam in C15
Any help will be greatly appreciated.
=join(", ",transpose(iferror(filter(A2:A11,REGEXMATCH(B2:B11,"Chicago")))))
works for me and I get Nils,Tom in F15.
One way of getting the matches in all columns is to get the row totals like this, and where they aren't zero to join the values in the first column:
=ArrayFormula(textjoin(",",true,if(mmult(n(regexmatch(B$2:G$11,A15)),transpose(column(B:G))),A$2:A$11,)))
You could use something like this:
=join(", ",transpose(filter(A2:A11,REGEXMATCH(C2:C11&D2:D11&E2:E11&F2:F11&G2:G11,"Chicago"))))

Google Sheets query to extract the top three instances according to criteria

I have a Google Spreadsheet with two sheets.
In sheet "Source" I have a series of countries, cities and landmarks - these are,respectively, in columns A, B and C.
In sheet "Sheet for Query", there are two columns: (A) Country, which has a list of unique country names; and (B) Top 3 cities by Landmark. In column B, I would like to have a Query which gives me, for each country, the top three cities by number of landmark, i.e., the query just has to count the number of instances each city in each country appears and return, for each country, the names of the three cities that come up the most times
This is a sample sheet that I've created in order to demonstrate what I mean: https://docs.google.com/spreadsheets/d/1IPwtAHjwjV1A03o9URws-AtDKw3h9QS9UTT0P1PeVN0/edit?usp=sharing.
Thank you!
I've given this some thought and to 'just' count the number of instances and return the top 3 in each country is surprisingly difficult.
The grouping is straightforward with a query like this
=query(A:C," select A,B,count(C) where A<>'' group by A,B order by A,count(C) desc label A 'Country',B 'City', Count(C) 'Landmarks'",1)
But I don't know of a way of getting the top 3 for each group without going through 2 further steps
(1) Number the results in each group (various ways of doing it but here is one)
=(E1=E2)*D1+1
where the country names after grouping are in column E.
(2) Filter the result for the number in column D being less than 4
=filter(E:G,D:D<4)
You don't specify what qualifies as top (so assuming those are the first listed - higher up the sheet), and you don't clarify number of landmark where there are no numbers in your sheet, but perhaps:
=textjoin(", ",,query(Source!A:C,"select B where A='"&A2&"' limit 3"))
in B2 of sheet for Query, copied down to suit.

Resources