I am trying to consolidate some data and running into a bizarre issue. I am using a query function that is working perfectly in some instances but not in other. When referencing a particular sheet, it keeps outputting the values in the row beneath, which is causing aa #REF error.
Here is a copy of the Google Sheet.
Here is the formula I'm using (which again, works perfectly sometimes, but not when pulling data from certain sheets)
=QUERY(INDIRECT($B125&"!$A$2:$1000"), "SELECT E WHERE A = date '"& text($C125, "yyyy-mm-dd") &"' and B contains '"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($E125, " " , ""),"AM", ""), "PM", "")&"' ")
You need to specify the number of headers otherwise it will try to guess which eventually results in rows being magically merged into the header causing all sorts of unpredictable behavior:
=QUERY(INDIRECT($B125&"!$A$2:$1000"), "SELECT E WHERE A = date '"& text($C125, "yyyy-mm-dd") &"' and B contains '"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($E125, " " , ""),"AM", ""), "PM", "")&"' ",1)
All I did was add a ,1 just before the closing parenthesis so that it specifies always 1 row of headers. The format of your data is really messed up and you need to think about cleaning up your data.
Related
I would like to change one of the formulas I'm using to return an array of values instead of having to copy/paste or drag down the formula for each cell. My current formula that works for single cells that can be dragged down is:
=SUMIFS('Tracking Tasks'!B$2:B,
'Tracking Tasks'!C$2:C, ">="&B2,
'Tracking Tasks'!C$2:C, "<="&D2,
'Tracking Tasks'!D$2:D, "<>")
The formula sums up all points that are "Completed" between two dates that have also been "Released" (cell not blank). I've tried many solutions, however this appears to be closest to what I'm trying to achieve:
=ArrayFormula(SUM(
QUERY('Tracking Tasks'!B$2:D,
"select B
where C >= date '" & TEXT(B2:B, "yyyy-mm-dd") &
"' and C <= date '" & TEXT(D2:D, "yyyy-mm-dd") & "'")
))
However, this solution doesn't return an array of values like I'd expect it to, and it doesn't account for the "Released" column.
Sample Google Sheet
Solution for you
=ArrayFormula(IFERROR(VLOOKUP(FLOOR((B2:B-2)/7),QUERY({IF('Tracking Tasks'!D$2:D<>"",FLOOR(('Tracking Tasks'!C$2:C-2)/7),""),'Tracking Tasks'!B$2:B},"select Col1,sum(Col2) where Col1>0 group by Col1"),2,0),""))
Looking for some assistance please.
To start, here's the function that I'm having trouble with:
=IFERROR(
QUERY(
OrderDetails!A8:Q9,
"SELECT SUM(J) where Q >= date '" & TEXT(D3,"yyyy-mm-dd") & "' label sum(J) ''"
),
0
)
The dates in the data range (OrderDetails!A8:Q9, columns P is Date & Q is DateOnly) look like this:
I added Q manually in an attempt to make the date-matching work, but P is the raw data which I would prefer to use.
Next the SUM(J) which are just order balances. If I remove the WHERE clause the query runs as expected.
D3 is the column date I want to match to, in the format: 8/13/2018, however I've formatted it on screen to be only DDD.
To show the actual value rather than the header in the cell, I've used label sum(J) ' '.
When running I get the message "Nothing to return".
Can anybody spot an obvious error with the code or my approach? Happy to add any further detail if needed.
Populate Q8 with:
=left(P8,10)*1
and copy down.
The QUERY is failing for attempting to compare a date (in D2/3) with the output of a string function (LEFT). *1 coerces the strings into dates. Left alignment was a clue that the contents were Text.
I have a table using Google Sheets. It has three columns that will always have a null value or a specific value for that column. Each line will have one, two, or three values; it will never have three null values on one line. In the fourth column, I want an ArrayFormula that will combine those values and separate the values with a comma if there is more than one.
Here is a photo of what I am trying to accomplish.
I've tried several ideas so far and this formula is the closest I've gotten so far but it's still not quite working correctly; I think it is treating each column as an array before joining rather than doing the function line by line. I'm using the LEN function rather than A2="" or ISBLANK(A2) because columns A-C are ArrayFormulas as well. I realize this probably isn't the most efficient formula to use but I think it covers every possibility. I'm definitely open to other ideas as well.
={"Focus";
ArayFormula(
IFS(
$A$2:$A="", "",
(LEN(A2:A)>0 & LEN(B2:B)>0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, A2:A, B2:B, C2:C),
(LEN(A2:A)>0 & LEN(B2:B)>0 & LEN(C2:C)=0), TEXTJOIN(", ", TRUE, A2:A, B2:B),
(LEN(A2:A)>0 & LEN(B2:B)=0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, A2:A, C2:C),
(LEN(A2:A)=0 & LEN(B2:B)>0 & LEN(C2:C)>0), TEXTJOIN(", ", TRUE, B2:B, C2:C),
(LEN(A2:A)>0 & LEN(B2:B)=0 & LEN(C2:C)=0), A2:A,
(LEN(A2:A)=0 & LEN(B2:B)>0 & LEN(C2:C)=0), B2:B,
(LEN(A2:A)=0 & LEN(B2:B)=0 & LEN(C2:C)>0), C2:C
)
)
}
Is it possible to achieve this with Google Sheets?
Sample File
Please try:
=ARRAYFORMULA(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(A2:C,ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99)))," ",", "))
Notes:
The formula will work incorrectly if some names have space inside: like "Aston Martin"
So if you have spaces, please try this:
=ARRAYFORMULA(SUBSTITUTE(
SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(SUBSTITUTE(A2:C," ",char(9)),ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99)))," ",", "),
CHAR(9)," "))
EDIT
Noticed the shorter variant (without *COLUMN(A2:C)^0) will work:
=ARRAYFORMULA(SUBSTITUTE(
SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(FILTER(SUBSTITUTE(A2:C," ",char(9)),ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C),0)))),,2^99)))," ",", "),
CHAR(9)," "))
Notes:
I used an old trick to join strings with an array-formula. See sample file
Explanations
If you like to understand any tiered formula, the best way is to split it by parts:
Part 1. Filter the data
FILTER(any_columns,ROW(A2:C)<=MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0))). this is my way to limit the data range.
The range is open, means it starts from the second row (A2) and
ends in any row.
I want to get the limited array in this step to reduce work that the formula should do. This is done with a condition, if.
ROW(A2:C) must be less or equal to the max row of data.
MAX(IF(LEN(A2:C), some_rows) gives the max row.
If(len.. part checks if a cell has some text inside it.
Note some_rows part:
MAX(IF(LEN(A2:C),ROW(A2:C)*COLUMN(A2:C)^0,0)))),,2^99))).
ROW(A2:C) must be multiplied by columns, because filter formula
takes only one row into its condition. That is why I multiply by
COLUMN(A2:C)^0 which is columns with 1s. Edit. Now noticed,
that the formula works fine without *COLUMN(A2:C)^0, so it's an
overkill.
Part 2. Join the text
query formula has 3 arguments: data, query_text, and a number_of_header_rows.
data is made with a filter.
query_text is empty, which gives us equivalent to select all
("select *").
And the number of rows of a header is some big number (2^99).
This is a trick: when a query has more headers then one row,
it will join them with space.
After a union is made, transpose function will convert the result back to the column.
Part 3. Substitute and trim
The function trim deletes extra spaces.
Then we replace spaces with the delimiter: ", ". That is why the
formula needs to be modified if spaces are in strings. Correct
result: "Ford, Aston Martin". Incorrect: "Ford, Aston, Martin". But
if we previously replace spaces with some char (char(9) is Tab),
then we do not replace it in this step.
I'm having trouble figuring out a fairly simple QUERY statement in Google Spreadsheets. I'm trying to use a cell reference instead of static values and I'm running into trouble. Below it the code I'm using, but I keep getting a "Error: Formula parse error."
=QUERY(Responses!B1:I, "Select B where G contains"& $B1 &)
I'm sure it is a simple error, but can someone please show me how to write the above so the QUERY is pulling data from B where G contains the value in cell B1 (cell reference)?
Copied from Web Applications:
=QUERY(Responses!B1:I, "Select B where G contains '"&$B1&"'")
I only have a workaround here.
In this special case, I would use the FILTER function instead of QUERY:
=FILTER(Responses!B:B,Responses!G:G=B1)
Assuming that your data is on the "Responses" sheet, but your condition (cell reference) is in the actual sheet's B1 cell.
Hope it helps.
UPDATE:
After some search for the original question: The problem with your formula is definitely the second & sign which assumes that you would like to concatenate something more to your WHERE statement. Try to remove it. If it still doesn't work, then try this:
=QUERY(Responses!B1:I, "Select B where G matches '^.\*($" & B1 & ").\*$'") - I have not tried it, but it helped in another post: Query with range of values for WHERE clause?
I know this is an old thread but I had the same question as the OP and found the answer:
You are nearly there, the way you can include cell references in query language is to wrap the entire thing in speech marks. Because the whole query is written in speech marks you will need to alternate between ' and " as shown below.
What you would need is this:
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' ")
If you then wanted to refer to multiple cells you could add more like this
=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' and G contains '"& B2 &"' ")
The above would filter down your results further based on the contents of B1 and B2.
I found out that single quote > double quote > wrapped in ampersands did work. So, for me it looks like this:
=QUERY('Youth Conference Registration'!C:Y,"select C where Y = '"&A1&"'", 0)
Here is working code:
=QUERY(Sheet1!$A1:$B581, "select B where A = '"&A1&"'")
In this scenario I needed the interval to stay fixed and the reference value to change when I drag it.
none of the above answers worked for me. This one did:
=QUERY(Copy!A1:AP, "select AP, E, F, AO where AP="&E1&" ",1)
To make it work with both text and numbers:
Exact match:
=query(D:E,"select * where D like '"&C1&"'", 0)
Convert search string to lowercase:
=query(D:E,"select * where D like lower('"&C1&"')", 0)
Convert to lowercase and contain part of the search string:
=query(D:E,"select * where D like lower('%"&C1&"%')", 0)
A1 = query/formula
yellow / A:B = result area
green / C1 = search area
blue / D:E = data area
If you get error when the input is text and not numbers; move the data and delete the (now empty) columns. Then move the data back.
Old Thread but I found this on my journey to the below answer and figure someone else might need it too.
=IFERROR(ArrayFormula(query(index(Sheet3!A:C&""),"select* Where Col1="""&B1&""" ")), ARRAYFORMULA({"* *","no cells","match"}));
Here is a simply built text Filter from a 3 column data set (A,B and C) located in "sheet3" into the current sheet and calling a comparison to a cell from the current sheet to filter within Col1(A).
This bit is just to get rid of the #N/A error if the filter turns up no results //ARRAYFORMULA({"* *","no cells","match"}))
Your question-title is very broad despite the question-body is asking for only one type of 'cell reference'. I'll try to answer some of the other scenarios in case other people come here looking for them.
A - String comparison with cell reference
=TRANSPOSE(QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') "))
=QUERY(MIX!A16:F, "SELECT B,C,D,E,F WHERE (UPPER(A) = '"&F1&"') ")
F1 = StringToSearch
B - String comparison with cell reference + SheetName is called using cell reference + Dates comparison using cell reference
F6 = SheetName
F2 and F3 = StringToSearch
I4 and I5 = dates with this format = 2022-01-01 = yyyy-mm-dd
If the dates you have are in a different format, you can use the formula below to convert them
I4 = TEXT(H5,"yyyy-mm-dd")
=QUERY(INDIRECT(F6&"!A2:C") , "select A,B,C where ((upper(B) contains '"&F2&"') or (upper(B) contains '"&F3&"')) AND (A >= date '"&I4&"') AND (A <= date '"&I5&"') ")
C - Data to query comes completely using cell reference
B1 = Sheetname!Range = Sheet1!A1:C
A36 = stringToFind
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A36&"') ")
The "$" in $B$1 make the reference static for column and row, so if you copy/paste the CELL containing the formula (NOT the text of the formula), the A36 adapts accordingly, but B1 stays fixed.
D - Data to query comes completely using cell reference + String comparison using cell reference + Number comparison using cell reference
=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A40&"') AND C > "&A41&" ")
B1 = Sheetname!Range = Sheet1!A1:C
A40 = stringToFind = blah
A41 = Number = 100
E - Nested query (query inside query) + String comparison using cell reference + Number comparison using cell reference, using LIMIT and OFFSET
=QUERY( transpose( query(IMPORT!$A$7:AAL,"select * where A contains '"&$C34&"' ")) ,"SELECT * LIMIT " & $A34 & " OFFSET " & $B34, 0 )
The final '0' is there to NOT include the header in the results.
I am using the new Google Spreadsheets that was rolled out a couple of weeks back. I'm using a following query to pull data out of another spreadsheet:
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheet/ccc?key=blablathisisanexample", "samplesheet!A:D"), "select SUM(Col4) where ( Col1 >= date '2012-1-1' ) ")
The query works perfectly well if I remove the bit 'where ( Col1 >= date '2012-1-1' )'. It will pull data and sum it correctly.
But what's wrong with my where-col1-date filter thingy? It's written the way it was in previous version of Google Spreadsheet, and it gives no error messages. It just returns a header and no result, as if it didn't find a single date. The dates in the data file are in column 1, written in format 4/7/2014, for example. This used to work in the previous version and I'm at a loss now.
I have several suggestions here. But first: Let's copy the "2012-1-1" value to a particular cell, say F5, and write it with the exact same date format az in the original spreadsheet: 1/1/2012.
Use FILTER function:
=SUM(FILTER(IMPORTRANGE("https://docs.google.com/spreadsheet/ccc?key=blablathisisanexample", "samplesheet!D:D"), IMPORTRANGE("https://docs.google.com/spreadsheet/ccc?key=blablathisisanexample", "samplesheet!A:A")>=F5))
I admit that it's not that pretty because of the two ImportRange functions, but it works for sure, I tried it.
If you insist on using QUERY then try to replace the where condition as follows:
"select SUM(Col4) where ( Col1 >= " & F5 & ")"
I haven't tried this one, but the first one (with FILTER) should work.