Google Sheets Order By Query isnt showing any data - google-sheets

I am trying to bring in my list of employees from another file and have them sorted when they come in. When I do this:
=QUERY(importrange(K1, "Employees"), "Select *", 1)
Everything works great, but as soon as I add the order by method,
=QUERY(importrange(K1, "Employees"), "Select * order by Col4", 1)
It no longer works and just shows the header. Col4 is the correct column and if I use "Select Col4" all of the correct data shows up once again. Any idea of why this is happening?

Try putting your IMPORTRANGE() function in curly brackets. I was able to reproduce (what I believe to be) a similar error in my sheet.
When I defined a range to query, it would not work when using "Col4". This did work when I used D, though.
However, when I then placed the range into curly brackets, it understood Col4, and was able to order it.
Given that you did not get an error and instead got no values, make sure that the values in the column you are sorting by are of one majority datatype.
For example:
In this, the majority of column 4 are numbers, so when the data is sorted all the letters are left out.
My best advice is to simply check your datatypes. Dissimilar types won't sort properly with QUERY.

Make sure the formatting of the source data is not mixed.
Example: Text, Number, ...
Format the columns in the same format to the last row.

Related

Unpivoting using QUERY function is not fetching the expected result

I am currently working on a dataset that includes several columns, mostly the dates. What I am trying to achieve is - unpivot all the date columns to use for my subsequent calculations. I use the following formula to unpivot: =ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|"))
Even though this returns the expected result, when I try to nest this within a Query function, it does not work correctly. This is how I applied the formula - QUERY(ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|")),"Select * WHERE Col3 IS NOT NULL")
PS: When I change the data range to say, A2:A100, it gives me the correct result. However, it does not help since lot of new data would get added and I want the formula to be dynamic.
Here's the link to the sample sheet - https://docs.google.com/spreadsheets/d/1dgFY5mT9nUJtFefjAros-XpWXRMFtxEf8Fqrv82N5Ys/edit#gid=1813140523
Any help/suggestions would be highly appreciated
Not sure where you got your SPLIT(FLATTEN technique,
but you have to include both the 3rd and 4th parameters of the split function as FALSE or 0. so in your case it would be:
=ARRAYFORMULA(SPLIT(FLATTEN(Data!A2:A&"|"&Data!D1:AG1&"|"&Data!D2:AG),"|",0,0))
If you do that you'll find your query works.
Also note that the way you have it it's not really working. If you look all the way down in column 1 you'll find a bunch of dates formatted to look like integers.

QUERY() function treats multiple rows as the header and groups all into first row. Is there a fix or workaround?

When I use the query() function, if there is a date or number column that has some blanks at the top it will group all the rows down to the first date/number entry into the first row, with the headers. I need it to treat all rows after the header as individual rows, regardless of blanks. I'm assuming that it treats those rows as not being part of the data because they don't have values in certain columns, however it achieves this decision sporadically. See image or link for context.
My attempts at resolving so far have been
Removing the labels at the top i.e.: =QUERY(A1:C, "SELECT * label A ''") but that kept the grouping.
ORDER BY doesn't help, as those top rows seem exempt, part of the header.
Inserting a column of numbers to trick it into thinking there are numbers, it ignores this wherever I position it (hence the sporadic comment).
I am aware that I could write a Google Apps Script to resolve this, however I'm trying to keep the required skill for adapting the process on a level that others can use. The data is coming from an API that I can't order or format until in the spreadsheet.
This is the only blocker from me fully automating several processes so I'd appreciate any help in finding a workaround or a solution. :)
Image: Cell E1 is =QUERY(A1:C, "SELECT *") and you can see A2 to A5 are shoved unceremoniously in with the header. Solutions?
https://docs.google.com/spreadsheets/d/1MU35HrkRxyHQaliQgKxqeHBViulMnRmPN9UUO7kq0ts/edit?usp=sharing
Using the (optional) headers argument should solve this. See if this helps
=QUERY(A1:C, "SELECT *", 1)

How to pick one of the results of UNIQUE() in Google Sheets

Problem
I have a column with duplicate items in Google Sheets, and I would like to get one of the unique values (say, the last one) in the cell of the formula. Is there a way to do this with just formulas (i.e., no scripts/macros)?
What I've tried
Not sure if this is the best way, but I've tried using the UNIQUE(range) function, which returns a list of distinct values, and I tried to pick one with FILTER(range, condition1, [condition2, …]), but I've only managed to do it when I know in advance and hard-code in the number of unique values.
Since I can get the length of the unique list with =LEN(UNIQUE(my_range)), I tried using the REPT(text_to_repeat, number_of_repetitions) function.
For example,
=REPT(0&";",2) & 1 returns "0;0;1"
but
=FILTER(UNIQUE(A$1:A$26), {REPT(0&";",2) & 1})
(or any variation I tried) doesn't quite work.
P.S.
I realise this is not the most suitable problem for a spreadsheet, and I do wish I was using something like Python, but this is the restriction at the moment.
try:
=QUERY(UNIQUE(A1:A), "offset "&COUNTA(UNIQUE(A1:A))-1)
Or more old-school using index:
=index(unique(A:A),counta(unique(A:A)))
You can also just enter a number fot the one you want e.g.
=index(unique(A:A),2)

Query is failing at pulling both words and integers in the same query

While using Query, I kept receiving blanks in some columns while the data was correctly pulled in others.
I realized that the first column of data I was calling were numbers, the second were words, and the third were also numbers.
Columns 1 and 3 worked fine, but 2 kept on showing blanks. When I switched all to be words, then the queries worked out fine.
Here is a sheet demonstrating this problem.
https://docs.google.com/spreadsheets/d/1JhD75CzDJxCiXdcD8pBXzThzzNWgCvv2M46UxA48Xzo/edit?usp=sharing
My goal is to be able to query the 9 values with
=query(A2:D4,"Select B,C,D where A contains 'id'")
What's going wrong? It must have to do with different types of data, right?
QUERY is just a bit confused and auto-assumes the presence of header rows. to fix it just add 3rd QUERY parameter 0 - which defines there are no header rows:
=QUERY(A2:D4, "select B,C,D where A contains 'id'", 0)

Why does my importrange query fail when I "wrap" with arrayformula

I have the following formula which is currently returning the expected results -
=join(",",query(importrange(vlookup(mid(G4,1,find(",",G4)-1),xref,2,false),vlookup(mid(G4,1,find(",",G4)-1),xref,3,false)),"Select Col3,Col6,Col9 where Col1 = '"&mid(G4,find(",",G4)+1,20)&"' "))
However, I naturally want to make this as dynamic and flexible as possible so I would like to "wrap" it in an arrayformula which ends up like this -
=arrayformula(join(",",query(importrange(vlookup(mid(G4:G,1,find(",",G4:G)-1),xref,2,false),vlookup(mid(G4:G,1,find(",",G4:G)-1),xref,3,false)),"Select Col3,Col6,Col9 where Col1 = '"&mid(G4:G,find(",",G4:G)+1,20)&"' ")))
This formula gives me "Unable to parse query string for Function QUERY parameter 2: NO_COLUMNCol3" error.
I tried to include an iferror to try to trap some error but this made no difference.
I tried various angles to debug and basically focussed on the importrange not providing the data to the query once it was wrapped by the arrayformula. I tried to explicitly reference the external sheet key and range in the importange function, instead of using the lookups, and this did give me a result - but only in the first cell. There should also have been a result returned about 4 rows down.
If I copy the formula down the column, I do get the expected result 4 rows down, but this obviously defeats the purpose of the arrayformula.
In my research in the Google forums there were some suggestions that arrayformula and importrange may not play well together, but no hard and fast facts.
I noticed in this forum that the combination of the two functions has been mentioned but no indication that they did not work together, so I am wondering if there is just some little thing I am missing in my syntax that is causing my ideal scenario not to work ?
I don't think this will work for a couple of reasons.
Firstly, not all the functions in Google Sheets can be automated using an arrayformula, and QUERY is one of them. As far as I know this is because the output of QUERY can be an array itself, so it is not possible to iterate an array output across another array (i.e. your results range).
Secondly, JOIN works across a either a single row or column, whereas your query outputs 3 columns. The arrayformula result would therefore consist of an array with multiple rows and columns, which JOIN cannot use.
I think the best solution is to use the IFERROR as you've described, and copy the single-row formula down the entire column - that way the blank records will not show as errors, but you will be able to add new values to column G and they will be picked up automatically.

Resources