Forum Discussion
Convert columns to rows with dynamic columns
I've tried to find the solution for this but didn't work so I post this message for your help.
I have this data:
| 1/1 | 1/2 | 1/3 | |
| A | 1 | 1 | 2 |
| B | 0 | 2 | 3 |
| C | 1 | 1 | null (ignore null or default to be 0 is fine) |
| 2/1 | 2/2 | null | |
| A | 1 | 1 | null |
What I'm looking for
| A | 1/1 | 1 |
| A | 1/2 | 1 |
| A | 1/3 | 2 |
| B | 1/1 | 0 |
| B | 1/2 | 2 |
| B | 1/3 | 3 |
| C | 1/1 | 1 |
| C | 1/2 | 1 |
| A | 2/1 | 1 |
| A | 2/2 | 1 |
😍I know that the data looks stupid becauseI have to deal with legacy from decade ago.
I would like to say thanks in advance.
Hallo huynq
you have to first split der your data into tables, promote headers, combine your tables and then do unpivotother
Here the complete solution
let Source = #table ( {"Column1","Column2","Column3","Column4"}, { {"","1/1","1/2","1/3"}, {"A","1","1","2"}, {"B","0","2","3"}, {"C","1","1",""}, {"","2/1","2/2",""}, {"A","1","1",""} } ), AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1), AddDevideTable = Table.AddColumn(AddIndex, "Devide Table", each if [Column1]="" then [Index] else null), FillDownDevideTable = Table.FillDown(AddDevideTable,{"Devide Table"}), GroupTables = Table.Group(FillDownDevideTable, {"Devide Table"}, {{"AllRows", each _, type table [Column1=text, Column2=text, Column3=text, Column4=text, Index=number, Devide Table=number]}}), MaintainTable = Table.TransformColumns ( GroupTables, { { "AllRows", (tableint) => let DeleteIndexDevideTable = Table.RemoveColumns(tableint, {"Index", "Devide Table"}), Promote = Table.PromoteHeaders(DeleteIndexDevideTable) in Promote } } ), Combine = Table.Combine ( MaintainTable[AllRows] ), UnpivotOther = Table.UnpivotOtherColumns(Combine, {""}, "Attribute", "Value"), FilterNonEmptyValue = Table.SelectRows(UnpivotOther, each ([Value] <> "")) in FilterNonEmptyValueCopy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
5 Replies
- Jimmy801Community Champion
Hallo huynq
you have to first split der your data into tables, promote headers, combine your tables and then do unpivotother
Here the complete solution
let Source = #table ( {"Column1","Column2","Column3","Column4"}, { {"","1/1","1/2","1/3"}, {"A","1","1","2"}, {"B","0","2","3"}, {"C","1","1",""}, {"","2/1","2/2",""}, {"A","1","1",""} } ), AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1), AddDevideTable = Table.AddColumn(AddIndex, "Devide Table", each if [Column1]="" then [Index] else null), FillDownDevideTable = Table.FillDown(AddDevideTable,{"Devide Table"}), GroupTables = Table.Group(FillDownDevideTable, {"Devide Table"}, {{"AllRows", each _, type table [Column1=text, Column2=text, Column3=text, Column4=text, Index=number, Devide Table=number]}}), MaintainTable = Table.TransformColumns ( GroupTables, { { "AllRows", (tableint) => let DeleteIndexDevideTable = Table.RemoveColumns(tableint, {"Index", "Devide Table"}), Promote = Table.PromoteHeaders(DeleteIndexDevideTable) in Promote } } ), Combine = Table.Combine ( MaintainTable[AllRows] ), UnpivotOther = Table.UnpivotOtherColumns(Combine, {""}, "Attribute", "Value"), FilterNonEmptyValue = Table.SelectRows(UnpivotOther, each ([Value] <> "")) in FilterNonEmptyValueCopy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - AnonymousNot applicable
I believe the function you are looking for is "Unpivot Other Columns". You'll find it in the ribbon under "Transform"
Go into your query editor, select only the first column (that contains A, B, C etc). Select unpivot other columns and you should be good to go.
- AnonymousNot applicable
Try using "Unpivot Other Columns" under "Transform" options in Power BI.