Merge Values from Two Columns into One in Google Sheets

This tutorial describes two methods for merging values from two columns into one column in Google Sheets.

The first method combines values, such as merging first names from one column with last names from another. This is straightforward to do.

The second, less common method involves using a category column and another column. In this approach, the values from the second column are placed below each category. The values in the first column act as labels that separate the values in the second column.

Merge Values from Two Columns by Concatenating

Merging values from two columns by concatenating in Google Sheets

Assume you have first names in A2:A and last names in B2:B.

In cell C2, you can either enter the following formula and drag it down as far as needed:

=A2&" "&B2

Alternatively, clear the contents of the range C2:C and enter the following array formula in cell C2:

=ArrayFormula(A2:A&" "&B2:B)

These are the simplest ways to merge values from two columns into one column.

Sometimes, you might want to use a different character to separate values. In that case, you can replace " " with "-" to place a hyphen or use any other character you choose.

However, this approach may result in unwanted separators if the second column does not contain values. To handle this, you can use this formula:

=ArrayFormula(A2:A&IF(B2:B= "", "", "-")&B2:B)

This formula removes the separator when the second column is empty. Similarly, if you are using a space as the separator, the formula would be:

=ArrayFormula(A2:A&IF(B2:B="", "", " ")&B2:B)

This formula prevents adding a space character when no last name is present, even if it’s not visible.

The above methods are the simplest ways to merge values from two columns into one.

Merged List with Categories and Values in Google Sheets

This type of merging is useful when you have a category column and a value column to combine.

The sample data consists of weekday names in column A and sales values on those days in column B.

Merging two columns by placing values below categories in Google Sheets

For instance, if three sales occurred on Monday, you will have “Monday” in three rows in one column and the corresponding sales values in another column.

After merging, the first row will contain “Monday,” followed by the three rows with the values from the second column. There will be multiple categories as well.

We can use two types of formulas to merge values from two columns this way in Google Sheets: with or without using LAMBDA functions.

Merge Values from Two Columns Without Using LAMBDA

The LAMBDA function is known to have performance issues when applied to very large datasets, so I would prefer this method. However, the LAMBDA approach has the advantage of being more flexible, which I will discuss later.

If your sample data is in A2:B and it’s sorted by column A, use this formula in cell C2 after clearing the contents in C2:C:

=ArrayFormula(LET(
   range1, A2:A, 
   range2, B2:B, 
   rc, COUNTIFS(range1, range1, ROW(range1), "<="&ROW(range1)), 
   merge, FLATTEN({IF(rc=1, range1,), range2}), 
   FILTER(merge, merge<>"")
))

Do you want to know how this formula merges values from A2:A and B2:B by using unique values in A2:A as row separators for B2:B?

Formula Explanation

The formula uses the LET function to assign names to the ranges A2:A and B2:B as range1 and range2, respectively.

COUNTIFS(range1, range1, ROW(range1), "<=" & ROW(range1)) calculates a running count of values in A2:A, where each category start is numbered 1. This running count is named rc.

FLATTEN({IF(rc = 1, range1,), range2}) – Here, the IF logical test returns values from range1 (A2:A) where rc (running count) equals 1. This is combined with the values from range2 (B2:B) using curly brackets. The FLATTEN function combines these two columns into a single column, named merge.

The issue with merge is that it may contain blank cells due to the flattening process.

FILTER(merge, merge <> "") removes these blank cells.

This generates a merged list with categories and values, where categories serve as separators.

Merge Values from Two Columns Using LAMBDA

Merging values from two columns with added empty cells between categories

You can replace the previous formula with the following LAMBDA-based formula:

=IFNA(TOCOL(
   HSTACK(
        MAP(
           TOCOL(UNIQUE(A2:A), 1), 
           LAMBDA(col, HSTACK(col, TOROW(FILTER(B2:B, A2:A=col))))
        )
   ), 1
))

This formula uses the MAP LAMBDA function to merge values from two columns.

Formula Breakdown

TOCOL(UNIQUE(A2:A), 1) returns unique categories and removes empty cells. The MAP function maps this array and applies the LAMBDA function to iterate through each value in the array.

Monday
Tuesday
Wednesday
Thursday
Friday

LAMBDA(col, HSTACK(col, TOROW(FILTER(B2:B, A2:A = col)))) – This LAMBDA function filters the values in B2:B based on the current value (col) from the array. TOROW transforms the output into a row. HSTACK appends the category value (col) with the filtered results.

Monday100125130

The MAP function processes each value in the array, transforming the results into rows and appending them to the category value.

Monday100125130
Tuesday5055
Wednesday100
Thursday120130
Friday140

TOCOL transforms the merged rows into a single column. The HSTACK function helps to insert empty rows between categories, and IFNA handles errors by replacing them with empty cells.

Adding Blank Rows Between Categories:

If you want to add blank rows below each category, you can specify " " within HSTACK, as shown below:

=IFNA(TOCOL(
   HSTACK(
        MAP(
           TOCOL(UNIQUE(A2:A), 1), 
           LAMBDA(col, HSTACK(col, TOROW(FILTER(B2:B, A2:A=col))))
        ),""
   ), 1
))

This will leave one blank row below each category in the merged column. To add two blank rows, specify " ", " ".

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Marking Case-Sensitive Unique Values in Excel

Marking case-sensitive unique values provides several benefits compared to merely extracting them in an...

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

7 COMMENTS

  1. Hi Prashanth,

    Is it possible to do the same as the above but insert the new group at intervals of 10 (assuming that there are always less than 8 values per day)?

    i.e Monday and its values starting from C1, Tuesdays and it’s values starting from C11, Wednesday and it’s values starting from C21, etc.

    So it won’t matter how many values are assigned to each day, the new group is started at the next interval.

      • Apologies, I have created an example sheet with a better explanation:

        (URL has been removed by the Admin)

        I’ve also added an extra request on the sheet to add a standard string between column A and column B when combining them.

        • Hi, Jodan,

          Sorry! I don’t find a way to modify the current formula. But I do have some ideas. Here is it.

          In the “New Example” tab, empty the columns D1:Z1.

          Insert the below unstack formula in cell E1.

          =ArrayFormula(transpose(trim(sort(split(transpose(query(
          {transpose(unique((A2:A)));IF(len(B2:B),IF(transpose(
          unique(A2:A))=A2:A,"-"&B2:B,""),"")},,1000)),"-")))))

          The above formula is from my Unstack Data tutorial.

          Then use the following Flatten in cell L1 to merge the two columns.

          =flatten(transpose(F1:J10))

          Note: This is an untested formula. It has issues like sorting Weekdays in A-Z order.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.