Forum Discussion

DatamirHub's avatar
DatamirHub
Icon for Advocate I rankAdvocate I
1 year ago
Solved

Sort Matrix Columns by Values

I have this matrix view   I want my users to have the ability to sort by any column they want (e.g. rev of 2024 or 2023, or cap of 2024, ... etc) I cant click on the columns to sort whe...
  • MFelix's avatar
    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.