Column LOOKUP Dynamic Query (Google Sheets) - google-sheets

Trying 'query' for the first time in google sheets. Need some assistance. Thanks in advance.
I have a google sheet with two tabs "sheet 1" and "sheet 2"
Sheet 1 Structure:
Sheet 2 Structure:
I am trying to create a dynamic query, where if Entity = Person "x" and Value = A or B or C, the Des number (from sheet2) is populated in Sheet 1 in yellow cells
So far I have tried these queries and for some reason it throws a "#N/A" error
QUERY({Sheet2!$A$1:$AC$10}, "SELECT Col1,Col"&MATCH(B1,Sheet2!$A$1:$AC$1,0))
LOOKUP($B$1,QUERY({Sheet2!$A$1:$AC$10}, "SELECT Col1, Col"&MATCH("L",Sheet2!$A$1:$AC$1,0)))
Can someone point me in right direction with this query?

It's bit more complex as you have first to unpivot Sheet2, then apply query, finally join the results in one cell.
=iferror(textjoin(char(10),,query(arrayformula(split(flatten(Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4),"|")),"select Col2 where Col1='"&$A2&"' and Col3='"&B$1&"' ")))
adjust Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4as necessary.
Explanation
Sheet2 is like
To unpivot Sheet2, try
=arrayformula(split(flatten(Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4),"|"))
then apply your query
query(_______________,"select Col2 where Col1='"&$A2&"' and Col3='"&B$1&"' ")
and finally join the results

Related

Dynamically offset data of multiple rows to match the header column in Google Sheets

I'm trying to dynamically offset data of multiple rows to match the header column in Google Sheets. The first tab contains data of multiple fruits and how many are harvested on a particular day. Each fruit starts harvesting on different dates, and the dates might not be continuous.
The second tab, "Fruit bank", shows how many fruits are harvested in total for each day. Column D is a continuous set of dates. In cell E1, a QUERY formula dynamically pulls the names of the fruits so whenever a new fruit is added, it shows up here as well. In cell E2 and the rest of the row, I use VLOOKUP formulas to pull the data from the first tab. What I need help with is to write a formula on cell E2 that expands to the rest of the row so I don't have to manually type in the lookup range every time a new fruit is added.
Also, I suspect there're better functions to use than the VLOOKUP because the way VLOOKUP pulls data is very slow. I could literally see it loading even with this small dataset.
Yellow cells contain formula.
I appreciate anyone who can take a look at my spreadsheet (linked below) and see what's the best solution for this. The 3rd tab is editable.
https://docs.google.com/spreadsheets/d/14GeJKgxadInNWVVyft2gilae7HOIEvKXRop-Kz_On-Q/edit#gid=53523977
Thanks! J
Use filter(), like this:
=arrayformula(
ifna(
vlookup(
$D2:$D,
{
filter(
'Fruits data'!$B2:$AA,
'Fruits data'!$A1:$Z1 = E1
),
filter(
'Fruits data'!$A2:$Z,
'Fruits data'!$A1:$Z1 = E1
)
},
2, false
)
)
)
See your sample spreadsheet.
you can try either. added solutions here and here
=BYROW(D2:D,LAMBDA(dx,IF(dx="",,BYCOL(E1:1,LAMBDA(ex,IF(ex="",,XLOOKUP(dx,FILTER('Fruits data'!1:46,COLUMN('Fruits data'!1:1)=MATCH(ex,'Fruits data'!1:1,0)-1),FILTER('Fruits data'!1:46,'Fruits data'!1:1=ex),)))))))
OR
=MAKEARRAY(COUNTA(D2:D),COUNTA(E1:1),LAMBDA(r,c,XLOOKUP(INDEX(D2:D,r),FILTER('Fruits data'!1:46,COLUMN('Fruits data'!1:1)=MATCH(INDEX(E1:1,,c),'Fruits data'!1:1,0)-1),FILTER('Fruits data'!1:46,'Fruits data'!1:1=INDEX(E1:1,,c)),)))
try this with dates:
=ARRAYFORMULA(QUERY({
FLATTEN(FILTER('Fruits data'!A2:100, ISODD(COLUMN('Fruits data'!A2:2)))),
FLATTEN(FILTER('Fruits data'!A2:100, ISEVEN(COLUMN('Fruits data'!A2:2)))),
FLATTEN(IF(FILTER('Fruits data'!A2:100, ISEVEN(COLUMN('Fruits data'!A2:2)))="",,
FILTER('Fruits data'!A1:1, ISEVEN(COLUMN('Fruits data'!A2:2)))))},
"select Col1,sum(Col2) where Col2 is not null group by Col1 pivot Col3"))
which could be simplified:
=ARRAYFORMULA(LAMBDA(x, QUERY({
FLATTEN(FILTER(x, ISODD(COLUMN(X)))),
FLATTEN(FILTER(x, ISEVEN(COLUMN(x)))),
FLATTEN(IF(FILTER(x, ISEVEN(COLUMN(x)))="",,
FILTER(OFFSET(x, -1,,1), ISEVEN(COLUMN(x)))))},
"select Col1,sum(Col2) where Col2 is not null group by Col1
pivot Col3 label Col1'Date'"))('Fruits data'!A2:100))

Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem

I'm pretty new with ArrayFormula, have been trying but sometime the formula works, sometimes does not. What I'm trying to do is the combination of ArrayFormula, Countif for searching partial text.
As shown in the worksheet below, there are 10 subjects (column A), each subject has at least one of 4 samples (A,B,C,D) summarized as a string (column B). What I'm trying to do is to find which subject has sample A or B or C or D.
I have tried single formula for each sample, eg cell D3
=IF(COUNTIF($B3,"*"&$D$2&"*")>0,$A3,"")
it returns the correct results. However, when I try arrayformula in cell I3,
=arrayformula(IF(COUNTIF($B3:B,"*"&$D$2&"*")>0,$A3:A,""))
The answers are weird. For example: Subjects (Gamma, Zeta, Eta, Theta) who don't have the sample "A" are shown to have sample "A". And this applies to sample B,C,D too
Not sure what went wrong in here. Here is the link to the worksheet
I wouldn't use Countifs or an array formula. Use filter instead. Put this formula in cell i3.
=Filter(if(REGEXMATCH(B3:B,$D$2),A3:A,""),B3:B<>"")
try:
=INDEX(QUERY(IFERROR(TRIM(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B3:B, ","))="",,
SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"))
or use in row 2 if you want to sort it as in your example:
=INDEX(IFNA(VLOOKUP(A2:A, QUERY(IFERROR(TRIM(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B3:B, ","))="",,SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select Col2,max(Col2) where Col2 is not null group by Col2
pivot Col1 label Col2'Subjects'"), {2,3,4,5}, 0)))
You can accomplish all four columns of results with a single formula.
Delete all formulas from I3:L3.
Place the following formula into I3:
=ArrayFormula(IF(REGEXMATCH(B3:B,I2:L2),A3:A,))
In plain speech, this read "If anything in B3:B matches a value found in I2:L2, return A3:A in the matching columns(s) at the matching row(s); if not, return null."

Import multiple Sheets into one with Criteria

I am trying to import several ranges from 8 different spreadsheets in to one master list.
This is my first Sheet.
This is my second Sheet.
This is my master list Sheet.
For now, I am testing it on just 2 spreadsheets. Will add the remaining once I figure this out.
So on my master list sheet, A4, I have this code
=query(
{
importrange("https://docs.google.com/spreadsheets/d/1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc/edit#gid=0","Scholarship Applicants Database!A4:W");
importrange("https://docs.google.com/spreadsheets/d/1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w/edit#gid=664164578","Scholarship Applicants Database!A4:W")
},"Select * where Col1 <> ''")
Which works well. Then on Column X, these code does not work.
=query(
{
importrange("https://docs.google.com/spreadsheets/d/1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc/edit#gid=0","CDP Scoring Sheet (Initial)!G4:J");
importrange("https://docs.google.com/spreadsheets/d/1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w/edit#gid=664164578","CDP Scoring Sheet (Initial)!G4:J")
},"select * where A = '"&$A4&"'")
What I am trying to do here is, import the ranges from Sheet1 and 2, from this sheet (CDP Scoring Sheet (Initial)), Column G4 to J, with the condition that Column A of Sheet 1 and 2 matches Column A of my Master list. Error I am receiving is "Unable to parse query string for function QUERY parameter 2: NO_COLUMN: A", I then change the "A" on the "where" part to "Col1", I get the error "Query complete with an empty output" and the result of "#N/A".
I also thought of changing my importrange from "G4:J" to A4:J" but I know it's not right cause it will then pull out the data from columns A to F which I no longer need.
Help please.
try:
=QUERY(
{
importrange("1scqPKhWpbZeOK_iYR7EaypNxMhh__V_O2FLUjbjrNFc", "CDP Scoring Sheet (Initial)!A4:J");
importrange("1PkBWxUG_0BG4gNhXFGHInSdVI5nMo4FReRpCQIVVs3w", "CDP Scoring Sheet (Initial)!A4:J")
},
"select Col7,Col8,Col9,Col10
where Col1 = '"&$A4&"'")

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

Include a specific cell in an array query Google Sheets

I have an array query that runs across multiple sheets in a Google Doc. The query works fine, but I would like to know the sheet of each data returned. I made an example HERE . the formula is :
=QUERY({Sheet2!A2:B6; Sheet3!A2:B6}, "select Col1, Col2 where Col2 = 'Y'")
When a 'Y' is found, I want to return the Data in Col1, Col2 AND (this is the part I don't know how to do) the value of A1 on the page where the match occured.
You can insert page title in the array as its third column, and select that column with the query:
=arrayformula(query({Sheet2!A2:B6, iferror(Sheet2!A2:A6/0, Sheet2!A1); Sheet3!A2:B6, iferror(Sheet3!A2:A6/0, Sheet3!A1)}, "select Col1, Col2, Col3 where Col2 = 'Y'"))
Here, iferror(Sheet2!A2:A6/0, Sheet2!A1) is a clumsy but effective way of saying: take the shape of array Sheet2!A2:A6/0 and fill it with the content of the cell Sheet2!A1. This results in the page title being repeated in every row taken from that sheet (provided that arrayformula wrapper is present, so that the computation /0 is performed with the array). The query returns
Data 2 on page 2 Y Page 2 Title
Data 4 on page 2 Y Page 2 Title
Data 2 on page 3 Y Page 3 Title

Resources