Forum Discussion

afaherty's avatar
afaherty
Icon for Helper V rankHelper V
2 days ago

Why does a sorting column affect DAX measures?

I have had this issue happen to me many times over the past few years and I don't understand it, so I was hoping someone else might!

Let's say I have a table of student grades:

StudentIDTestDescriptionOrder Sort - From Sort Order Table
1Test 1Average2
1Test 2Average2
1Test 2Average2
2Test 1Above Average1
2Test 2Average2
2Test 3Below Average3
3Test 1Average2
3Test 2Below Average3
3Test 3Below Average3
4Test 1Average2
4Test 2Average2
4Test 3Average2
5Test 1Above Average1
5Test 2Above Average1
5Test 3Above Average1

There is a relationship between the table above and this "Sort Order" table below:

DescriptionOrder Sort
Above Average1
Average2
Below Average3

Next, I have some measures that calculate percentages that do not involve the "Order Sort" column at all. I have charts and matrices that utilize these measures. Everything looks great. However, if I then go into the data view of the first table, select the "Description" column, and sort by the "Order Sort" column, all of my percentages in my charts/matrices change, and they are all incorrect.

I am going crazy and have given up on trying to sort since it ruins my measures!

3 Replies

  • ShivekMaharaj's avatar
    ShivekMaharaj
    Icon for Community Champion rankCommunity Champion

    Hi afaherty​,

    Yes, this can happen, and the reason is that Sort by Column is not purely cosmetic.

    When you set:

    Description
    -> Sort by
    -> Order Sort

    Power BI can include both Description and Order Sort in the internal query used by the visual.

    So even if your measure does not explicitly reference Order Sort, that column can still become part of the filter/grouping context in which the measure is evaluated.

    This becomes especially noticeable with percentage measures that do something like:

    ALL ( Table[Description] )

    or

    REMOVEFILTERS ( Table[Description] )

    because that only removes the filter from Description.

    The filter on Order Sort can still remain.

    A common fix is to clear both columns together, for example:

    CALCULATE (
        [Your Measure],
        REMOVEFILTERS (
            'Grades'[Description],
            'Grades'[Order Sort]
        )
    )

    or, if appropriate for the calculation:

    CALCULATE (
        [Your Measure],
        ALL (
            'Grades'[Description],
            'Grades'[Order Sort]
        )
    )

    This is a known DAX pattern with Sort by Column. SQLBI has a good explanation of the issue in this article on Sort by Column and filter removal.

    Microsoft’s Sort by Column documentation also confirms that the display column and sort column are tied together and must exist at the same granularity.

    So I would not remove the sorting itself.

    Instead, review any measure where you intentionally remove the Description filter and make sure you also remove the corresponding Order Sort filter when that is what the calculation logically requires.

    That is why the numbers appear to change even though Order Sort is never referenced directly in the measure.

    AI-assisted drafting: AI was used to help structure and phrase this response. I reviewed and validated the technical content before posting.

  • Hi, 

    when the description is not sorted by the sort order column, and when run the visualization's dax query view, 

    the DAX query does not contain sort order column, so the measure does not need to include sort order column.


    However, when the description column is sorted by the sort order column, then you query it,

    then, the query contains sort order column, even the visual does not show sort order column, so in this case, measure need to contain this sort order column as well.

     

  • littlemojopuppy's avatar
    littlemojopuppy
    Icon for Community Champion rankCommunity Champion

    afaherty​ can you provide a PBIX?  Difficult to offer any insight without seeing some data and understanding the DAX in the measures...