Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Joris_NL
Helper II
Helper II

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 sorting column*   (The desired sorting result, based on custom sorting column ) 
1001 first position
2003 third
1002 second
3004 fourth
E4006 sixth
F3005 fifth

 

I managed to make the output for the custom sorting column:

 

 

My custom sorting column = 
RANKX ( 'Table' ; 'Table'[NAME] ; ; ASC )
+
( 1000 * 'Table'[CATEGORY Nr.] )

 

 

 

But I can't use this column to sort NAME! This gives a circular dependancy error. So the order of the names is used for the RANKX function, which is then used to order the names. 

 

Any suggestions?

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

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"

 

 

 

Jihwan_Kim_2-1718645796502.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

2 REPLIES 2
Joris_NL
Helper II
Helper II

Wow thank you, that must help a lot of people. Unfortunately I'm working with a calculated table. But I'll try to add the column to the original source table and get back to you.

Jihwan_Kim
Super User
Super User

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"

 

 

 

Jihwan_Kim_2-1718645796502.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.