Forum Discussion

ngct1112's avatar
ngct1112
Post Patron
4 years ago
Solved

Pivot columns with error

Hi All,   I am trying to pivot the the "column" but it shows error as "Expression.Error: There were too many elements in the enumeration to complete the operation.",   let Source = Table.Fro...
  • Fowmy's avatar
    4 years ago

    ngct1112 

    You cannot Pivot as you have a duplicate of B for ID 1. You need to do some additional steps before performing Pivot.
    Create a blank Query, go to the Advanced Editor, clear the existing code, and paste the codes given below and follow the steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nRR0lFyzs8pzc0DMsISc0pTlWJ1opUMgTxHEI4pNTAwMi9WQJVyAmIjFJ4xmGeEW5sRVKGhJZhrjENlLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}}),
        #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
        #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", Int64.Type}, {"Column", type text}, {"Value", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"ID", "Column"}, {{"Count", each Table.RowCount(_), Int64.Type}, {"All", each _, type table [ID=nullable number, Column=nullable text, Value=nullable text]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All],"Index",1)),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"ID", "Column", "Value", "Index"}, {"ID", "Column", "Value", "Index"}),
        #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Custom", each if [Index] = 1 then [Column] else [Column] & Number.ToText([Index] )),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Column", "Index"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Custom]), "Custom", "Value")
    in
        #"Pivoted Column"