Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Sorting row or column values in a specific order

Hi, this is a 2-part question.

 

Part #1:

I followed a previously published solution: https://community.powerbi.com/t5/Desktop/Sorting-rows-and-columns-in-matrix-visual/m-p/723838#M349347

to try to sort a field in a matrix row. The field name is "LOB" and the sort column field I created is "Sort_LOB" where:

Sort_LOB = IF([LOB] ="Accounts",1
,IF([LOB] ="ESC Accounts",2
,IF([LOB] ="Billing",3
,IF([LOB] ="ESC Billing",4
,IF([LOB] ="Tech",5
,IF([LOB] ="ESC Tech & Hardware",6))))))
However, when I then selected the field I want to sort, i.e. "LOB" and then went to "Sort by Column", then selected the new field "Sort_LOB" as the sort by field, nothing changed. Meaning, the row values were still sorted alphabetically as opposed to in the order I specified in the DAX for field name Sort_LOB. See screenshot. 
 

Part #2:

In the same matrix, I also display a column field with 2 values - Coaching and Pre-Coaching.

As you can see from the screenshot -

the 2 values are displayed in alphabetical order, "C" then "P".

But I really want it to display Pre-Coaching, then Coaching.

How do I do that? Do I also create a new field with sort order specified like in the DAX for LOB, which that one didn't seem to work in the visualization!

Thanks.

 

10 Replies

  • mjantune's avatar
    mjantune
    Regular Visitor

    Part #1:

     

    Is the "Sort_LOB" field a calculated column? If it is, you should use sort by column in the Data View 

     

     

     

     

    You select the "LOB" column and you sort by the Sort_LOB column.

    Instead of using IF, its better to use SWITCH.

     

    Sort_LOB = SWITCH( [LOB] ,
    "Accounts", 1 ,
    "ESC Accounts", 2 ,
    "Billing" , 3 ,
    "ESC Billing" , 4 ,
    "Tech" , 5 ,
    "ESC Tech & Hardware" , 6 ,
    7
    )
    --- 7 is for any other values

     

     

    Part 2:

    Here is a post explaining how to sort by custom orders.
    http://www.excelnaccess.com/custom-sorting-in-power-bi/

    You could use the same method as in the row order.

     

    I hope this helps.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Re. Part #1

       

      I changed the Sort_LOB calculated column to the SWITCH dax, then I selected LOB and sorted by Sort_LOB, and encountered and error message - screenshot attached.

      • mjantune's avatar
        mjantune
        Regular Visitor

        is [Lob] a measure or a Column?

        You could also create a new table (Sort_Lob) to do the sorting.

         

         

         

         

        LOBSort_LOB
        Accounts1
        Esc Accounts2
        Billing3
        Esc billing4
        Tech5
        Esc Tech & hardware6

         

        Then you relate this table to the Employee_Team table you want to sort, and use this Sort_Lob table in the report.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks, but I have one question though.

      LOB is from one table, and 'Type' is from a different table.

      That would mean I can't do conditional column, right?
      Or, if I can, how do I do that?
      Thank you!