Forum Discussion

VijayRbn's avatar
VijayRbn
New Member
1 year ago
Solved

Pivoting the table data in Modeling

Hi, I am unable to get the below output using Pivot table option. Table. ID Attrib Value 82 Section A 82 Section B 82 Class 2 83 Section A 83 Class 3   O...
  • BeaBF's avatar
    1 year ago

    VijayRbn Hi! Here the M code for the advanced editor:

    let
    // Load the original data
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjBS0lEKTk0uyczPA7IclWJ1MASdEILOOYnFxUDaCCJkjE2zMZI6Y6XYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Attrib = _t, Value = _t]),

    // Group the data by ID and Attrib to handle multiple rows for the same key
    GroupedTable = Table.Group(
    Source,
    {"ID", "Attrib"},
    {{"Values", each Text.Combine([Value], ","), type text}}
    ),

    // Pivot the grouped table based on the 'Attrib' column
    PivotedTable = Table.Pivot(
    GroupedTable,
    List.Distinct(GroupedTable[Attrib]), // Pivot by the unique values in 'Attrib' (i.e., 'Section' and 'Class')
    "Attrib",
    "Values"
    ),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(PivotedTable, {{"Section", Splitter.SplitTextByDelimiter(",", QuoteStyle.None), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section"),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ID", Int64.Type}, {"Section", type text}})
    in
    #"Changed Type"

     

    you'll achieve:

     

    if it's ok, please accept my answer as solution.

     

    BBF