Forum Discussion
Sorting by 2 columns
Hi all,
I want to sort by table in Power BI using 2 columns. To help explain what I am trying to achieve, what I have is the blue table. I want to sort by the product table first alphabetically. Essentially, all the apples will be together and so on. Within this grouping, I want to sort by decreasing order of volume. My output should look like the green table. I tried the following link but the it does not get me to the required output.
Thanks,
Jay
Ok Jay_Arora I would personally do something this:
- Create a Volume Sum measure
Volume Sum = SUM ( Sales[Volume] )
- Create a ranking measure
Combined Rank = VAR ProductRank = RANKX ( ALL ( Sales[Product] ), CALCULATE ( SELECTEDVALUE ( Sales[Product] ) ), , ASC ) VAR CityRank = RANKX ( ALL ( Sales[City] ), [Volume Sum] ) VAR CityCount = CALCULATE ( DISTINCTCOUNT ( Sales[City] ), ALL ( Sales ) ) VAR CombinedRank = ProductRank + CityRank / ( CityCount + 1 ) RETURN CombinedRank - Create a Table visual with Product, City, Volume Sum, Combined Rank, and sort by Combined Rank ascending
Turn off Word Wrap for Column Headers and Values
- Narrow the Combined Rank column until it's invisible.
Note: Creating a measure for ranking allows for the possibility that you might filter your report in the future. Otherwise, you could created a similar calculated column and sort by that instead.
Regards,
Owen
- Create a Volume Sum measure
6 Replies
- OwenAugerSuper User
Hi Jay_Arora
Just clarifying, are you wanting to sort the table in the data model, or sort a table visual in your report page (which could be subject to filters on Product or City)?
In either case, an approach similar to the post you linked to is possible, just the exact syntax may be a little different.
Regards,
Owen
- OwenAugerSuper User
Ok Jay_Arora I would personally do something this:
- Create a Volume Sum measure
Volume Sum = SUM ( Sales[Volume] )
- Create a ranking measure
Combined Rank = VAR ProductRank = RANKX ( ALL ( Sales[Product] ), CALCULATE ( SELECTEDVALUE ( Sales[Product] ) ), , ASC ) VAR CityRank = RANKX ( ALL ( Sales[City] ), [Volume Sum] ) VAR CityCount = CALCULATE ( DISTINCTCOUNT ( Sales[City] ), ALL ( Sales ) ) VAR CombinedRank = ProductRank + CityRank / ( CityCount + 1 ) RETURN CombinedRank - Create a Table visual with Product, City, Volume Sum, Combined Rank, and sort by Combined Rank ascending
Turn off Word Wrap for Column Headers and Values
- Narrow the Combined Rank column until it's invisible.
Note: Creating a measure for ranking allows for the possibility that you might filter your report in the future. Otherwise, you could created a similar calculated column and sort by that instead.
Regards,
Owen
- Create a Volume Sum measure