The demand to unstacking a column into a table is not rare (see here for example: PowerBIForum ) . Also if you copy a table from a post in the Power BI community forum to the enter-data-section in Power BI, it will show up as such a one-column-table.
Task
Solution
Table.FromRows( List.Split( Table1[Column1], 3) )
..provided that your table is named “Table1”
How it works
You have to read a formula like this like an Excel formula: From inside out.
-
Transform a table to a list-format by selecting the relevant column (here: [Column1])
2. Chunk up the list into a nested list with one list per each row
The number in the last argument of the function determines the number of columns of the desired table.
3. Create table from that list
This formula creates a table from a list of rows. Those rows have to be in the format of a list as well, so the desired input matches exactly the format from step 2.
4. Optionally: Integrate column names for the new table
Benefits
I prefer this formula-solution to other methods that use the UI and add index- and modulo columns before finally pivoting. Mainly, because I don’t have to create a function to apply this transformation within an add-column-step where I want to apply it to all rows of a table.
Enjoy & stay queryious 😉
Hey Imke,
That’s very cool. Definitely like the simplicity of the single line of code. I like the UI approach better for newer users (M code can be intimidating), but that is pretty awesome once you get comfortable. Would be interesting to compare the two approaches for larger data sets. 🙂
Thank Ken 🙂
Haven’t compared performance on this specific case, but used the Table.FromXXX – functions fairly often for other use cases where they have been extremely fast.
Hi Imke 🙂
This is exactly the same code i have been using for years
Good to know that we think alike :-))
sq
Thanks Bill 🙂
Very good do know that I’m still on your track 😉
sq
Pingback: Converting One Column Into Multiple With Power Query – Curated SQL
It’s sad that List.Split function does not exist in Excel version 🙁
Oh, it does exist in Power Query (Get Data) in Excel as well 😉
Oh yes it works ! Great !
I love your blog 🙂
Great solution!
One issue I have is that when I tried to include the data type I want in the Table.FromRows function it doesn’t convert the data to the type I identified. It’s identified properly in the column headers, but the data in the columns does not convert. Can anyone else replicate this? And if so do you have any idea why it won’t convert the data to the type identified for the column?
Here’s the formula I used:
Table.FromRows(List.Split(Source[Column1],3), type table [ID = Int64.Type, Name = text, Age = Int64.Type])
Hi John,
you’re just ascribing a type with that transaction. That will NOT convert the values in the columns. (See this blogpost for details: https://www.poweredsolutions.co/2018/03/12/data-types-data-conversion-ascribed-data-types-power-query-power-bi/) .
Like this for example:
Table.TransformColumnTypes(Table.FromRows(List.Split(Source[Column1],3), type table [ID = Int64.Type, Name = text, Age = Int64.Type]),{{“ID”, type number}, {“Name”, type text}, {“Age”, type number}})
A shortened code would then be this:
Table.TransformColumnTypes(Table.FromRows(List.Split(Source[Column1],3), {“ID”, “Name”, “Age”}),{{“ID”, type number}, {“Name”, type text}, {“Age”, type number}})
Perfect. Thank you!
That’s a great solution! Thanks!
I was facing the unstacking task just recently and couldn’t find something like the List.Split function. It turns out that Microsoft’s online documentation did not include the “List.Split” function in the function index on the left hand side, but did mention it in the “List functions overview” page. Is there a way to let Microsoft know about this issue and re-index that documentation?
Hi Ben,
There is a feedback-button down to the right (“Did you find the page helpful?”) where you can paste that comment.
I will try my contacts as well
/Imke
Cool, already reported. Thank you!
@Imke
The below should work as well
= #table(3,List.Split(D[Column1],3)) – Default column headers
or
= #table({“Id”,”Name”,”Age”},List.Split(D[Column1],3)) – Specified column headers
or
= #table(type table [Id=number,Name=text,Age=number],List.Split(D[Column1],3)) – specified column headers and data types
Thanks for the code. How can I split the tables if I have 3 tables stacked together instead of 1 column stacked per your example. Many thanks
Not sure if I understood your request right, but you might check out this code:
let
Source = #table({"StackedTables"}, {{#table({"Column1", "Column2"}, {{"A", "C"}, {"B", "D"}})}, {#table({"Column1", "Column2"}, {{"E", "G"}, {"G", "H"}})}}),
Custom1 = Source[StackedTables],
TransformTablesToColumns = List.Transform(Custom1, Table.ToColumns),
UnionListOfColumns = List.Union(TransformTablesToColumns),
TableFromColumns = Table.FromColumns(UnionListOfColumns)
in
TableFromColumns
Otherwise, please specify your case more clearly please.
Thanks and cheers,
Imke
Nice solution, but there is a problem.
Let us assume we have the alphabet in the column, means 26 values, and we want to create columns with 10 chars in each:
= Table.FromRows(List.Split(Table1[Column1],10))
returns a table full of errors.
The problem seems to be that the last list has only 6 items. Is there a simple workaround for that?
Thanks, Andreas.
Hi Andreas,
that’s fairly simple:
Table.Transpose(Table.FromColumns(List.Split(Table1[Column1],10)))
/Imke
Seems not to work in Excel Power Query:
let
Source = Excel.CurrentWorkbook(){[Name=”Table1″]}[Content],
Data = Table.Transpose(Table.FromColumns(List.Split(Source[Column1],10)))
in
Data
Here’s my sample file:
https://www.dropbox.com/s/uq7vyekose45p7h/unstacking-columns-in-power-query-excel.xlsx?dl=1
Any Ideas?
Hi Andreas,
it worked perfectly fine for me.
Just had to refresh the data in the workbook you’ve sent.
/Imke
Hi Imke,
I’ve to apologize, I’ve tried other machines and it works perfectly as you said. Seems there is an issue with my machine. Thank you very much to clarify this issue.
Andreas.
No problem, you’re welcome!