How to query based off a cell in google sheets? - google-sheets

I'm trying trying to build out a stock financial analysis spreadsheet this is the example sheet. I have a financials tab where I import the balance sheet, income statement, and the cash flow statement by csv. In another tab I wrote queries that'll populate the financial statements based off of whatever ticker I put into the cell
=QUERY(Financials!B4:M, "SELECT * WHERE B = """&B2&""", ")
=QUERY(Financials!Q4:AB, "SELECT * WHERE Q = """&B2&""", ")
=QUERY(Financials!AH4:AR, "SELECT * WHERE AH = """&B2&""", ")
In the Example sheet I put a blue border where I placed those query functions. The problem I'm having is I keep getting a parse error for all 3.

Try this:
=QUERY({Financials!B4:M}, "SELECT * WHERE Col1 = '"&B2&"'")
=QUERY({Financials!Q4:AB}, "SELECT * WHERE Col1 = '"&B2&"'")
=QUERY({Financials!AF4:AR}, "SELECT * WHERE Col1 = '"&B2&"'")
Output:
Reference:
QUERY

As experienced I am in sheets concatenating strings often gets the best of me with the single and double quotes. I suggest you break the formula down. So, in another cell, get the SELECT clause string working then add back to the QUERY() function.
="SELECT * WHERE Col1 = '"&B2&"'"
This is sometimes referred to as the onion approach. If you get an error on the string formula you can break it down further until your identify where the problem is. In this case it is where you are referencing cells.
Another approach is to start with a hard coded formula then substitute the cell references:
=QUERY(Financials!B4:M, "SELECT * WHERE B = """&B2&""", ")
Becomes:
=QUERY(Financials!B4:M, "SELECT * WHERE B = 'AAPL', 1)
Then substitute 'AAPL' with a cell reference using the concatenate operator (&) and single quotes (since the entire string is using double quotes.
=QUERY(Financials!B4:M, "SELECT * WHERE B = '"&B2&"'", 1)
There is also a CONCATENATE() function which I sometimes use as well
=QUERY(Financials!B4:M, CONCATENATE("SELECT * WHERE B = '",B2,"'"), 1)
Some feel this is easier to follow without all the ampersand operators

Related

Google Sheet Countifs with Multiple Criteria

I'm planning to create a dynamic cell which can count total items or only certain items.
I used the following formula to get the total items. But this formula doesn't work when I filter by item.
=ARRAYFORMULA(SUM(COUNTIFS(A:A;{“D1”;{“APPLE”;”ORANGE”;”POMEGRANATES”}};B:B;”1/6/2022″)))
*D1 = Dropdown for Apple/Orange/Pineapple/Pomegranates
The result I expect is:
If I select Apple then the only value that appears is Apple (4/10).
Apple = 4
Orange = 3
Pineapple = 1
Pomegranates = 2
And
If I don’t select Apple then the values that appear are all values (10/10)
All Fruit = 10
I would be very grateful if you could tell me where the error is and provide a solution.
Demo Sheets
Try this out
=QUERY(
{A:B};
"select Count(Col1)
where
Col1 matches '"&IF(ISBLANK(D1);".*";D1)&"' and
Col2 = date '"&TEXT(DATE(2022;6;1);"yyyy-mm-dd")&"'
label Count(Col1) ''")
If D1 is blank, it'll return everything (.* / the wildcard) -- otherwise it'll pull the word in D1.
If you wanted to use a cell reference for the date, you can replace DATE(2022;6;1) with that cell reference.
Try
=sumproduct(regexmatch(A:A; IF(LEN(D1); D1; "Apple|Orange|Pomegranates|Pineapple")); B:B= date(2022; 6; 1))
and see if that works?
Note that regexmatch is case-sensitive.

=Query Funtion to Pull data from Another Sheet based on cell reference

i am looking for some help to pull the data in Sheets based on another sheet called "ServicesOffered"
here is the sheet link
The idea is simple: If Cell contains Yes then it should add data to WebDev sheet or SEO sheet.
Tried many different formulas but failed to understand how to do this.
Need to have a column list or * after the Select; also shouldn't have curly brackets round the range or else it will treat it as an array and expect Col1, Col2, Col3 etc. in the Select instead of A, B, C etc.
=QUERY(
ServicesOffered!A2:C,
"SELECT * WHERE C = 'Yes' ",1)

Query returning error Unable to parse query string for Function QUERY parameter 2: NO_COLUMN

I am currently working in the following spreadsheet
https://docs.google.com/spreadsheets/d/13KfjUhWSB-BjGyC1G8f8i8o4SPd1kFFLkjN7D6VY8Lk/edit#gid=993210576
In which I am importing data from another worksheet using IMPORTRANGE, and writing a QUERY to match the cells in column B, which correspond to a specific part number, to their corresponding cut quantity found in Column D of the imported sheet. The query I have written is as follows.
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1kFK-ZW8QjtsLYY5twdoMNTdqobGNWIV8nAFBRdouE28/edit#gid=473793446",
"FABRICATION LOG!A78169:K"), "Select Col3 where Col4 = "&B3&" limit 1", 0)`
And is returning the error message:
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: WFR332703
I have used ImportRange for the sheet I am linking to in sheet1 of the spreadsheet linked above, and allowed access, so the error is not there.
Sheet1 is there to display the values returned for the IMPORTRANGE so that I can manually look up values I am expecting to get. Now for some of these cells, I expect to not get a value, as these will not be in the sheet I'm importing. But for others, I am expecting a numerical value, which is not being returned. I suspect this may have something to do with the fact that there is a mismatch between Datatypes since the entries in column b are both letters and numbers, but this is only a hunch with no actual facts to back it up. If anyone has any suggestions It'd be greatly appreciated.
first, paste this into some cell and connect you sheets by allowing access:
=IMPORTRANGE("1kFK-ZW8QjtsLYY5twdoMNTdqobGNWIV8nAFBRdouE28", "FABRICATION LOG!A1")
then use the formula:
=QUERY(IMPORTRANGE("1kFK-ZW8QjtsLYY5twdoMNTdqobGNWIV8nAFBRdouE28",
"FABRICATION LOG!A78169:D"),
"select Col3
where Col4 = '"&B3&"'
limit 1", 0)
if cell B3 is number use:
=QUERY(IMPORTRANGE("1kFK-ZW8QjtsLYY5twdoMNTdqobGNWIV8nAFBRdouE28",
"FABRICATION LOG!A78169:D"),
"select Col3
where Col4 = "&B3&"
limit 1", 0)
I came here because I had a query like:
=Query(Data, "select * where i < 70", 1)
What fixed it was changing to:
=Query(Data, "select * where I < 70", 1)
The column in the where clause needs to be upper case if its a letter higher than 'd'.

Error while using QUERY with Importrange function

I am trying to use QUERY and importrange data from another spreadsheet (Say A) into my spreadsheet (say B). However, I want to import only rows from A where Column X in A has the same name as in cell L1 of spreadsheet B.
=QUERY(importrange("https://docs.google.com/spreadsheets/d/1IJw96gEObg0fE3SF_XfEXFnilNbjLaHwUm7gctZ74/edit#gid=1403434756","Data !A2:K3000"),"Select * where Col11 = "&L1&"")
error message:
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: JohnSmith
try this one if it works:
=QUERY(IMPORTRANGE("1IJw96gEObg0fE3SF_XfEXFnilNbjLaHwUm7gctZ74", "Data!A2:K3000"),
"select * where Col11 = '"&L1&"'")
or:
=QUERY(IMPORTRANGE("1IJw96gEObg0fE3SF_XfEXFnilNbjLaHwUm7gctZ74", "Data !A2:K3000"),
"select * where Col11 = '"&L1&"'")
and if still no luck then try to change = to matches or contains

Trying to create SUMIF statements inside and IF statement

I currently have a SUMIF statement that outputs a graph, based upon the value of a dropdown.
=SUMIF('Rev Tracking'!$C$16:$C$57,$E$21,'Rev Tracking'!I$16:I$57)
This creates a sum of all the values in I$16:I$57 where the value of $E$21 is found in $C$16:$C$57.
I'm looking to select from multiple ranges of cells dependent upon the dropdown result, but I'm not sure you can nest SUMIFS inside an IF statement.. i.e.
=if(E21="Client1",(SUMIF('Rev Tracking'!$C$65:$C$76,$E$21,'Rev Tracking'!I$65:I$76)),(SUMIF('Rev Tracking'!$C$85:$C$100,$E$21,'Rev Tracking'!I$85:I$100)))
This doesn't work and feels like it should be quite different.
Any ideas?
Try nested =QUERY() inside an IF statement.
E.g.
=IF($E$21 = "Client1",
QUERY(<<CELL_RANGE_HERE>>, "SELECT SUM(Col1) WHERE col1 = '''&$E$21&''' ", 1),
IF($E$21 = "Client2",
QUERY(<<CELL_RANGE_HERE>>, "SELECT SUM(Col1) WHERE col1 = '''&$E$21&''' ", 1), ... ))
I've found this to be helpful when trying to sum different ranges.

Resources