Forum Discussion
Sort Matrix Columns by Values
- 1 year ago
Hi DatamirHub ,
I have create a work around based on some slicers and a calculation group but this can depend on the way you have your semantic model.
In my case I have the following table:
Add the following measures:
Cap Value = SUM('Data Values'[cap]) Rev Value = SUM('Data Values'[rev])I have also created the following two disconnected tables:
Then I created a calculation group with the following code:
Calculation item = VAR _measure = SELECTEDVALUE ( 'Measures filter'[Measure] ) VAR _year = SELECTEDVALUE ( 'Year filter'[year] ) VAR _temptable = CALCULATETABLE ( ADDCOLUMNS ( 'Data Values', "valuetotal", SWITCH ( _measure, "cap", [Cap Value], "rev", [Rev Value] ) ), 'Data Values'[year] = _year ) RETURN IF ( NOT ( ISINSCOPE ( 'Data Values'[year] ) ), SUMX ( _temptable, [valuetotal] ), SELECTEDMEASURE () )Now if you add the measures to your values and apply the calculation group you get the following result:
Has you can see the totals have in this case the values for the rev value 2023, if I changed it to 2024 it gets the inverse order because I'm doing the order by total:
It does not matter wich of the column of the total you select because they will be equal.
Then you can hide the totals column and it continues to sort in the same way:
Only question that I did not address here was the highest to lowest or lowest to highest not sure if you want it.
See pbix file attach.
Hi DatamirHub
Could you please try below steps:
- Unpivot your data in Power Query so that “Year” and “Measure (Rev/CAP)” become rows instead of columns. Then you can build a table visual and users will be able to click and sort by any Year/Measure combination.
- Or, create a separate table visual for each Year if column-level sorting is critical.
- If the requirement is interactive sorting, another option is to build a field parameter or a slicer that lets the user choose which Year/Metric they want to sort by, and then apply that dynamically to the Matrix/Table.