Forum Discussion

mp390988's avatar
mp390988
Post Partisan
9 months ago
Solved

Trying to sort column headers

Hello,

 

I have a table that has 2 distinct values "Revenue" and "IB" in a column called FactMain[ColumnType].

When I create a matrix visual, I put the FactMain[ColumnType] in the columns section and I don't like it showing IB and then Revenue,

 

I would like Revenue first and then IB.

 

I tried creating a calculated column called "HeaderSort" with this formula: 

HeaderSort = IF(
    FactMain[ColumnType] = "Revenue",
    0,
    1
)

 

and then I try to apply the sort to the field ColumnType using HeaderSort but I get an error complaining about Circular Dependency as per below:

 

 

Any ideas on how I can achieve the sort? 

 

Thank You




 

  • mp390988 
    Instead of creating a calculated column in the reporting view. Create custom column on the power query layer (Transform Data layer) please refer the below M expression and add it as the last step of your transformation

    SortedColumn = = Table.AddColumn(PreviousStep, "HeaderSort", each if [ColumnType] = "Revenue" then 0 else 1)

    Use this custom column (HeaderSort) to sort the ColumnType Column, now you will not get circular dependency issues.

     

    Thanks,

    Jai

  • I had the same issue as the OP with a column that determines column header values, e.g. 

    1-Pear

    2-Apple

    3-Grapes

     

    I really didn't want to include the 1-, 2-, 3- in the column text results for sorting, but the column calculations are really heavyweight so I didn't want to dupe the column just to create a sort order output I could use to sory the original.  Thought about some of the other approaches that happen to be mentioned in this thread, e.g. a separate sort table accessed through a join, but they are too clunky for my taste.

     

    Went searching and happened to come across your suggestion in this thread.  It's brilliant, and works perfectly.    I was actually able to simplify it since I don't need a new colunn to do this.  I just added the zero-space character, repeated appropriately, to the text output of my original column.  e.g.

     

    REPT(UNICHAR(8203), 3,) & "Pear"

    REPT(UNICHAR(8203), 2) & "Apple"

    REPT(UNICHAR(8203), 1) & "Grapes"

     

    I can't mark this as a solution because this isn't my thread, but I wanted to tip my cap to you for the idea.  Hopefully someone else will come across this in the future and can use my post here as confirmation that this solution works well.  Thank you!

7 Replies

  • mp390988 
    Instead of creating a calculated column in the reporting view. Create custom column on the power query layer (Transform Data layer) please refer the below M expression and add it as the last step of your transformation

    SortedColumn = = Table.AddColumn(PreviousStep, "HeaderSort", each if [ColumnType] = "Revenue" then 0 else 1)

    Use this custom column (HeaderSort) to sort the ColumnType Column, now you will not get circular dependency issues.

     

    Thanks,

    Jai

    • markmsc's avatar
      markmsc
      Resolver I

      I had the same issue as the OP with a column that determines column header values, e.g. 

      1-Pear

      2-Apple

      3-Grapes

       

      I really didn't want to include the 1-, 2-, 3- in the column text results for sorting, but the column calculations are really heavyweight so I didn't want to dupe the column just to create a sort order output I could use to sory the original.  Thought about some of the other approaches that happen to be mentioned in this thread, e.g. a separate sort table accessed through a join, but they are too clunky for my taste.

       

      Went searching and happened to come across your suggestion in this thread.  It's brilliant, and works perfectly.    I was actually able to simplify it since I don't need a new colunn to do this.  I just added the zero-space character, repeated appropriately, to the text output of my original column.  e.g.

       

      REPT(UNICHAR(8203), 3,) & "Pear"

      REPT(UNICHAR(8203), 2) & "Apple"

      REPT(UNICHAR(8203), 1) & "Grapes"

       

      I can't mark this as a solution because this isn't my thread, but I wanted to tip my cap to you for the idea.  Hopefully someone else will come across this in the future and can use my post here as confirmation that this solution works well.  Thank you!

  • Hi, 

     

    A column cannot be sorted by another column that is created from itself, or if both derive from each other directly or indirectly.

     

    You may create a new table by usin 'Enter data'

                       

     

    Select 'ColumnType' in this new table, Sort by Column,  SortOrder.

     

    then create a relationship between this newly created table and the fact table. 

    FactMain[ColumnType] --  SortTable[ColumnType]

     

    In the Matrix Visual, use 'ColumnType' from the SortTable and sort the 'ColumnType' by Ascending. 

  • Hi mp390988 

     

    Create the sort by colum in the query editor as what Jai-Rathinavel has suggested. Creating a sort by caclculated column that references the column being sorted by  can cause circular dependency. 

  • v-prasare's avatar
    v-prasare
    Community Support

    Hi mp390988,

    As we haven't heard back, we would like to confirm if our community members answer resolves your query or if you need further help. If you still have any questions or need more support, please feel free to let us know. We are happy to help you.


    danextian, Ahmedx, Ashish_Mathur & Jai-Rathinavel ,Thanks for your prompt response

     

     

     

     

    Thank you for your patience and look forward to hearing from you.
    Best Regards,
    Prashanth Are
    MS Fabric community support