Forum Discussion
Unpivot or Transpose Header in row level into Column also values
- 1 year ago
One way to do this is to;
1) group by the first column choosing do not aggregate so you get all the rows
2) add an index column to the resulting nested tables
3) expand the nested tables
4) pivot the table on the first column, choosing not to aggregate the values in the second column
5) remove the index column
Here is an example code...let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"All", each Table.SelectColumns(_, {"Column2"}), type table [Column1=text, Column2=text]}}), Custom1 = Table.TransformColumns(#"Grouped Rows", {{"All", each Table.AddIndexColumn(_, "index", 1, 1)}}), #"Expanded All" = Table.ExpandTableColumn(Custom1, "All", {"Column2", "index"}, {"Column2", "index"}), #"Pivoted Column" = Table.Pivot(#"Expanded All", List.Distinct(#"Expanded All"[Column1]), "Column1", "Column2"), #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"index"}) in #"Removed Columns" - 1 year ago
Hi ssk_1984, another solution:
Before
After
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kvMTVXSUQouLUotzlCK1YlWCk7MSS0GChkagLlQBUGJWSiyRiiywaV5mTko8sZA+VgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]), Transformed = Table.FromRows(List.Split(Source[Column2], 2), {"Name", "Sales"}) in Transformed
Hi ssk_1984 , here's a quick way to solve your problem. I am attaching two images, first of the M code snippet used and second of the ouput. Thanks!
- ssk_19841 year agoHelper II
Thank you for your quick revert, one more query rather than only two column...in my table has more than two column...i can add for each by col3, col4,col5 ...like that....Suggest me pls
- SundarRaj1 year agoSuper User
Could you be a little more specific with regards to your query? I didn't quite understand it correctly
- SundarRaj1 year agoSuper User
Do you mean something like this?
If that's the case, then this particular idea should work. Do match it with your idea. If this is not what you were looking for, do provide a sample set of your data and the output that you'd want. Thanks