Forum Discussion
Merleau
4 years agoHelper II
Pivoting a column and merging linked fields with count
Hello, I have a table with multiple columns (more than 30) in no particular order. I need to create multiple columns by pivoting one of the columns. Then populate the resulting columns with associat...
- 4 years ago
This isn't especially elegant but I think it works:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nRR0lEKqSxIBVIBRfnpRYm5xUCmX2JuqlKsDkjeEMh19AUSrnnpOZnFGWA2UFg3D6HGCKbGrSg1LxmmxAhZiTHIApC4Y1FiUmZyAESJMbISQ5iS4ILEPKBNAThsAos75xelJuYFYLEJ7mDPksSczMQ8vA5G9RR2F0PNweVkTI8boivxA4m7F6WmZvuhqogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]), #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Grouped Rows" = Table.Group(#"Promoted Headers", {"ID", "Name", "Type"}, {{"Combine", each Text.Combine([Programs], ", "), type nullable text}, {"Count", each Table.RowCount(_), type text}}), #"Removed Count" = Table.RemoveColumns(#"Grouped Rows",{"Count"}), #"Removed Combine" = Table.RemoveColumns(#"Grouped Rows",{"Combine"}), #"Add Count Text" = Table.TransformColumns(#"Removed Combine",{{"Type", each "Count" & _, type text}}), #"Renamed Columns" = Table.RenameColumns(#"Add Count Text",{{"Count", "Combine"}}), #"Appended Query" = Table.Combine({#"Removed Count", #"Renamed Columns"}), #"Pivoted Column" = Table.Pivot(#"Appended Query", List.Distinct(#"Appended Query"[Type]), "Type", "Combine"), #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"ID", "Name", "AM", "CountAM", "PM", "CountPM", "NM", "CountNM"}) in #"Reordered Columns"
Merleau
4 years agoHelper II
Thank you Alexis, Your approach definitely appears to work and it is very clever. How to sort the list in this structure? This requirement is not in your solution. Thanks.
- AlexisOlson4 years agoSuper User
In the #"Grouped Rows" step, replace Text.Combine([Program]) with Text.Combine(List.Sort([Program])).