Forum Discussion
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:
| StudentID | Test | Description | Order Sort - From Sort Order Table |
| 1 | Test 1 | Average | 2 |
| 1 | Test 2 | Average | 2 |
| 1 | Test 2 | Average | 2 |
| 2 | Test 1 | Above Average | 1 |
| 2 | Test 2 | Average | 2 |
| 2 | Test 3 | Below Average | 3 |
| 3 | Test 1 | Average | 2 |
| 3 | Test 2 | Below Average | 3 |
| 3 | Test 3 | Below Average | 3 |
| 4 | Test 1 | Average | 2 |
| 4 | Test 2 | Average | 2 |
| 4 | Test 3 | Average | 2 |
| 5 | Test 1 | Above Average | 1 |
| 5 | Test 2 | Above Average | 1 |
| 5 | Test 3 | Above Average | 1 |
There is a relationship between the table above and this "Sort Order" table below:
| Description | Order Sort |
| Above Average | 1 |
| Average | 2 |
| Below Average | 3 |
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!
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.
7 Replies
- v-abhinavmu
Community Support
Hi afaherty,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.Thank you.
- littlemojopuppy
Community Champion
afaherty can you provide a PBIX? Difficult to offer any insight without seeing some data and understanding the DAX in the measures...
- Jihwan_Kim
Super User
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.
- ShivekMaharaj
Community 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 SortPower 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.
- maruthisp
Super User
Hi afaherty,
Yes, Sort by column can affect the filter context used by a Power BI visual. Power BI internally needs both Description and Order Sort in the query so it can display the descriptions in the required order. So if your percentage measure removes only the filter from Description.
Please find the attached pbix file with a solution for your scenario. Please let me know if you hae any further questions. Thanks.
Best Regards,
Maruthi
LinkedIn - http://www.linkedin.com/in/maruthi-siva-prasad/
X - Maruthi Siva Prasad - (@MaruthiSP) / X - Shubham_rai955
Super User
Please check below points:
- Check the relationship. Make sure Description in your main table has a valid relationship to the Sort Order table and that the relationship cardinality and filter direction are correct.
- Check the measure's filter context. If your measure uses ALL, ALLEXCEPT, REMOVEFILTERS, or DISTINCTCOUNT, sorting may reveal an existing filter-context issue.
- Check for duplicate descriptions. Each description must map to exactly one sort value. For example, Average should always map to 2.
- Test with a separate dimension table. Create a dimension containing one row per description and its sort order. Sort the dimension's Description column by its Order Sort column, then use the dimension's description in your visuals.
- v-abhinavmu
Community Support
Hi afaherty,
May I check if this issue has been resolved? If not, Please feel free to contact us if you have any further questions.
Thank you