Combine Rows into a Delimited List

This is the first of two related posts that demonstrate how to use Power Query to deal with rows and delimited lists. In this first post, we’ll combine rows into a delimited list. In the second post, we’ll do the opposite and convert a delimited list into rows. Well, what are we waiting for … let’s get to it!

Objective

Let’s say we’ve exported data from our accounting system. Rather than displaying one row for each record, our system uses many rows for each order. Plus, the orders have a variable number of rows. In other words, some orders have 4 rows while others have 3 and so on. This is illustrated below:

But, what we need is a single row per OrderID, with the Attribute and Value strings combined in a single delimited list. Basically, we want it to look like this:

As with anything in Excel, there are multiple ways to accomplish this task. Currently, I’m on a Power Query kick, so this post will demonstrate how to do it with Power Query.

Here are the steps:

  • Create our basic query
  • Do a few transformations
  • Return the data to our worksheet

Let’s get to it.

Note: The steps below are presented with Excel for Windows 2016. If you are using a different version of Excel, please note that the features presented may not be available or you may need to download and install the Power Query Add-in.

Create our basic query

First, we need to get our data table from our worksheet into Power Query. So, we select any cell in the table and click Data > From Table/Range. And, just like that, we have our data loaded into the Power Query window, as shown below.

Now the fun begins 🙂

Do a few transformations

In this case, we want to retain both the Attribute and Value text, so, we’ll combine them into a single column and use a colon : delimiter. We do this by selecting both the Attribute and Value columns at the same time (Ctrl + click) and then select Transform > Merge Columns. The Merge Columns dialog is displayed, we pick the colon Separator and set the new combined column name to Merged, as shown below.

We click OK, and the updated query is shown below.

Now, we need to create one row for each OrderID. We can do this by clicking the Transform > Group By command. The Group By dialog is displayed. We want to Group by the OrderID column and we want the new column to be named Data and to contain All Rows, as shown below.

We click OK and the updated query is shown below.

Now, this is the cool part. And this part I learned from Ken Puls and Miguel Escobar during their workshop, which, was totally awesome by the way 🙂 They taught a bunch of the content from their wonderful book called M is for (Data) Monkey, which, I highly recommend.

We need to create a new column, so, we select Add Column > Custom Column. The Custom Column dialog opens where we specify any column name and then write the following formula:

=Table.Column([Data],"Merged")
  • Where [Data] is the name of the table column, and “Merged” is the column name we set up previously.
  • Note: if you used different names then you’ll want to update the formula accordingly.

Now, this creates a new list column, as shown below.

Now, from here, we click the Expand icon on the right side of the Custom header and select Extract Values as shown below.

This displays the Extra values from list dialog, where we specify our desired delimiter, in this case a Semicolon, as shown below.

We click OK … and Bam! (shown below)

Since we really don’t need the Data column any longer, we can select the column and then use the Remove Columns command. Now, we are ready to send the results to Excel.

Return the data to our worksheet

This part is really easy. We just click Close & Load, and now we have the results loaded into a worksheet, as shown below.

Now, the best part about this approach is that tomorrow, next week, or next month when we have an updated export table, we just right-click the results table and hit Refresh. And done. No need to go through these transformation steps again. Booya!

If you’d like to practice, feel free to check out the Sample File below.

And in the next post, we’ll do the reverse and assume our export contained a delimited list that we need to expand into multiple rows.

If you have any related Power Query tips, please share by posting a comment below.

Sample File

Get a quick email notice when a new Excel article is available

  • This field is for validation purposes and should be left unchanged.

This article was written by Jeff Lenning

1 comment:

  1. Frances Reed
    Reply

    Cool Transformation!

Leave a Reply

Your email address will not be published. Required fields are marked *

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

If you agree to these terms, please click here.