What is wrong with my Query Importrange in google sheets? - google-sheets

What is wrong with this code?
It is supposed to only gather the data in the is range for cols 1,2,3,5,11 for the name Bob that contain False. In my results I'm getting data that are TRUE. If I change 'not Col25 = 'True' to Col25 = 'False', I get no results at all.
=QUERY(IMPORTRANGE(" "Master!A:AB"),
"select Col1,Col2,Col3,Col5,Col11
where Col11 = 'Bob'
AND not Col25 = 'TRUE'")

It doesn't look like you need to use importrange there.
Try the following formula:
=QUERY(Master!A:AB,
"select A,B,C,E,K
where K = 'Bob'
AND Y = FALSE")
Result on my sheet:
NOTE: Make sure that the FALSE and TRUE values are setted as automatic and not in plain text:

Related

Query where Col = range from another sheet

How can I query importrange where Col values = any of the range specified in another sheet?
For example:
=QUERY(IMPORTRANGE("/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/","Data!A2:E"),"select Col4,Col3,Col1,Col2 where Col4 = '"&Helper!A2:A&"'",0)
This workbook has 3 sheets in it:
Data! = Data source
Helper! = Range required for the query to search for
Testing! = Is where I would like the data to return (Testing!A2 has the formula I have tried but it is not working as expected)
https://docs.google.com/spreadsheets/d/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/edit?usp=sharing
I would like it to return data for the range Helper!A2:A but it is currently only returning the data from Helper!A2 I'm not sure what is going wrong as no errors return.
=QUERY(IMPORTRANGE("/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/","Data!A2:E"),"select Col4,Col3,Col1,Col2 where Col4 matches '"&TEXTJOIN("|",TRUE,Helper!A2:A)&"'",0)
use:
=QUERY(IMPORTRANGE("18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA","Data!A2:E"),
"select Col4,Col3,Col1,Col2
where Col4 matches '"&TEXTJOIN("|", 1, Helper!A2:A)&"'", 0)

Google Sheet IFS combine with FILTER Function

I have the following Google Seet Table
Old New New2
W01
W02 W04
W03 W05 W06
I want to create a formular that transforms the table to this one
Old New
W02 W04
W03 W05
W05 W06
So any time a switch from Old to New or New to New2 happens should be displayed.
I wrote the following formular but i always get an error:
= IFS(B1 = "";""; AND(NOT(B1 = ""); NICHT(C1 = ""));FILTER({A1\ B1}; NICHT(A1=""));NICHT(B1 = "");FILTER({B1\ C1}; NICHT(B1="")))
Has anybody an idea?
Concatenate the results of two Query calls:
={
QUERY(A1:B4,
"select A,B where B<>''");
QUERY(B1:C4,
"select B,C where C<>'' label B '', C ''", 1)
}
or in German locale:
={
QUERY(A1:B4;
"select A,B where B<>''");
QUERY(B1:C4;
"select B,C where C<>'' label B '', C ''"; 1)
}
The label statements in the second query are necessary to suppress the column labels since you want to treat certain columns in New1 as Old.

Google sheet array formula with Filter & Join

I need to build a table based on the following data:
Ref
Product
R1
ProdA
R2
ProdC
R1
ProdB
R3
ProdA
R4
ProdC
And here the result I need:
My Product
All Ref
ProdA
R1#R3
ProdC
R2#R4
The particularity is that the 'My Product' column is computed elsewhere. So I need an arrayformula based on 'My Product' column to look in the first table to build the 'All Ref' column. You follow me?
I know that Arrayformula is not compatible with filter and join ... I expect a solution like this one Google sheet array formula + Join + Filter but not sure to understand all steps and if really adapted to my case study.
Hope you can help.
You could try something like this:
CREDIT: player0 for the method shared to similar questions
=ARRAYFORMULA(substitute(REGEXREPLACE(TRIM(SPLIT(TRANSPOSE(
QUERY(QUERY({B2:B&"😊", A2:A&"#"},
"select max(Col2)
where Col1 !=''
group by Col2
pivot Col1"),,999^99)), "😊")), "#$", )," ",""))
Step by step:
Instead of the workaround hacks I implemented a simple joinMatching(matches, values, texts, [sep]) function in Google Apps Script.
In your case it would be just =joinMatching(MyProductColumn, ProductColumn, RefColumn, "#").
Source:
// Google Apps Script to join texts in a range where values in second range equal to the provided match value
// Solves the need for `arrayformula(join(',', filter()))`, which does not work in Google Sheets
// Instead you can pass a range of match values and get a range of joined texts back
const identity = data => data
const onRange = (data, fn, args, combine = identity) =>
Array.isArray(data)
? combine(data.map(value => onRange(value, fn, args)))
: fn(data, ...(args || []))
const _joinMatching = (match, values, texts, sep = '\n') => {
const columns = texts[0]?.length
if (!columns) return ''
const row = i => Math.floor(i / columns)
const col = i => i % columns
const value = i => values[row(i)][col(i)]
return (
// JSON.stringify(match) +
texts
.flat()
// .map((t, i) => `[${row(i)}:${col(i)}] ${t} (${JSON.stringify(value(i))})`)
.filter((_, i) => value(i) === match)
.join(sep)
)
}
const joinMatching = (matches, values, texts, sep) =>
onRange(matches, _joinMatching, [values, texts, sep])```

How to collapse sheets query result into a single cell

I have a sheets query that almost does what I want but I need a bit of help to get to the last step.
=QUERY(Sales!$A$2:$C,"SELECT B, SUM(C)
WHERE A='"&B3&"'
GROUP BY B
ORDER BY SUM(C) DESC
LIMIT 3
LABEL SUM(C) ''
FORMAT SUM(C) '$##,##0' "
,0)
This gives me the result in C3:D4. What I want is in E3.
I have two goals. First output the data stacked and joined like in cell E3. Second, the ideal solution is an array formula in C3 that does this for all the 'Partners'.
Sample Data
As always thanks in advance for the assistance and education!
Here is how I solved it. Maybe not the optimal solution because I need to duplicate the calculation, but it does exactly what I need. I've added my solution to the example data.
=REGEXREPLACE(REGEXREPLACE(textjoin(" ♦ ",0,
QUERY({Sales!$A$2:$A,Sales!$B$2:$B,Sales!$C$2:$C},"SELECT Col2, SUM(Col3)
WHERE Col1='"&B3&"'
GROUP BY Col2
ORDER BY SUM(Col3) DESC
LIMIT 3
LABEL SUM(Col3) ''
FORMAT SUM(Col3) '$##,##0' "
,0)
)," ♦ \$"," = \$")," ♦ ",CHAR(10))
One way to accomplish this is creating a custom function in Google Apps Script. To achieve this, follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Copy this function in the script editor, and save the project:
function GET_TOP_CUSTOMERS(partners) {
const sheet = SpreadsheetApp.getActive().getSheetByName("Sales");
const data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 3).getValues();
return partners.map(row => row[0]).map(partner => {
const partnerRows = data.filter(row => row[0] === partner);
const uniqueCustomers = [...new Set(partnerRows.map(row => row[1]))];
const topCustomers = uniqueCustomers.map(customer => {
return [customer, partnerRows.filter(row => row[1] === customer)
.reduce((acc, current) => {
return acc + current[2] }, 0)];
}).sort((a,b) => b[1] - a[1]).slice(0, 3);
return topCustomers.map(customer => customer[0] + "; $" + customer[1].toFixed())
.join("\n");
});
}
Now, if you go back to your spreadsheet, you can use this function like any in-built one. You just have to provide the appropriate range (in this case it would be B3:B12), as you can see here:
Reference:
Custom Functions in Google Sheets

What's the proper syntax to query a string in google sheets?

When I use the Query function like this using a number, it returns the correct results:
=QUERY(Sheet2!A1:B12,"select B where A matches '1200'", 0)
But, when I try to match a string, the result is always an empty output:
=QUERY(Sheet2!A1:B12,"select B where A matches 'qwerty'", 0)
This seems ultra-simple, but I can't seem to find an answer anywhere.. What exactly is the correct syntax to match the string? I've included an example of my problem here: sheets_query
I suggest you format ColumnA of Sheet2 as Plain text.
This indeed is simple, instead of matches, you need to use an "=" symbol. So your formula will be =QUERY(Sheet2!A1:B12,"select B where A='qwerty'", 0)
for number it would be:
=QUERY(Sheet2!A1:B12, "select B where A = 1200", 0)
for text, it would be:
=QUERY(Sheet2!A1:B12, "select B where A = 'qwerty'", 0)
for number with external reference:
=QUERY(Sheet2!A1:B12, "select B where A ="&1200, 0)
for text with external reference:
=QUERY(Sheet2!A1:B12, "select B where A = '"&"qwerty"&"'", 0)
for mixed data you can either format it as Plain text or:
=ARRAYFORMULA(QUERY(TO_TEXT(Sheet2!A1:B12), "select Col2 where Col1 = 'qwerty'", 0))

Resources