Forum Discussion
How to unstack a column into table using referncing index + pivot the table
- 5 years ago
Hi lcy0328
Place the following M code in a blank query to see the steps:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVbyMYRQRhDKGEKZQChTMAVVB1MIpY2htIkhiiK4KrgyuDo0hQiVCKUItVCWKZgVCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}}), #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Col1"}), list0_ = #"Replaced Value"[Col1], list1_ = if List.Last(list0_)<>null then list0_&{null} else list0_ = null, //Make sure list finishes with a null to make things easier in the next step list2_ = if List.First(list1_)=null then List.LastN(list1_,List.Count(list1_)-1) else list1_, list3_ = List.Accumulate(list2_,[total={}, partial={}],(s,c)=> if c<>null then [total=s[total], partial=s[partial]&{c}] else [total=s[total]&{s[partial]}, partial={}]), total = list3_[total], #"Converted to Table" = Table.FromList(total, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Text.Combine([Column1],"|")), #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Line1", "Line2", "Line3", "Line4", "Line5"}), #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Column1"}) in #"Removed Columns"Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
- 5 years ago
Hello lcy0328
try this approach. Uses Table.Group to split your tables on every "null", then skip and transpose every single column.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVbyMYRQRhDKGEKZQChTMAVVB1MIpY2htIkhiiK4KrgyuDo0hQiVCKUItVCWKZgVCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}}), #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Col1"}), #"Grouped Rows" = Table.Group(#"Replaced Value", {"Col1"}, {{"AllRows", each Table.Transpose(Table.Skip(_,1)), type table [Col1=text]}}, GroupKind.Local, (g,c)=> if c[Col1]=null then 1 else 0), #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"AllRows"}), #"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Other Columns", "AllRows", {"Column1","Column2","Column3","Column4","Column5"}) in #"Expanded AllRows"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
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
Hi lcy0328
Place the following M code in a blank query to see the steps:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVbyMYRQRhDKGEKZQChTMAVVB1MIpY2htIkhiiK4KrgyuDo0hQiVCKUItVCWKZgVCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Col1"}),
list0_ = #"Replaced Value"[Col1],
list1_ = if List.Last(list0_)<>null then list0_&{null} else list0_ = null, //Make sure list finishes with a null to make things easier in the next step
list2_ = if List.First(list1_)=null then List.LastN(list1_,List.Count(list1_)-1) else list1_,
list3_ = List.Accumulate(list2_,[total={}, partial={}],(s,c)=> if c<>null then [total=s[total], partial=s[partial]&{c}] else [total=s[total]&{s[partial]}, partial={}]),
total = list3_[total],
#"Converted to Table" = Table.FromList(total, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Added Custom" = Table.AddColumn(#"Converted to Table", "Custom", each Text.Combine([Column1],"|")),
#"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "Custom", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Line1", "Line2", "Line3", "Line4", "Line5"}),
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Column1"})
in
#"Removed Columns"
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers