Categories

## How To Join Multiple Columns Into 1 Sorted Unique Column

Further to my previous post on combining 2 columns into one column I’ve had to do some further work on this by now combining a number of unknown columns into one unique column and then sorting the result.

I’ve been able to achieve this and will demonstrate one way in how I’ve been able to get it to work. Let’s use a working example to show how it works.

First, we’ll acquaint ourselves with the old method of using this formula, which in this case would be the following for the following three columns (column `A` will be our results column):

``=unique(transpose(split(join(";",A:A)&join(";",B:B)&join(";",C:C),";")))``

Unfortunately the problem with this method is that is requires us to enter in the `join(delimiter,range)` function for each new column, and I want the function to be able to concatenate all cells in a range of columns.

Our first remedy at finding a solution avails itself when using the `ArrayFormula` function. As this function will loop through each of the cells in your array (being a range of cells) and will output each cell according to the way you define the function inside.

As an example, if we were to apply the following formula in cell A1 we would get the following:

``=arrayformula(concatenate(if(len(B:D)>0,B:D&";","")))``

As you can see what happens in the above function is that the `ArrayFormula` goes through my range `B:D` (being everything in columns B to D) and once it finds a cell that contains something (a string length greater than 0) it then gets that result and appends a semi-colon to it – this is denoted with the true statement in the `if` statement by `B:D&";"`.

Now we simply merge the two formulas together, in essence replacing the `join` section of our previous formula with the `ArrayFormula` above, producing:

``=unique(transpose(split(arrayformula(concatenate(if(len(B:D)>0,B:D&";",""))),";")))``

Lastly, if we wanted to sort the result of this output we would finally wrap the resulting formula in the `sort` function, where we would have everything sorted from A to Z:

``=sort(unique(transpose(split(arrayformula(concatenate(if(len(B:D)>0,B:D&";",""))),";"))))``

Of course if I didn’t know how many columns I would be expecting in this sheet I would amend the last column name to a much large letter, eg.

``=sort(unique(transpose(split(arrayformula(concatenate(if(len(B:Z)>0,B:Z&";",""))),";"))))``

However, do be aware processing time will increase with the more data columns you add.

Categories

## How To Easily Join 2 Columns Into 1

UPDATE (4 Nov 13): I’ve created another post that allows you to perform the same task demonstrated below, but works better with multiple columns (rather than just two as detailed below).

There are times within a Google Spreadsheet (or I guess any spreadsheet for that matter) where you need to merge two columns together to form one column. It comes in really handy when you are looking to use a series of cells for data validation and both bits of data span over two separate columns.

Just how can we get them into one column?

I had a case recently where I needed to append the contents of one column underneath the contents of another column, with the size of both column’s height unknown (this eliminated the easier approach of simply placing the contents underneath manually).

I was able to find a solution, and I’ll illustrate how it worked by using an example. Let’s assume the following columns of data:

The first thing we will do is move to the next adjacent column, column `C`, and in cell `C1` enter the following formula:

``=join(";",A:A)&join(";",B:B)``

What this formula does is mesh all the data in the first column into a single string where each cell’s content is appended together with a semi-colon. Now if your data contains semi-colons you will want to use another symbol that is not used in your data set.

Our output in cell `C1` would look a little something like this:

``Lions;Tigers;Elephants;Lemurs;ABC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Red;Yellow;Green;Blue;White;Black;ABC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;``

Now we’re going to amend our code in `C1` so that we can split each item back into their own cell, our code in cell `C1` will now look like so:

``=split(join(";",A:A)&join(";",B:B),";")``

This would nor produce something a little like this:

Then as we need the data to be placed into a column we would transpose our data, by amending our cell in C1 to this:

``=transpose(split(join(";",A:A)&join(";",B:B),";"))``

Giving us:

Now if we want to remove any pieces of data that are the same within both columns we would just further wrap the UNIQUE function around the TRANSPOSE function. In our working example this would remove the second ABC in our new column. Here’s the formula firstly:

``=unique(transpose(split(join(";",A:A)&join(";",B:B),";")))``

Resulting in:

This has certainly helped me when operating with ImportRange function calls and then working with the imported data in the active sheet. Hopefully it helps you too!