Forum Discussion
TaylorLile
6 years agoFrequent Visitor
Unpivot Two Groupings of Columns to Rows
Hello, I am struggling with taking two groups of columns and converting them into only two columns. I have a decent understanding of unpivoting columns, but I'm not sure how to handle this when I...
- 6 years ago
Hi,
here is the used M-Code:
// Table let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCgkKdVXSgVFujj7BINoEiE2B2ACIDZVidaLRFcBoU7ACiAYjsEKYDJoGkEJjqGJjpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Diabetes = _t, Hypertension = _t, Depression = _t, #"Diabetes Lab" = _t, #"Hypertension Lab" = _t, #"Depression Lab" = _t, ID = _t]), #"Reordered Columns" = Table.ReorderColumns(Source,{"ID", "Diabetes", "Hypertension", "Depression", "Diabetes Lab", "Hypertension Lab", "Depression Lab"}), #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"Diabetes", type logical}, {"Hypertension", type logical}, {"Depression", type logical}, {"Diabetes Lab", Int64.Type}, {"Hypertension Lab", Int64.Type}, {"Depression Lab", Int64.Type}}), #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"), #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Attribute.1", "Attribute.2"}) in #"Split Column by Delimiter" // Lab Value (Reference of Table) let Source = Table, #"Filtered Rows" = Table.SelectRows(Source, each ([Attribute.2] = "Lab")), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.2"}) in #"Removed Columns" // Diagnosis (Reference of Table) let Source = Table, #"Filtered Rows" = Table.SelectRows(Source, each ([Attribute.2] = null)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.2"}) in #"Removed Columns" // Merged Lab Value & Diagnosis let Source = Table.NestedJoin(#"Lab Value (Reference of Table)", {"ID", "Attribute.1"}, #"Diagnosis (Reference of Table)", {"ID", "Attribute.1"}, "Diagnosis", JoinKind.LeftOuter), #"Expanded Diagnosis" = Table.ExpandTableColumn(Source, "Diagnosis", {"Value"}, {"Value.1"}), #"Renamed Columns" = Table.RenameColumns(#"Expanded Diagnosis",{{"Attribute.1", "Condition"}, {"Value.1", "Diagnosis"}, {"Value", "Lab Value"}}), #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"ID", "Condition", "Diagnosis", "Lab Value"}), #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"ID", Int64.Type}, {"Condition", type text}, {"Diagnosis", type logical}, {"Lab Value", Int64.Type}}) in #"Changed Type"Figures:
Regards FrankAT
Ashish_Mathur
Super User
6 years agoHi,
This M code works
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Diabetes", type logical}, {"Hypertension", type logical}, {"Depression", type logical}, {"Diabetes Lab", Int64.Type}, {"Hypertension Lab", Int64.Type}, {"Depression Lab", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
#"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter",null,"Diagnosis",Replacer.ReplaceValue,{"Attribute.2"}),
#"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Attribute.2]), "Attribute.2", "Value")
in
#"Pivoted Column"
Hope this helps.
- FrankAT6 years ago
Community Champion
Hi Ashish_Mathur,
thx, I learned something again about pivoting data. I think your solution is the most efficient.
Regards Frank AT
- Ashish_Mathur6 years ago
Super User
Thank you. If my reply helped, please mark it as Answer.