简体   繁体   中英

How can I convert Table to new rows and combine multiplie columns into one?

I have got stuck on this for a while, i know how to make rows with columns but not how to create a combo of more into one. =) This is the data

Id  M1  M1Comments  M2  M2Comments  M3  M3Comments
1   Yes "Blabla"    Yes "Blabla"    No  "Blabla"
2   Yes "Blabla"    No  "Blabla"    No  "Blabla"
3   No  "Blabla"    No  "Blabla"    No  "Blabla"

And the end result should look like this

Id  M   Value   Comments
1   M1  Yes "Blabla"
1   M2  Yes "Blabla"
1   M2  No  "Blabla"
2   M1  Yes "Blabla"
2   M2  No  "Blabla"
2   M3  No  "Blabla"
3   M1  No  "Blabla"
3   M2  No  "Blabla"
3   M3  No  "Blabla"
  1. Unpivot everything except Id .
  2. Split the Attribute column.
  3. Pivot on the second column from the split.
  4. Rename columns as desired.
let
    Source = < Your Data Soucre Here >,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Id"}, "Attribute", "Value"),
    #"Split Column by Position" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByPositions({0, 2}, false), {"M", "Comment"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Position", List.Distinct(#"Split Column by Position"[Comment]), "Comment", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"", "Value"}})
in
    #"Renamed Columns"

You can try this below Advance Editor code for table to achieve your desired output-

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUYpMLQaSyfm5ual5JQqGCrlYBY2AXL98VDFjpVidaCUjDOVGEDNQlBthMcIIZoQxuowxFhOMsZhgDDYhFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, M1 = _t, M1Comments = _t, M2 = _t, M2Comments = _t, M3 = _t, M3Comments = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"M1", type text}, {"M1Comments", type text}, {"M2", type text}, {"M2Comments", type text}, {"M3", type text}, {"M3Comments", type text}}),
    
    // New steps started from here
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Id"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if Text.End([Attribute],8) = "Comments" then [Value] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each not Text.Contains([Attribute], "Comments")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"Attribute", "M"}, {"Custom", "Comments"}})
in
    #"Renamed Columns"

Input-

在此处输入图像描述

Output-

在此处输入图像描述

Condition Comments column name has to contain text Comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM