Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook

Power Query – Unstacking data in a column

Power Query - Unstacking Data in a Column

As we continue through this Power Query series, we have now reached a common and frustrating data format; data stacked in a column.  In this post, we will investigate one basic method for unstacking data.

While the example we are looking at below would most likely be created by a person who didn’t know how to structure data correctly, this format can equally be found in data exported, or copied from a IT system (though exported or copied data tends to be a single column, which is slightly easier to handle).

The screenshot below shows the Address has data stacked in a column.

Data in a stacked format

As you can imagine, manipulating data in this layout is exceptionally difficult.  To make it useable, each row of the address should be a separate column.  Power Query comes to the rescue once again.

Download the example file

I recommend you download the example file for this post.  Then you’ll be able to work along with examples and see the solution in action, plus the file will be useful for future reference.

Download Icon
Download the file: Power Query – Example Files

The examples in this post use the Example 13 – Unstack data.xlsx file.

Unstacking a column of data

Open the Example 1 worksheet from the downloaded file.  The data looks like the screenshot we saw above.

The data is already in a Table format, which I have named Customers.  To import the data into Power Query, select any cell in the Table and click Data -> From Table/Range from the ribbon.

The Power Query window will open and display the data, which will look like the screen show below:

Data in a stacked format - PQ editor

Tidy up the import

Now let’s work through the steps to get the separate address lines into columns.  First, let’s remove any blank rows by filtering the Address column to remove null values.

Filter to remove null

Click on the Customer column, then click Transform -> Fill (dropdown) -> Down from the ribbon.  This will ensure each row has the customer name in it; there should not be any null values in the Customer column.

Transform Fill Down

Adding an Index and Modulo column

For the next few transformations, things start to seem a bit odd.  Once all the steps are complete, it will make sense, but until then you’ll just have to trust me.

There are 3 rows for every customer address.  Our next transformations are to include a column of numbers which show the numbers 0, 1 and 2 repeatedly.  0 will represent the first row of the address, 1 the second row of the address, and 2 the last line of the address.

Click Add Column -> Index Column

Add Column - Index Column

An index column has been added, starting at zero.

Select this new Index column, then click Add Column -> Standard -> Modulo.

Add Modulo column

Enter 3 into the Modulo window, as there are 3 rows in each address.  Click OK.

Modulo window

If you’ve followed all the steps above the Preview Window should look like the screenshot below:

Preview Window after modulo applied

Select the Modulo column we created.  Click Transform -> Pivot Column.  In the Pivot Column window select the Address column, expand the advanced options, and select Don’t Aggregate, then click OK.

Pivot Column Window

Take a look at the preview window, you probably think we’ve broken everything, but it’s all part of the process. We are about to bring this thing back into order.

Preview Window after Pivot on Modulo column

Select the columns with the headers 1 and 2, then apply the Transform -> Fill Down transformation.

Select the 0 column and filter to remove the null values.  The Preview Window will now show the data in the following format:

Preview Window after Remove Null

Magic eh? We went from thinking that everything was broken, to a nice and tidy data set in just a few basic transformations.

One of the critical skills for Power Query is the ability to make lots of small transformations which eventually add-up get the data into the right format.  Often something which may seem like a small transformation, such as unstacking data, may require a lot of smaller steps.

Finishing up

Just a few simple transformations remain to tidy everything up:

  • Remove the Index column (Select the column and click Home -> Remove Columns)
  • Rename the 0, 1 and 2 columns (double-click each column heading and provide a meaningful name).

That’s it.  The data is now ready.  Click Close and Load to push the data into Excel.

Unstacked data - completed

What does this teach us?

In this post, we’ve seen how to unstack a column of data.  But more importantly, we’ve seen that outcomes which might seem basic can be quite complex.  Understanding simple transformations and how to combine them together is something which takes time and thought to learn.

There are other methods for unstacking data, while they take less steps, they are more complex for a beginner to understand, so are beyond the scope of this beginners series.

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).

Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

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