Forum Discussion

Joris_NL's avatar
Joris_NL
Helper III
2 years ago
Solved

Custom sorting for calculated table (circular error)

Hi,   I have a calculated table, in which I want the NAME column (all unique) to sort by CATEGORY first, and then sorted alphabetically second. Like this: NAME    CATEGORY Nr.     *My custom ...
  • Jihwan_Kim's avatar
    2 years ago

    Hi,

    If possible, one of ways is to create sort column in power query editor.
    Please check the below picture and the attached pbix file.

     

    let
        Source = source,
        #"Sorted Rows" = Table.Sort(Source,{{"CATEGORY Nr.", Order.Ascending}, {"NAME", Order.Ascending}}),
        #"Grouped Rows" = Table.Group(#"Sorted Rows", {"CATEGORY Nr."}, {{"Count", each Table.AddIndexColumn( _, "index",1,1)}}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"NAME", "index"}, {"NAME", "index"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Count", "sort_order", each [#"CATEGORY Nr."]*1000 + [index]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"index"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"NAME", type text}, {"sort_order", Int64.Type}})
    in
        #"Changed Type"