Query formula ignores string values [duplicate] - google-sheets

This question already has answers here:
Query is ignoring string (non numeric) value
(2 answers)
Closed 5 months ago.
I have a table that is a result of a few smaller tables merged together.
It's a result of searching a few sheets for rows that meet filtering criteria.
I wanted to remove empty rows using QUERY formula but it works in a strange way!
Normally
=QUERY(A1:Z,"Select *",0)
should return a full table. But not in this case.
What I actually try to do is to remove empty rows. I tried:
=QUERY({A1:Z},"Select * where Col5 is not null",0)
as column E is empty only when whole row is empty. But it does not work. It seems to ignore string values and sees only numbers.
Here is dummy table.
https://docs.google.com/spreadsheets/d/12QmFW9vlx4ToHsQYGmkXK4aLD2jlI30FV1wYgxa0V8c/copy
It looks like this:
When I apply Query that should cut empty rows, my result table looks like this:
It seems to cut all the rows without number value in Column A (strange!)
Note: Table is generated by a very long formula that searches multiple sheets. Whenever result is not found in one of sheets, formula returns empty row. So I need a solution to wrap around existing formula. Normally QUERY is a way to go, but not this time.
I know that I can make additional step. Make one more sheet and use Filter:
=filter(Sheet1!A1:Z,Sheet1!E:E<>"")
Anyway this solution adds bulk to my spreadsheet.

If you convert Col E to text (TO_TEXT), you can run the query without worrying about mixed data:
=index(query({Sheet1!A:D,to_text(Sheet1!E:E),Sheet1!F:Z}, "select * where Col5 is not null ",0))

QUERY only returns the predominant data type per column. Your E column has mixed data types (strings and numbers, with numbers being predominant), so anything that is not a number will be a null — and thus ruled out by the QUERY.
As for how to solve it, that would be difficult to impossible to do given your sample spreadsheet only, since we can't see the actual formula that generates the initial output shown in sample Sheet1.

there are ways but "short-formula lovers" will hate it... for example:
=ARRAYFORMULA(IF(ISNUMBER(QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)*1),
IFERROR(1/(1/QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)*1)),
QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)))
or:
=ARRAYFORMULA(IF(QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)<>"",
IF(ISNUMBER(QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)*1),
QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)*1,
QUERY(TO_TEXT(Sheet1!A:Z), "where Col5 is not null", 0)), ))
if you need zeros
or try like this:
=FILTER(Sheet1!A:Z, TRIM(FLATTEN(QUERY(TRANSPOSE(Sheet1!A:Z),,9^9)))<>"")
=FILTER(your_formula, TRIM(FLATTEN(QUERY(TRANSPOSE(your_formula),,9^9)))<>"")

Related

Query and Importrange to get the last value of the selected row of spreadsheet | Google Sheets

Right now I am using this query to search for a row based on its Column 1 value. Then it takes the value from the last column. I need a way for it to automatically find the last column in the row since some of the rows have more columns than others.
This is what I had before, which I had manually specified the last column with a value:
=QUERY(IMPORTRANGE("link_redacted","PriceList!A1:AZ100000"), "Select Col10 where Col1 = '5531001'",1)
I have tried using LOOKUP with ARRAYFORMULA I couldn't get it to work:
=QUERY(IMPORTRANGE("link_redacted","PriceList!A1:AZ100000"), "Select (LOOKUP(1, ARRAYFORMULA(1/[Select Col1 where Col1 = '5531006']:[Select Col100 where Col1 = '5531006']<>"")[Select Col1 where Col1 = '5531006']:[Select Col100 where Col1 = '5531006']))",1)
Any ideas for a simpler way to do this?
Since no example is presented, I tested the formulas given but no source data is fetched.
so i created a a minimal, reproducible example
Example is the data on the left
Use this formula to get the last non empty columns values.
=ArrayFormula(IFERROR( REGEXEXTRACT( TRIM(TRANSPOSE(QUERY(TRANSPOSE(C3:E),,ROW(C3:E)))), "[^\s]+$")))

Efficient way to collate/aggregate specific data in Google Sheets

I'm looking for an efficient way to gather and aggregate some date in Google Sheets. I've been looking at the query function, pivot tables, and Index + Match formulas, but so far I've not found a way that brings me to the result I'm looking for. I have a set of data which looks more or less as follows.
The fields with an X represent irrelevant data which I don't want to show up in my end result. They only serve to illustrate that there are columns of data that I don't want in between the columns of data that I do want. The data in those columns is of varying types and of varying values per type, they are not actually fields with an "X" in it. Only the fields with numbers are of interest along with the related names at the top and left of those. The intent is to create a list that looks more or less like this.
I've highlighted those yellow fields because that data has been aggregated. For example, in the original file field D3 shows a relation between Laura and Pete with the number 1, and field L3 also shows a relation between Laura and Pete, so the number in that field is to be added to the number in the other field resulting in an aggregated total of 2 for that particular combination.
I would really appreciate any suggestions that can help me get to an elegant and efficient solution for this. The only solutions I can come up with would involve multiple "in-between" sheets and there just has to be a better way.
UPDATE:
Solved by applying the solution in player0's answer. I just had to switch around the order of Col1 and Col2 in the formula to get the table sorted the way I needed it. Formula looks like below now. Many thanks to both player0 and Erik Tyler for their efforts.
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col2,Col1,sum(Col3)
where Col2 is not null
and Col3 is not null
group by Col2,Col1
label sum(Col3)''", ))
try:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"where Col3 is not null and Col2 is not null", ))
update:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col1,Col2,sum(Col3)
where Col3 is not null
and Col2 is not null
group by Col1,Col2
label sum(Col3)''", ))
Given your current data set (which only appears to extend to Col N), place the following somewhere to the right of Col N:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(D2:N)),,D1:N1&"~ "&A2:A&"|"&D2:N),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
It would be better if this were placed in a different sheet from the original data. Supposing that your original data sheet is named Sheet1, place the following version of the above formula into a new sheet:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A:A)))),,Sheet1!D1:1&"~ "&Sheet1!A2:A&"|"&INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A2:A))),Sheet1!A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
This separate-sheet approach and formula allows for the original data to extend indefinitely past Col N.

Extract Value from Cell on Another Sheet Based on List

I have a list of data on one sheet. A second sheet is used to display who's done what, like a dashboard. I've been given an array formula to generate the data that looks up the employee's name, and displays it if they are present on the list for a course completed. If so, a "yes" is listed on the dashboard under the corresponding course number. I cannot figure out what I need to change the REGEXREPLACE with to present the date column from the data list instead of "yes". I'm aware REGEXREPLACE only works for text values, and dates aren't - even changing the date column to text seems to matter not.
Here is a working example of the current array formula:
https://docs.google.com/spreadsheets/d/1jkG515zyl4UxRHxhtFTjgWsjG0aBE4vsOxbpvqgjogU/edit#gid=536376041
Here is the formula used:
=ARRAYFORMULA(IF(A5:A="",,REGEXREPLACE(IFNA(VLOOKUP(A5:A,
QUERY({TRIM('Form Responses 1'!B2:G)}, "select Col1,count(Col1) group by Col1 pivot Col6"), MATCH(F2:P2,
QUERY(QUERY({TRIM('Form Responses 1'!B2:G)}, "select Col1,count(Col1) group by Col1 pivot Col6"), "limit 0", 1), 0), 0))&"", "\d+", "yes")))
In the above example, I need the Date Completed from col D on the Form Responses sheet.
Here is a first attempt that might help you.
This is a straightforward query, that pulls the dates, and pivots to have the employee names on the left, and the course names on the top.
But it doesn't try to match up data with an existing list of employees - it just lists all of the employees that have submitted a form. So if you want to see all employees, with blank rows for those who haven't submitted a form, this won't work for you.
The formula, in A2,is:
=ARRAYFORMULA(QUERY({'Form Responses 1'!B2:G}, "select Col1,max(Col3) where Col1<>'' group by Col1 pivot Col6 order by Col1"))
See tab Sheet1-GK, added to your sample sheet.
Let me know if this helps, or if you need something different.
UPDATE:
To limit the result to a specific list of courses, use the following modification:
=ARRAYFORMULA(QUERY({'Form Responses 1'!B2:G},
"select Col1,max(Col3) where Col1<>''
and Col6 matches '" & TEXTJOIN("|",1,F2:2) & "'
group by Col1 pivot Col6 order by Col1 "))
Here, the list of desired courses to report on is in F2:2, where you had them originally, but this list could be kept anywhere, even on another tab. If you name the range where you place it, that can simplify this formula a bit. For now, you could just hide row 2. I've grouped it, on the left, to hide it. Use the [+] to reveal it again.

Google Sheets import range returning incorrect data

The full google spread sheet system is used for score keeping and is prone to delays when updating, however I have never run into an issue like this were the same basic function is returning two separate results. The problem is repeatable and occurs on more than one spreadsheet.
I have created a test sheet-
https://docs.google.com/spreadsheets/d/1arh0D9ch5MpQjRh_bHjLfLx5S7TAW8R_pgGLf5tovig/
with the code in question; Can anyone help please?
=QUERY(IMPORTRANGE("***","***"),"select Col1 where Col1 <>5 order by Col9 desc")
in your QUERY formula you are selecting cells that are numeric and comparing it to <>5 but take a notice that A2 is not numeric:
that is the reason why 2 2 is not included in your C column
also it looks like that your QUERY formula smashed first cells into one because you did not specify the 3td query parameter. try:
=QUERY(IMPORTRANGE("1pnowvo6YVj-DZAPCaKE2x9vSIbpAAmlwhRMO2OZNlrE","color!A84:M115"),
"select Col1 where Col1 <>5 order by Col9 desc", 0)

Google sheets - Collecting unique values from several sheets?

I'm in the need of collecting unique values from a specific range in several sheets. Is it possible to combine functions in order to do this?
All sheets look the same regarding column structure.
As of now, my function collects from one sheet and it looks like this:
=unique(filter('Sheet1'!C4:C1000,'Sheet1'!C4:C1000<>""))
This collects unique values from Sheet1 from C4 to C1000 and excludes empty cells. This works awesomely, but I have more sheets that I'd like to merge values from. Any idea?
Basic idea is to combine data, first with help of {}:
= {sheet1C4:C1000;sheet2C4:C1000;sheet3C4:C1000}
The next step is to get rid of empty cells. To do it only once, use query:
= query({sheet1C4:C1000;sheet2C4:C1000;sheet3C4:C1000},
"select Col1 where Col1 <> ''")
And then grab uniques/ The final formula will look like:
= unique (query({sheet1C4:C1000;sheet2C4:C1000;sheet3C4:C1000},
"select Col1 where Col1 <> ''"))
By the way, query string may be shortened to this "where Col1 <> ''" will also work

Resources