Power Query: Rows of Columns
11 September 2019
Welcome to our Power Query blog. Today, I split a column into rows.
I have received some sales data from John, my favourite imaginary salesperson. He’s decided to merge some of his data.
I have a list of companies on each line, instead of one row for each company. I want to have one row per company so that I can link to other company data.
I start by extracting my data to Power Query using ‘From Table’ on the ‘Get & Transform’ section of the ‘Data’ tab:
I keep the headings and create my table.
I want to split the column Company. I select this column and right-click to see the options:
I can split my column ‘By Delimiter…’, so I choose this option.
I can choose to split by semicolon (;) – however, I don’t want to split into multiple columns, as each separate piece of data will be a company, so I look at the ‘Advanced options’ available.
I can split into rows, so I choose this option instead.
I now have a row for each company, with one simple step (and no M code knowledge required!). The generated M code is:
= Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Company", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Company")
This has used the M function Table.ExpandListColumn():
Table.ExpandListColumn(table as table, column as text) as table
Given a column of list data in a table, this creates a copy of a row for each value in its list.
Power Query has converted the Company column to a column of lists by using Splitter.SplitTextByDelimiter(), and then converted that list into rows. In this case, my data was delimited by a simple semicolon, but there are also options to use special characters, making this a very powerful function when dealing with complex columns.
Come back next time for more ways to use Power Query!