Vlookup on multiple values in a cell - arrays

I would like to be able to return [cat,dog] as shown in column B. This is more of a POC, but for the main challenge I am facing, there will be an unknown number of values in cell A1. I'm not sure if there is a way to do this with a vlookup, or if there is a better way to do this.

Guessing there could be more then one canine species (dog, dog2, ...) and all should be returned:
= "["
& JOIN(
",",
QUERY(
A3:B11,
"SELECT B
WHERE A MATCHES '^(?:" & JOIN("|", UNIQUE(SPLIT(A1, "[,]"))) & ")$'
ORDER BY B",
0
)
)
& "]"
We just filter out everything that does not match the regular expression ^feline|canine|...$ with QUERY using MATCHES and join it with commas.
Regular expression is constructed in this (just unique words joined by |):
'^(?:" & JOIN("|", UNIQUE(SPLIT(A1, "[,]"))) & ")$'

use:
=ARRAYFORMULA("["&TEXTJOIN(",", 1,
IFERROR(VLOOKUP(FLATTEN(SPLIT(A1, "[,]", 1, 0)), A3:B, 2, 0)))&"]")

Related

How to return "Empty" where a query returns empty cells Google sheets

I have two columns. The first one contains names and the second some numbers. I am perfoming a query on the two columns but some of the cells in the second columns will not contain any value. I need to return "Empty" in those cells but I seem not to be able to with my formula. When I add ISBLANK to the query it throws an error.
My query =query($A$1:$B$20, "select A, B where (B <=80)")
Link to my spreadsheet
https://docs.google.com/spreadsheets/d/12gDxvNONKYxqAPJa6FPGJMXkNv6wlXgHEMNjCg-Iuco/edit?usp=sharing
use:
=ARRAYFORMULA(IF(QUERY(A1:B, "where B <=80 and A is not null")="",
"Empty", QUERY(A1:B, "where B <=80")))
or:
=ARRAYFORMULA(QUERY({A2:A, ""&IF((A2:A<>"")*(B2:B=""), "Empty", B2:B), B2:B},
"select Col1,Col2 where Col2='Empty' or Col3<=80", 0))

Any way in Google sheets queries to concat/append results of two columns and add text to result? (I know there is no concat within sheets queries)

I am clear that there is no concat method in google sheets queries but I'm hoping for a workaround. I've seen some suggestions for transpose but can't seem to apply them to my use, hoping for help.
My actual source sheet has more complexity in columns but I've simplified it to demonstrate
Existing Query
=query($L$10:$N$27, "SELECT L, M WHERE N = 'Full' LABEL L 'Name', M ''",1)
If Concat/Append were available it would look more like
=query($L$10:$N$27, "SELECT ("Name is: " & L & " " & M) WHERE N = 'Full' LABEL L 'Name'",1)
Thanks!
try:
=ARRAYFORMULA(QUERY({"Name is: "&L10:L27&" "&M10:M27, N10:N27},
"select Col1
where Col2 = 'Full'
label Col1 'Name'", 1))

Using COUNTIFS in Google Sheets with a possible blank criteria

Ok so I'm sure there is a way to do this, I am just not sure how to do it. Say I have 3 columns. Column A contains a "A" or "B", column B contains "1" or "2" and column C contains "!" or "?". I have separate cells in which I use to filter what I am looking for. I want to find each time a certain instance when all 3 character that I pick show up. D1 is where I choose "A" or "B", D2 is where I choose "1" or "2" and D3 is where I choose "!" or "?". I have a formula to find where if I choose say A, 1, !, it counts how many times all three of those appear in the same row. Now I want to leave one of the Cells blank (say D1) and only calculate how many times "1" and "!" appear together and not worry about "A" or "B" in the first column. Is there any way to do this? My current equation is just a long COUNTIFS equation that checks row A against cell D1, row B against D2 and row C against D3. Is there any way to use this equation but ignore one of the conditions if a cell is blank?
Sorry for the rambling and poor formatting. I hope this makes sense!
Now you can combine the criteria (1,2 or 3):
= countif
(
filter
(
if ( D2 = "" , left("", row($A$2:$A)^0), $A$2:$A) &
if ( D3 = "" , left("", row($B$2:$B)^0), $B$2:$B) &
if ( D4 = "" , left("", row($C$2:$C)^0), $C$2:$C)
, $A$2:$A <> ""
)
, "=" & D2 & D3 & D4
)
try:
=COUNTIFS(A:A, IF(E3="", "<>"&E3, E3), B:B, E4, C:C, E5)
for full independency you can do:
=COUNTIFS(A:A, IF(E3="", "<>"&E3, E3),
B:B, IF(E4="", "<>"&E4, E4),
C:C, IF(E5="", "<>"&E5, E5))

How can I use an array as input for FILTER function in Google Spreadsheet?

So this might be trivial, but it's kinda hard to ask. I'd like to FILTER a range based other FILTER results.
I'll try to explain from inside out (related to image below):
I use filter to find all names for given id (the results are joined in column B). This works fine and returns an array of values. This is the inner FILTER.
I want to use this array of names to find all values for them using another outer FILTER.
In other words: Find maximum value for all names for given id.
Here is what I've figured:
=MAX(FILTER(J:J, CONTAINS???(FILTER(G:G, F:F = A2), I:I)))
^--- imaginary function returning TRUE for every value in I
that is contained in the array
=MAX(FILTER(J:J, I:I = FILTER(G:G, F:F = A2)))
^--- equal does not work here when filter returns more than 1 value
=MAX(FILTER(J:J, REGEXMATCH(JOIN(",", FILTER(G:G, F:F = A2)), I:I)))
^--- this approach WORKS but is ineffective and slow on 10k+ cells
https://docs.google.com/spreadsheets/d/1k5lOUYMLebkmU7X2SLmzWGiDAVR3u3CSAF3dYZ_VnKE
I hope to find better CONTAINS function then the REGEXMATCH/JOIN combo, or to do the task using other approach.
try this in A2 cell (after you delete everything in A2:C range):
=SORTN(SORT({INDIRECT("F2:F"&COUNTA(F2:F)+1),
TRANSPOSE(SUBSTITUTE(TRIM(QUERY(QUERY(QUERY({F2:G},
"select max(Col2) group by Col2 pivot Col1"), "offset 1"),,999^99)), " ", ",")),
VLOOKUP(INDIRECT("G2:G"&COUNTA(F2:F)+1), I2:J, 2, 0)}, 1, 1, 3, 0), 999^99, 2, 1, 1)

Array formula to fill column based on two or more lookup keys?

There is a three-column table. Each row in the first two columns makes a unique identifier (but values in the columns separately are not unique). The third column contains a value for each row in the table.
Then there is another three-column table. Each row in the first two columns is a value to lookup in the first table, to fill the third column with the respective value. So I've been trying to come up with an array formula to fill the third column.
I don't want a script for the solution, only formula
I don't want a one-row formula to distribute, only an array formula
try:
=ARRAYFORMULA(IFNA(VLOOKUP(E1:E10&F1:F10; {A1:A10&B1B:10, C1:C10}; 2; 0)))
where columns A, B, C are the first table and columns E, F, G are the second table.
Here using vlookup, query, arrayformula and others to get summing data, with assumption, A21 to B25 as unique master, I16 to K19 is the distribution data with I16 to J19 are unique master from A21 to B25, if the data different with my assumption, change them actually:
=arrayformula(vlookup(arrayformula(len({A21:A25}) & ":" & len ({B21:B25}) & ":" & {A21:A25} & {B21:B25}),query({arrayformula(len({I16:I19}) & ":" & len ({J16:J19}) & ":" & {I16:I19} & {J16:J19}),arrayformula(if(isnumber(K16:K19),K16:K19,0))},"select Col1, sum(Col2) group by Col1"),2,false))
I combine two column with identifier length of each column and colon, because I hope my column combination is right unique, if I combine 'aaa' & 'aaaa' will same with 'aaaa' & 'aaa' but it can be omitted if necessary to shorten the formula:
=arrayformula(vlookup(arrayformula({A21:A25} & {B21:B25}), query({arrayformula( & {I16:I19} & {J16:J19}), arrayformula(if(isnumber(K16:K19), K16:K19, 0))}, "select Col1, sum(Col2) group by Col1"),2,false))

Resources