How to importrange dynamically based on dropdown/variable - google-sheets

I'm trying to dynamically retrieve all rows where a status = [Dynamically chosen value] from a drop down list.
below you can see my current formula:
=query(IMPORTRANGE("https://example.com", "Tracker!A1:V"), "select * where Col7 = 'B1' ")
Here is an example where you can see what I'm trying to do (Status is A1, and A2 is the formula grabbing all rows from another sheet)
If I select the "A1" in the formula is brings up "In Progress" so it appears to be referencing the correct value, but it doesnt work unless I explicitly write the status into the formula as a text object..
As noted below:

to use cell references, you have to escape them using '"&cell&"'
QUERY(IMPORTRANGE("your_url","your_sheet!your_range"),"Select * where Col7 = '"&A1&"'",0))

Related

Using Indirect function as a dynamic input inside the query function in google sheet

I have a google sheet (Final Calculation) where I am trying to get sum of all values in Column G (in Data tab) against a set of values in Column B, L and D (in Data tab). However, condition for values in Column D is dynamic and would likely change every month. I have created a tab Advertiser List where the condition for filter in Column D gets updated. How, do I use this condition in Query function used in C5 cell in Final Calculation tab?
I tried referencing the condition in Advertiser List sheet using indirect but I am getting an error. I have also added a hard coded formula is other cells in Final Calculation tab to show what am I trying to achieve.
Below is the formula that I have written and is showing an error:
=iferror(QUERY(Data!$A:$L, "Select Sum(G) where B = '"&$B5&"' and L = '"&C$3&"' and indirect('"Advertiser List"'&'"!"'&'"D2"') label sum(G)'' "),0)
Here is the link of the Google Sheet (Link)
You don't need INDIRECT to get a text value from another tab:
=QUERY(Data!$A:$L, "Select Sum(G) where B = '"&$B5&"' and L = '"&C$3&"' and (" & 'Advertiser List'!D1 & ") label sum(G)'' ")
When in doubt, try to build the "select ... " string in a separate cell until you see the expected result. Then wrap it with query and check again. Adding IFERROR should always be your last step, because you will want to see the errors that your query is throwing.

Google Sheets Query Error: Query Not Returning proper Array value when a condition is TRUE

I'm trying to solve why one of the Google Sheets I maintain is failing to operate properly. I've isolated that the issue is with a query
QUERY(Import_MJL!A:BU, "select A where AJ> 0")
For reference the Import_MJL sheet is a static database. The AJ column holds a currency value. When performing an IF function of that column, it returns as TRUE as seen here
if(Import_MJL!AJ2 > 0,1,0)
What could be the reason that the query is not recognizing when my AJ Column is above 0 ?
Screenshot for bellow reference. Row 1 holds the code & row 2 holds the Results (The B2 Cell should result in an array whose length is over 10K but only returns the header. The Import_MJL!AJ2 cell has value 38K and should be a result of the cell on B2)
Screenshot of code and results
Thanks for the time y'all!
Chances are that Import_MJL!AJ2 does not contain a number but the text string $38,738.00 that only looks like a number. Many spreadsheet functions will automatically convert such values into numbers, but query() will not.
You can check whether a value is actually a number with the isnumber() function.
Here's a quick fix to try:
=arrayformula(
query(
{ Import_MJL!A1:AI, value(Import_MJL!AJ1:AJ), Import_MJL!AK1:BU },
"select Col1 where Col36 > 0",
1
)
)

Array cell reference in where clause of query

Google Sheets sample file:
https://docs.google.com/spreadsheets/d/1WqqiysWdYMAop5uQXZnisuX291wY2PFmAMEKZhya2IM/edit?usp=sharing
I am trying to calculate the average value of each person for each month. My two variables to match are the names and month of the date. Currently I am able to get the correct values if I am applying the formula to each cell using this formula:
=IFERROR(QUERY(Sheet2!A:C,"Select avg(C) where A = '"&A2&"' and month(B) = month(date '"&TEXT(DATE(2021,MONTH(DATEVALUE($A$1&"1")),1),"yyyy-mm-dd")&"') label avg(C) '' "),"-")
What I want to do is be able to do an arrayformula instead so I will only need to input the formula once and wont have to worry about the number of names entered. I tried using this formula:
=ARRAYFORMULA(IFERROR(QUERY(Sheet2!A:C,"Select avg(C) where A = '"&A2:A&"' and month(B) = month(date '"&TEXT(DATE(2021,MONTH(DATEVALUE($A$1&"1")),1),"yyyy-mm-dd")&"') label avg(C) '' "),"-"))
, but it only calculates the average for that row.
If possible, the solution should also be using query since in the data I will be using it on I am not able to use sumifs/averageifs for some reason.
try:
=QUERY('data source'!A2:C,
"select A,avg(C)
where month(B)+1 = "&MONTH(A1&1)&"
group by A
label avg(C)''")
=ARRAYFORMULA({SORT(UNIQUE(FILTER('data source'!A2:A, 'data source'!A2:A<>""))),
IFNA(VLOOKUP(SORT(UNIQUE(FILTER('data source'!A2:A, 'data source'!A2:A<>""))),
QUERY('data source'!A2:C, "select A,avg(C) where month(B)+1 = "&
MONTH(A1&1)&" group by A label avg(C)''"), 2, 0), "-")})

Trigger IMPORTRANGE using query not working

I'm trying to use Query and IMPORTRANGE to get data from another google sheet when a checkbox in the column is TRUE. But I'm just getting an error..
Found this solution:
=QUERY(IMPORTRANGE("G-Sheet URL", "TAB & range"), "where Col15=TRUE", 1)
My code:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o/edit#gid=0", "Spelbricka!C6:C12"), "where Col6=TRUE", 1)
It just gives me error.
Anyone?
Thanks on behalf / R
To import a range if one of your cells in that sheet is set to true, you will be wanting to set everything with an IF condition and wrap the IMPORTRANGE with an ARRAYFORMULA function. ARRAYFORMULA will let you display an array of values in differnt rows and columns rather than a single one avoiding therefore errors (as IF itself would just accept returning a single value), making your non-array function into an array function.
Moreover, with this you can also choose to return another IMPORTRANGE is the checkbox is unmarked. In the following example I'm importing a range if the cell B1 checkbox is ticked.
=(IF(B1=True,ARRAYFORMULA(IMPORTRANGE("SHEETURL","Sheet1!C6:C12")),""))
first run this in any cell and allow access:
=IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!A6")
then try:
=QUERY(IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!A6:E12"); "select Col3 where Col6=TRUE"; 0)
notice the range A6:E12 where we return column C only if column E equals TRUE
UPDATE:
use in C6:
=IF(
IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!"&ADDRESS(MATCH(A6;
IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!A:A"); 0); 3))=TRUE;
IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!"&ADDRESS(MATCH(A6;
IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!A:A"); 0); 3)&":"&ADDRESS(MATCH(A6;
IMPORTRANGE("1ztQla9sOO7xkQdSAMOKHcas81H4jVbYjg9tVi9w1u0o";
"Spelbricka!A:A"); 0)+6; 3)); )
then select C6
copy it with CTRL + C
and paste with CTRL + V into C15, C24, etc.

Google Sheets Arrayformula a Query

I originally have this formula:
=QUERY('Sheet1'!$C$6:$I, "Select F where D contains '"&$B86&"'")
This is applied to every cell from $B86 down to $B145. Users of the sheet accidentally deletes formula and adding protection isn't an option. So I'm thinking of adding the formula to the header only using arrayformula. However, arrayformula can't be used for query.
QUESTION 1:
Is there any other way around to get the same result when only the header has the formula?
={"Messages Sent";ARRAYFORMULA(QUERY('Sheet1'!$C$6:$I, "Select F where D contains '"&B86:B145&"'"))}
This is kinda what I want to achieve. However, this doesn't fill the data from B87 to B145. I only get the header and result for 1 row below the header.
Explanation
This shows the header ={"Messages Sent";
This is supposed to be the arrayformula ARRAYFORMULA(QUERY('Sheet1'!$C$6:$I, "Select F where D contains '"&B86:B145&"'"))}
This is the data from another sheet 'Sheet1'!$C$6:$I
This is the same data from the current sheet to find F in sheet 1 '"&B86:B145&"'
QUESTION 2: HOW CAN I GET THE ANSWER FROM THE QUERY AUTOMATICALLY SET INTO A NUMBER FORMAT SO I CAN DIRECTLY ADD THEM INSTEAD OF FORMATTING THEM MANUALLY AS NUMBER?
try maybe:
={"Messages Sent"; ARRAYFORMULA(IFNA(VLOOKUP(B86:B145,
{REGEXEXTRACT(""&Sheet1!D6:D, TEXTJOIN("|", 1, B86:B145)), Sheet1!F6:F*1}, 2, 0)))}

Resources