Google sheet: joint text from multiple columns - google-sheets

On a new tab, for each row, I want to enclose the text of all the columns from my dataset tab that contains the word "WORD" in its 2nd row.
I cannot directly target the column letter, and the number and place of columns containing "WORD" will change over time.
I've tried with HLOOKUP and QUERY, I can't get there.
Example
dataset
#
Another header
Another header
xxxx
WORD
WORD
1
contentA
contentC
2
contentB
contentD
new tab
#
ALL WORD
1
contentA ContentC
2
contentB ContentD

use:
=FLATTEN(QUERY(TRANSPOSE(A1:B);;9^9))
or:
=INDEX(TRIM(FLATTEN(QUERY(TRANSPOSE(A1:B);;9^9))))
update:
=INDEX(TRIM(FLATTEN(QUERY(QUERY(TRANSPOSE(FILTER(
dataset!A2:99999; REGEXMATCH(dataset!1:1; "(?:)WORD")));;9^9)))))

use:
=ARRAYFORMULA(TRANSPOSE(TRIM(QUERY(TRANSPOSE(FILTER(dataset!3:100000,dataset!2:2="WORD")),,9^9)))
the use of the number 100000 is intentional, it should be more rows than you'd ever have.

Related

Is there a function in Google Sheets to return the string next to a string that I match from a column of strings?

Okay, Sheet1!F:F is a list of words in English. The same word occurs multiple times and the sheet is organized in order of chapters with the words in question in order as they appear in the chapter. "G:G needs to be that word in Arabic. "H:H needs to be the definition in English. "I:I needs to be the definition in Arabic.
Sheet2!A:A has the word in English, B:B the word in Arabic, C:C the definition in English, D:D the definition in Arabic.
Is there a function that would allow me to find the word from Sheet1!F:F in Sheet2!A:A and return Sheet2!B:B in Sheet1!G:G?
Here's some snipits of an example sheet.
Sheet1!
Sheet2!
You want to find the "Word AR" Sheet1 column G in "Word AR" Sheet2 column B, in other word find the arabic word for the English word from another table.
Paste this formula in Sheet1 cell G2, and drag it down.
=IF(F2="",,INDEX(Sheet2!$B$2:$B,IFNA(MATCH(F2,Sheet2!$A$2:$A,0),"No Match")))
Breakdown:
1 - MATCH function to find the matching row in the range Sheet2!$A$2:$A with [search_type] set to 0 to finds the exact value when range is unsorted.
2 - INDEX gives back a cell's content from a range when given a row and column, our reference is Sheet2!$B$2:$B we set the [column] as 1 or left it blank in case of a single column and pass the result of MATCH function as [row].
3 - handel N/A error with IFNA function and set [value_if_na_error] to "No Match".
4 - IF function IF(F2="",,[value_if_false] To calculate only when the cells of F columns are not blank.
hope that answers your question.
One option would be to use a VLOOKUP formula. For example:
=ifna(arrayformula(vlookup(A2:A, Sheet2!A2:D, 2,0)))
Sheet1:
Sheet2:
This formula can be adjusted to fit your needs:
=ifna(arrayformula(vlookup(F3:F, Sheet2!A2:D, 2,0)))
This can be placed in cell G3 of your Sheet1 and it will auto fill down the column. Repeat this for the next 3 columns, and simply increment from 2 in the original (ie =ifna(arrayformula(vlookup(F3:F, Sheet2!A2:D, 3,0))), etc)

Using ArrayFormula with a Dynamic Number of Column Header Names

My goal is to use ArrayFormula with the SPLIT() function, and name the headers of each column.
My problem is that the formula below only works when the number of headers declared exactly matches the first row's number of elements to split ie. if there are 3 elements being split on the first row, the formula needs 3 headers named (g1, g2, g3), but if any rows have more than 3 elements to split, it gives an error.
Is there a way to make the column header names dynamic in number, so that the number of elements to split can be, say, from 0-10? The elements to be split will always be separated by a comma and no spaces.
=ArrayFormula({"g1", "g2", "g3";if(A2:A="","",split(A2:A,","))})
link to example: https://docs.google.com/spreadsheets/d/1c2pskSYsGs12Yjbn-5gORQ22mDSaC9cSnp1nWeULlf4/edit?usp=sharing
You can try:
=index(iferror({"g"&sequence(1,max(len(substitute(
transpose(query(transpose(if(iferror(split(A2:A,","))="",,"z")),,9^9)),
" ",))));split(A2:A,",")}))
If we can use the Orders column, it's as simple as:
=index(iferror({"g"&sequence(1,max(B:B));split(A2:A,",")}))
You can achieve it by combining the index function, the sequence function and the max function. Here is the thought process behind it:
The max function (you can read more about it here) will retrieve the maximum value of the orders column.
The sequence function (you can read more about it here) will generate a series starting at 1 and ending at the previous maximum value.
The index function (you can read more about it here) will distribute the elements of the sequence (with a "g" in front) across as many cells as elements are in the sequence.
If you combine those, you get:
=INDEX("g"&SEQUENCE(1,MAX(B:B)))

How can I generate a three column list of unique "combos"?

I have three columns of information. For example: color, model, year.
Can I use the "unique" instruction to generate in three new columns each unique combination for color, model, year, each in one column?
ex.
color model year
red sedan 2016
red sedan 2020
black truck 2018
Thanks!
Suppose your three headers are in A1, B1 and C1 with your data running A2:C. And suppose you want the unique combinations in E:G. First, be sure that the entire range E:G is empty. Then place the following formula in E1:
=ArrayFormula({A1:C1;SPLIT(FLATTEN(UNIQUE(FILTER(A2:A,A2:A<>""))&"|"&TRANSPOSE(FLATTEN(UNIQUE(FILTER(B2:B,B2:B<>""))&"|"&TRANSPOSE(UNIQUE(FILTER(C2:C,C2:C<>"")))))),"|")})
The formula first reproduces the headers from A1:C1.
The combinations are formed by first concatenating each UNIQUE model (from a list that is FILTERed to remove blanks) with each UNIQUE year (from a list that is also FILTERed to remove blanks), with a pipe symbol between each as a separator that SPLIT will later use.
That grid of combinations is FLATTENed into a single column and then concatenated once more with a UNIQUE and FILTERed list of the colors leading off, and again with a pipe symbol as a separator. Once more, the entire grid of results is FLATTENed into a single column.
Finally, SPLIT acts on the pipe symbols to separate the three pieces into their own columns under the headers.
try:
=INDEX({A1:C1; UNIQUE(QUERY(SPLIT(FLATTEN(FLATTEN(A2:A&"×"&
TRANSPOSE(B2:B))&"×"&TRANSPOSE(C2:C)), "×"),
"where Col3 is not null"))})
the task is simple: take column A and combine it with transposed column B. flatten the output in one single column and combine it with transposed column C and again flatten it into one single column. then split it and query out all combinations that have less than 3 columns. next, run it through unique to remove duplicates.

VLOOKUP remove spaces when cell is empty

This a simple customer sheet:
A B C D
ID First Middle Last
1 John Doe
2 Jane Maia Doe
And in F1 I put this vlookup code:
=VLOOKUP($G$1;$A$1:$D$3;2;FALSE)&" "&VLOOKUP($G$1;$A$1:$D$3;3;FALSE)&" "&VLOOKUP($G$1;$A$1:$D$3;4;FALSE)
When I lookup ID 2, it's perfect nicely spaced between the vlookups
But when I lookup ID 1 you see 2 spaces between the first and last name, because there is no middle name here.
How can I manage that I always see 1 space between the vlookups?
One way you could achieve the result you're looking for is to simply replace multiple spaces with a single space.
=REGEXREPLACE(JOIN(" ",ARRAYFORMULA(VLOOKUP(G1,A:D,{2,3,4},FALSE))),"\s{2,}"," ")
This formula looks up G1 in your table (A:D). VLOOKUP can be used in an ARRAYFORMULA to efficiently retrieve all of the columns you want in one shot. Your JOIN joins all of the retrieved columns, inserting a space between each value. Finally, your REGEXREPLACE function looks for multiple consecutive spaces and replaces them with a single space.
Alternatively, you could filter the resulting array (i.e. the result of what your VLOOKUP returns). The following formula looks up the array of first, middle, and last name, and then filters out any empty cells before joining the remaining elements with a space.
=JOIN(" ",FILTER(VLOOKUP(I1,A:D,{2,3,4},FALSE),INDIRECT("B"&MATCH(I1,A:A,0)&":D"&MATCH(I1,A:A,0))<>""))
all you need is TRIM fx and:
=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(
VLOOKUP(G1:G2, A1:D3, {2,3,4}, 0))),,999^99))))

Get data between number two and three delimiter

I have a large list of people where each person has a line like this.
Bill Gates, IT Manager, Microsoft, <https://www.linkedin.com/in/williamhgates>
I want to extract the company name in a specific cell. In this example, it would be Microsoft, which is between the second and third delimiters (in this case, the delimiter is ", "). How can I do this?
Right now I'm using the split method (=SPLIT(A2, ", ",false)). But it gives me four different cells with information. I would like a command only to output the company in one cell. Can anyone help? I have tried different things, but I can't seem to find anything that works.
Maybe some regex can do it, but I'm not into regex.
Short answer
Use INDEX and SPLIT to get the value between two separators. Example
=INDEX(SPLIT(A1,", ",FALSE),2)
Explation
SPLIT returns an 1 x n array.
The first argument of INDEX could be a range or an array.
The second and third arguments of INDEX are optional. If the first parameter is an array that has only one row or one column, it will assume that the second argument corresponds to the larger side of the array, so there is no need to use the third argument.
A bit nasty, but this formula works, assuming data in cell D3.
=MID(D3,FIND(",",D3,FIND(",",D3)+1)+2,FIND(",",D3,FIND(",",D3,FIND(",",D3)+1)+1)-FIND(",",D3,FIND(",",D3)+1)-2)
Broken down, this is what it does:
Take the Mid point of D3 =MID(D3
starting two characters after the 2nd comma FIND(",",D3,FIND(",",D3)+1)+2
and the number of characters between the 2nd and 3rd comma, excluding spaces FIND(",",D3,FIND(",",D3,FIND(",",D3)+1)+1)-FIND(",",D3,FIND(",",D3)+1)-2)
I'll add my favourite ArratFormula, which you could use to expand list automatically without draggind formula down. Assumptions:
you have list with data in range "A1:A20"
all data have same sintax "...,Company Name, <..."
In this case you could use Arrayformula, pasted in cell B1:
=ArrayFormula(REGEXEXTRACT(A1:A20,", ([^,]+), <"))
If your data doest's always look like "...,Company Name, <..." or you wish to get different ounput, use this formula in cell B1:
=QUERY(QUERY(TRANSPOSE(SPLIT(JOIN(", ",A1:A20),", ",0)),"offset 2"),"skipping 4")
in this formula:
change 2 in offset 2 to 0, 1, 2, 3 to get name, position, company, link
in skipping 4 4 is a number of items.
Number of items can be counted by formula:
=len(A1)-len(SUBSTITUTE(A1,",",""))+1
and final formula is:
=QUERY(QUERY(TRANSPOSE(SPLIT(JOIN(", ",A1:A20),", ",0)),"offset 2"),
"skipping "&len(A1)-len(SUBSTITUTE(A1,",",""))+1)

Resources