Look up elements of a delimited string within an array - Google Sheets - google-sheets

I have put together some sample data in a sheet here.
Essentially I have a column with names separated by a pipe (col B in example). I have a separate table (col F:G) that maps each name to a role type. I want to add a column that looks up each individual person in the pipe delimited string and returns a similarly structured string containing their job roles.
I can do this on a row by row basis (see col C), but this is a table that will grow (and is sourced from elsewhere) so ideally I'd like to use an array formula that will work for every row. However, because of the SPLIT() in there I can't get this to work.
Is there a way I can achieve this?

try:
=ARRAYFORMULA(REGEXREPLACE(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IFERROR(VLOOKUP(SPLIT(B2:B, "|"), {F:F, G:G&"|"}, 2, 0))),,9^9))),
"\|$", ), "\| ", "|"))

Related

Unnest two columns in google sheet

I have a table like this one here (basically it's data from a google form with multiple choice answers in column A and B and non-muliple choice data in column C) I need a separate row for each multiple choice answer.
Column A
Column B
Email
A,B
XX,YY
1#gmail.com
A,C
FF,DD
2#gmail.com
I tried to un-nest the first column and keep the remaining columns like this
enter image description here
I tried several approaches I found with flatten and split with array formulas but I don't know where to start really.
Any help or hint would be much appreciated!
You can use the split function on the column A and after that, use the index function. Considering the table, you can use:
=index(split(A2,","),1,1)
The split function separate the text using the delimiter indicated, returning an array with 1 line and 2 columns; the index function will return the first line and the first column from this array. To return the second element from the column A, just change to
=index(split(A2,","),1,2)
I think there's no easy solution for this. You're asking for as many combinations of elements as multiple-choice elections have been made. Any function in Google Sheets has its potentials and limitations about how many elements it can express. One very useful formula here is REDUCE. With REDUCE and sequences of elements separated by commas counted with COUNTA, you can stablish this formula:
=QUERY(REDUCE({"Col A","Col B","Email"},SEQUENCE(COUNTA(A2:A)),LAMBDA(z,c,{z;LAMBDA(ax,bx,
REDUCE({"","",""},SEQUENCE(ax),LAMBDA(w,a,
{w;
REDUCE({"","",""},SEQUENCE(bx),LAMBDA(y,b,
{y;INDEX(SPLIT(INDEX(A2:A,c),","),,a),INDEX(SPLIT(INDEX(B2:B,c),","),,b),INDEX(C2:C,c)}
))})))
(COUNTA(SPLIT(INDEX(A2:A,c),",")),COUNTA(SPLIT(INDEX(B2:B,c),",")))})),
"Where Col1 is not null",1)
Since I had to use a "initial value" in every REDUCE, I then used QUERY to filter the empty values:

How can I use REGEXMATCH and SPLIT in a filter in Google Sheets?

I have two data sets:
Data set 1 is data from our Sales team
Data set 2 is data from our Finance team
The common element between these two sets is the Invoice ID column (col A in data set 1 and col E in data set 2). However, in data set 1, this data is stored as an array, and in data set 2, each value of the array is displayed on its own row.
GOAL
I'd like to be able to enrich data set 2 (cols F & G) with the data from data set 1, however, I'm having trouble making that work. I've tried using =FILTER(A3:A7, REGEXMATCH(TEXT(E3, "0"), TEXT(ARRAYFORMULA(SPLIT(A3:A7, ",")), "0"))), but that gives me the following error: "FILTER range must be a single row or a single column.". I understand that this happens because of the SPLIT function, but I don't know how else to go about this.
The sheet can be found here.
Any help is super appreciated.
I've added a new sheet ("Erik Help") to your sample spreadsheet. The following single formula will produce all results for F3:G ...
=ArrayFormula(IF(E3:E="",,VLOOKUP("*"&"%"&E3:E&"%"&"*",{REGEXREPLACE("%"&A3:A&"%","[,\s]","%"),B3:C},{2,3},FALSE)))
The % symbol is just used to "pad" every element from Col A and Col E in something unique in order to differentiate search term 1 from, say, 14 (i.e., the VLOOKUP will search for %1% instead of just 1, which will not be found in %14%, etc.) The % symbol has no special meaning; it could have been any unique character we were sure wouldn't normally turn up in Col A or Col E.
REGEXREPLACE replaces all commas and spaces with this special % character in addition to the front and back placements. So a Col-A list like 1, 14 would be seen by sheets at this point as %1%%14%.
The * is a wildcard symbol that, appended front and back to the search term, which will allow it to be found in elements of Col A that contain lists.
Results from the second and third columns (e.g., {2,3}) of the virtual array are returned.
Give a try on-
=FILTER($B$3:$B$7,ArrayFormula(MMULT(--(SPLIT($A$3:$A$7,", ")=E3),SEQUENCE(Columns(ArrayFormula(SPLIT($A$3:$A$7,", ")))))))

Is it possible to use two cells of criteria to return a value from a third column?

I have an Google sheets file with two sheets. I'm trying to reference one sheet's text based on two cells of criteria using a formula in the other sheet. I have one sheet that looks similar to this -
And another that looks like this -
I would like to put a formula on the second sheet that basically says - look on the first sheet for the values in columns A and B and return me the value in column C. The tricky part is - the values in the second sheet may be inverted or there may be an instance where only one value is present, like in row 1 in the first sheet. Also the formula should only fill in a value if both columns match. All text combinations in both sheets are unique.
Is it possible to do this with text? Thank you for your help!
Try the following
=ArrayFormula((IFERROR(VLOOKUP(R2:R&S2:S,{O2:O&P2:P,Q2:Q},2,0))&
IFERROR(IF((R2:R<>"")*(S2:S<>""),VLOOKUP(R2:R&S2:S,{P2:P&O2:O,Q2:Q},2,0),""))))
(Do adjust locale and ranges according to your needs)
Functions used:
ArrayFormula
IFERROR
IF
VLOOKUP
If you can use two keys concated in the lookup table as a virtual key (i.e. make a key like "CAT|DOG"), then you can use that to look in the secondary table.
If you can't guarantee the sort order of the two keys in the secondary table, you can use the following technique to "sort" the two keys so you can make a single lookup key that's always in one stable order.
Sample Table
A
B
C
D
E
SortedKey
Cat
Dog
TRUE
Cat
Dog
CatDog
Dog
Cat
FALSE
Cat
Dog
CatDog
Formulas
Sample formulas for row #1.
For column C, use formula: =A1<B1
For column D, use formula: =IF(C1=TRUE, A1, B1)
For column E, use formula: =IF(C1=TRUE, B1, A1)
For SortedKey, use formula: =concat(D1, E1)

How to definitely use column names in Google Sheet Query

query function doesn't let you use column names; you have instead to use letters if you refer to a cell range or ColN if you refer to an array.
This is very annoying, most of all when you alter the queried table adding, deleting or exchanging columns.
I would like to use column names, like in a standard SQL query.
You can actually get around this by splitting the Query formula and using other formula's to automatically get the desired column names from a list.
For example if you have a table in range A1:E15 with headers "H1, H2, H3, H4, H5", and you'd like to only get columns H3 & H5:
Store the desired headers (H3 & H5) in another table/range as a list - lets say this range is G1:G2
Use MATCH formula along with TextJoin formula to generate an concatenated string like Col3, Col5
=TextJoin(", ",TRUE,ArrayFormula(IFERROR("Col"&MATCH(G1:G6,$A$1:$E$1,0),"")))
Lets say this was in cell H1
You can refer to this cell in your Query formula like below
=QUERY({A1:E20},"SELECT "&H1&" WHERE Col2='w'")
You can see it in action in below screenshot:
One solution could be recurring to some custom function created by a script, but when you have a not so small table you surely will incur in some error due to the exceeding computation time.
The most efficient solution (using only native functions) I found is as follows.
Suppose you are working on a sheet range, your column names are in row 1 and you want to refer to the column "salary"; you can obtain the column letter by
substitute(address(1,match("salary",A1:1,0),4),"1","")
Instead, if you are querying arrays, it is simpler; the string you need is
"Col"&match("salary",A1:1,0)
The final query could be not so elegant, but the efficiency is guaranteed:
query(
employeessheet!A:E,
"select "&substitute(address(1,match("salary",employeessheet!A1:1,0),4),"1","")&" where ...",
1)

Reduce a range of values to a list in Google Sheets

I have a table with printer names and the model numbers of the toner and drums which they use. I'm looking for a way to generate a single column list from this range of values, excluding blank cells. Below is the table:
I'd like to end up with a list of values as such:
MX-C30NT-B
MX-C30NT-C
MX-C30NT-M
MX-C30NT-Y
MX-C30DR
MX-C30HB
MX-C30NV-B
MX-C30NV-C
MX-C30NV-M
MX-C30NV-Y
...
I know in Excel I can use an array formula to accomplish this, but array formulas don't seem to be as robust in Sheets. Is there a formula which will perform this conversion?
Combining nonempty cells in columns E-M into a single column, ignoring the header row:
=filter({E2:E; F2:F; G2:G; H2:H; I2:I; J2:J; K2:K; L2:L; M2:M}, len({E2:E; F2:F; G2:G; H2:H; I2:I; J2:J; K2:K; L2:L; M2:M}))
The first argument is just a bunch of column stacked; the filter excludes empty ones. Note that the second {...} construct is identical to the first, it's a copy-paste.

Resources