How to apply multiple criteria when using VLOOKUP+QUERY+IMPORTRANGE? - arrays

So, right now, the formula below successfully queries data from another file, butthis based on a column's data as the criteria. The need is to have more than one criteria and I'm not sure if thecolumn to be compared against needs to come in as an additional query, together with the first query...so I'm lost here.
This is the formula I'm using:
=Arrayformula(
if(F9:F="";"";
vlookup(A9:A;
QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/xxxxXXXXXXXxxxxxXXXX/edit";"Costura!A1:R");
"select Col6, Col8, Col14";0)
;2;0)
)
)
Here's a sample file with dummy data!

can be done like this:
=ARRAYFORMULA(IF(B6:B="",,IFNA(VLOOKUP(A6:A&B6:B,
{QUERY(Data!A1:J, "select A", 0)&
QUERY(Data!A1:J, "select H", 0),
QUERY(Data!A1:J, "select J", 0)}, 2, 0))))
which will translate into this:
=ARRAYFORMULA(IF(B6:B="",,IFNA(VLOOKUP(A6:A&B6:B,
{IMPORTRANGE("1gh5w0czg2JuoA3i5wPu8_eOpC4Q4TXIRhmUrg53nKMU", "Data!A:A")&
IMPORTRANGE("1gh5w0czg2JuoA3i5wPu8_eOpC4Q4TXIRhmUrg53nKMU", "Data!H:H"),
IMPORTRANGE("1gh5w0czg2JuoA3i5wPu8_eOpC4Q4TXIRhmUrg53nKMU", "Data!J:J")}, 2, 0))))

For a system like this, the best practice is just to import the entirety of the imported data in a tab of it's own, then run a vlookup to that tab using "both" criteria. This will simplify your formulas and also does not increase processing time at all. Just hide the "naked" import tab.
I will demonstrate on your sample sheet in two tabs beginning with "MK".
but the basic idea is an ampersanded (&'ed) [search key] in your VLOOKUP()
=ARRAYFORMULA(IFERROR(VLOOKUP(A6:A&B6:B,{MK_Data!A:A&MK_Data!H:H,MK_Data!J:J},2,0)))

Related

Adding In Original Sheet Name With IMPORTRANGE, QUERY, in an Array Formula

Basically I am trying to import several different tabs from one Google Sheets doc and then have that data aggregated into a separate single tab
on a different Google Sheets doc with an extra column indicating which sheet that data originally came from.
So for an example
If my Aggregation sheet was here:
https://docs.google.com/spreadsheets/d/1855Y-TzHtXZi-0g_-Jh8LxuqI0rdJasoSKi9YBkGfs8/edit#gid=0
And my raw data was coming from multiple sheets/tabs (in this example Store 1 and 2) here:
https://docs.google.com/spreadsheets/d/1yar_i4-KdVOT-IgjgUNij48R9yuH-gFNprZgl0X7S94/edit#gid=0
What can be added to my currently functioning formula:
=ArrayFormula(QUERY({
(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1yar_i4-KdVOT-IgjgUNij48R9yuH-gFNprZgl0X7S94/edit#gid=0","'Store 1'!A2:G"))
},"select * where Col1 is not Null"))
to make sure in the Aggregation Sheet, I can tell where that data originally came from by having that source sheet/tab name (Store 1 or 2) in a column?
I have had some success using a similar formula as this in the past to accomplish the same thing:
=ArrayFormula(QUERY({
'Store1'!A2:G,IF(N('Store1'!A2:G),"Store1");
'Store2'!A2:G,IF(N('Store2'!A2:G),"Store2")
},"select * where Col1 is not Null"))
But adding in the IMPORTRANGE functionality in this case seems to prevent that from working :(
Feel free to edit that example Aggregator sheet
If I am completely on the wrong track here feel free to let me know, just wanted to share what has been tried.
Thank you so much for the assistance in advance!
try:
=QUERY({
QUERY(IMPORTRANGE("1yar_i4-KdVOT-IgjgUNij48R9yuH-gFNprZgl0X7S94", "Store 1!A2:G"),
"select Col1,Col2,Col3,Col4,Col5,Col6,Col7,'Store 1' label 'Store 1'''");
QUERY(IMPORTRANGE("1yar_i4-KdVOT-IgjgUNij48R9yuH-gFNprZgl0X7S94", "Store 2!A2:G"),
"select Col1,Col2,Col3,Col4,Col5,Col6,Col7,'Store 2' label 'Store 2'''")},
"where Col1 is not null")

How to Sort or Filter imported IMPORTHTML column with Numbers & Text Strings in it?

I'm basically importing both Tables from this website.
Q1. When I'm importing data via IMPORTHTML and sorting out results, some results are showing as "5 cases or less". It's making it hard to sort it out. I want these to come after 0. How can I make sure of that?
Q2. I wish to import only the numbers from the element. It's basically a whole phrase. How do I only import the last number, in such case, 1,991?
Here is my Sample Google Sheet
try:
=ARRAY_CONSTRAIN(SORT({IMPORTHTML(
"https://santemontreal.qc.ca/en/public/coronavirus-covid-19/",
"table", 1), REGEXREPLACE(SUBSTITUTE(IMPORTHTML(
"https://santemontreal.qc.ca/en/public/coronavirus-covid-19/", "table", 1),
"5 cases or less", 0), "\*", )*1}, 6, 0), 99^99, 2)
not sure if you want 3rd and 4th column too so if you do then change 2 to 4
=REGEXREPLACE(IMPORTXML(
"https://santemontreal.qc.ca/en/public/coronavirus-covid-19/",
"//div[#id='c36390']/h4[2]"), "(.*) ", )*1

How can I do advanced filtering in Google Sheets?

UPDATED AT END
I have a Google Sheet I'm using to track television programs available to the PBS Station I work for. (The link is for a copy of the first 30 rows rather than sharing the full sheet.) Things I need to track and be able to search by are: Title, NOLA Code, Program/Series, Length (half hour increments), Topic, Secondary Topic, Aired (Yes/No), Server/Tape/sIX, Distributor. I have other columns but rarely need to search content by them.
Originally I created a tab for each Topic I have listed and run a filter in an equation to populate the tab based on the master sheet but that's problematic if I find an error - I have to go back to the main sheet to correct it. I find I'm using the Data/Filter View option on the main tab now to save jumping back and forth but it's limiting in that I often need to filter by multiple rows at the same time.
Ideally I would like to create a row for queries (row 2, in this example, would be where I would like to enter query information), and be able to enter data into the cell in each column of this row as needed, and run a script that will then filter each row based on whether it matches all the criteria I have entered - I made each cell I want to be able to filter on orange. And then another script to revert back to an unfiltered sheet.
For example, I often have to look for all programs (not series) that are 30 minutes with a certain topic so I run a Filter View on the Length column, but still have to look through the resulting list to find everything that matches the other 2 criteria I'm looking for. There has to be an easier way. The full database is almost 1300 rows and is growing faster than I'm taking old content out so each search is taking longer and it's getting frustrating.
https://docs.google.com/spreadsheets/d/1TIelXfkJ46NkjcPDTG_seLKR-fqKO5tn3R-OI5p984k/edit?usp=sharing
update
So, I've gotten the method that commented about to mostly work. I get an error saying the equation expected a "(" if the search criteria in P2 is empty, but if it's filled in I get the expected results. I don't understand the "(" error? I have updated my sample sheet to show this in the Search Engine tab. It uses the following formula to query.
=QUERY(Programs!A2:AG,
"where "&TEXTJOIN(" "&A2&" ", 1,
IF(B2<>"", " B='"&B2&"'", ),
IF(C2<>"", " C='"&C2&"'", ),
IF(E2<>"", " E='"&E2&"'", ),
IF(F2<>"", " F='"&F2&"'", ),
IF(G2<>"", " G='"&G2&"'", ),
IF(H2<>"", " H='"&H2&"'", ),
IF(I2<>"", " I='"&I2&"'", ),
IF(O2<>"", " O='"&O2&"'", ),
IF(P2<>"", " P='"&P2&"'")))
Ideally I would like something similar to this but which runs on the Programs tab so I could edit entries directly rather than bouncing back and forth between tabs if I need to make a change.

Dynamically query or filter a table in Google Sheets using checkboxes and searchbox

I have a database of songs and want to use filter or query to return results based on a dashboard with search-boxes and check-boxes. This seems rudimentary, but I can't find what I'm looking for after a few days. Query seems super powerful, but largely when hard coding. I need a user-friendly interface! I've seen some tutorials on incorporating drop-down boxes, but I don't necessarily want to use those.
I couldn't figure out how to add IF() statements to the WHERE clause in query.
=QUERY(A4:G9, "WHERE IF '"(C2=TRUE, "' '"C CONTAINS TRUE"'",1))
This is what I'm starting with, and I know its all kinds of wrong.
GOAL: When the checkbox for MP3 is ticked, all songs with MP3s are listed. But when I also tick the WAV box, only songs with MP3s and WAVs remain, etc.
I will keep the source data and the search key on different tabs. Eventually like to add ranges to the length and bpm boxes too.
Any suggestions on the methods or formulas would be much appreciated.
Please read: Query
=query(A4:G9,"select A,B,C where 0=0"&IF($B$2," and B=TRUE","")&IF($C$2," and C=TRUE",""),1)
try:
=IFERROR(QUERY(A4:G, "where "&TEXTJOIN(" and ", 1,
IF(B2=TRUE, "B=TRUE", ),
IF(C2=TRUE, "C=TRUE", )), 1), QUERY(A4:G))
UPDATE:
=IFERROR(QUERY(A4:G, "where "&TEXTJOIN(" and ", 1,
IF(B2=TRUE, "B=TRUE", ),
IF(C2=TRUE, "C=TRUE", ),
IF(F1<>"", "lower(D) contains '"&F1&"'", )), 1), QUERY(A4:G))

Alternative to QUERY nested in COUNTIF for column array in Google Sheets

Currently I have a formula which is working very well, however, it is making my sheets extremely slow as I am using more than one of these COUNTIF queries.
=IF($O$1="YT",COUNTIFS(
QUERY(Contacts!$Q$3:$EZ, "select V,AC,AJ,AQ,AX,BE,BL,BS, BZ ,CG,CN,CU,DB,DI,DP,DW,ED,EK,ER,EY", 0), $T22,
QUERY(Contacts!$Q$3:$EZ, "select U,AB,AI,AP,AW,BD,BK,BR,`BY`,CF,CM,CT,DA,DH,DO,DV,EC,EJ,EQ,EX", 0), U$21),
COUNTIFS(
QUERY(Contacts!$Q$3:$EZ, "select T,AA,AH,AO,AV,BC,BJ,BQ, BX ,CE,CL,CS,CZ,DG,DN,DU,EB,EI,EP,EW", 0), $O$1,
QUERY(Contacts!$Q$3:$EZ, "select V,AC,AJ,AQ,AX,BE,BL,BS, BZ ,CG,CN,CU,DB,DI,DP,DW,ED,EK,ER,EY", 0), $T22,
QUERY(Contacts!$Q$3:$EZ, "select U,AB,AI,AP,AW,BD,BK,BR,`BY`,CF,CM,CT,DA,DH,DO,DV,EC,EJ,EQ,EX", 0), U$21))
Basically what this does is search the month from a drop-down in the dashboard, and then counts the number of calls by filtering Call Category, Sales Person and Month. The reason I have 2 COUNTIF's is that the first calculates based on the whole year hence YT = "Year Total"
The calls come from an array of columns, hence the query for different columns.
Any feedback would be much appreciated.
I'm more proficient with functions than I am with app script, but always willing to learn, so if there is an app script solution I would be more than grateful to learn.
I look forward to learning from you.
you will need to kill all your query formulas (all those which looks like the example in your question) and replace them with this build:
=IF($G$2="Year Total",
COUNTA(FILTER(Contacts!V3:V, Contacts!V3:V=$B12, Contacts!U3:U=E$11))+
COUNTA(FILTER(Contacts!AC3:AC,Contacts!AC3:AC=$B12,Contacts!AB3:AB=E$11))+
etc...
,COUNTA(FILTER(Contacts!T3:T, Contacts!T3:T=$G$2, Contacts!V3:V=$B12, Contacts!U3:U=E$11))+
COUNTA(FILTER(Contacts!AA3:AA,Contacts!AA3:AA=$G$2,Contacts!AC3:AC=$B12,Contacts!AB3:AB=E$11))+
etc...
)
this will give you ~80% boost
Can you share your data or at least post a screenshot showing an idea how they look like? That always helps in understanding what you want to do.
Anyway, the first thing I'd do would be to replace your QUERY() calls with straight {} addressing, that alone should speed things up. See if this helps:
=IF($O$1="YT",
COUNTIFS(
{V,AC,AJ,AQ,AX,BE,BL,BS,BZ,CG,CN,CU,DB,DI,DP,DW,ED,EK,ER,EY}, $T22,
{U,AB,AI,AP,AW,BD,BK,BR,BY,CF,CM,CT,DA,DH,DO,DV,EC,EJ,EQ,EX}, U$21),
COUNTIFS(
{T,AA,AH,AO,AV,BC,BJ,BQ,BX,CE,CL,CS,CZ,DG,DN,DU,EB,EI,EP,EW}, $O$1,
{V,AC,AJ,AQ,AX,BE,BL,BS,BZ,CG,CN,CU,DB,DI,DP,DW,ED,EK,ER,EY}, $T22,
{U,AB,AI,AP,AW,BD,BK,BR,BY,CF,CM,CT,DA,DH,DO,DV,EC,EJ,EQ,EX}, U$21))
Another simple speedup possible would be to delete any rows below the last row for which you have data.

Resources