How to Sort Tables in DAX Using ORDER BY
In this article, we will learn how to sort a table in DAX using
ORDER BY. We will retrieve the complete Sales table,
sort it by a single column, and sort it using multiple columns.
Practice Data:
Download Practice Data Free
Prefer learning through video?
DAX YouTube Playlist
What Is ORDER BY in DAX?
ORDER BY is used to sort the result of a DAX query.
It specifies the column or columns that determine the order of the returned rows.
Important: ORDER BY is used with a
DAX query to sort its result. It does not change the
original Sales table.
Basic Syntax
EVALUATE
<Table Expression>
ORDER BY
<Column> [ASC | DESC]
By default, ASC means ascending order.
DESC means descending order.
1. Retrieving the Sales Table
Query
// Get Sales Table
EVALUATE Sales
Step-by-step explanation
Step 1: EVALUATE
EVALUATE tells DAX to return a table as the query result.
Step 2: Sales
Sales is the table expression.
DAX returns the rows and columns available in that table.
What will you get?
The result contains the Sales table without applying any sorting instruction
in the query.
| ResellerKey | ProductKey | Sales Amount |
|---|---|---|
| 3 | 100 | 1500 |
| 1 | 200 | 2500 |
| 2 | 150 | 1800 |
The example above is only for illustration.
Your actual result will contain the data available in your Sales table.
EVALUATE Sales retrieves the table.
To control the order of the returned rows, we use ORDER BY.
2. Sorting by a Single Column
Query
// Sort by a Column in Descending Order
EVALUATE
Sales
ORDER BY
Sales[ResellerKey] DESC
Step-by-step explanation
Step 1: EVALUATE Sales
Returns the Sales table as the query result.
Step 2: ORDER BY
Tells DAX that we want to sort the returned rows.
Step 3: Sales[ResellerKey]
Specifies the column that will be used for sorting.
Step 4: DESC
Sorts the values in descending order.
For a numeric column, the largest value appears first.
What will you get?
The Sales table will be sorted from the highest
ResellerKey to the lowest.
| ResellerKey | ProductKey | Sales Amount |
|---|---|---|
| 3 | 100 | 1500 |
| 2 | 150 | 1800 |
| 1 | 200 | 2500 |
The example above is only for illustration.
The actual values and row order depend on your Sales table.
Ascending vs Descending Order
| Keyword | Meaning | Example |
|---|---|---|
| ASC | Ascending order | 1, 2, 3, 4 |
| DESC | Descending order | 4, 3, 2, 1 |
Use ASC for ascending order and
DESC for descending order.
3. Sorting by Multiple Columns
Query
// Sort by Multiple Columns in an Order
EVALUATE
Sales
ORDER BY
Sales[ResellerKey] DESC,
Sales[ProductKey] DESC
Step-by-step explanation
Step 1: EVALUATE Sales
Returns the Sales table as the query result.
Step 2: ORDER BY Sales[ResellerKey] DESC
Sorts the rows by ResellerKey in descending order.
This is the primary sorting column.
Step 3: Sales[ProductKey] DESC
Sorts by ProductKey in descending order
when the ResellerKey values are the same.
This is the secondary sorting column.
How does multiple-column sorting work?
DAX first sorts the rows using the first column.
If two or more rows have the same value in that column,
DAX uses the next column to determine their order.
In this query, the sorting priority is:
- ResellerKey — descending order
- ProductKey — descending order when ResellerKey is the same
What will you get?
The result will be sorted by ResellerKey first.
Within each reseller, the ProductKey values will be sorted
in descending order.
| ResellerKey | ProductKey | Sales Amount |
|---|---|---|
| 3 | 200 | 2200 |
| 3 | 100 | 1500 |
| 2 | 250 | 3000 |
| 2 | 150 | 1800 |
| 1 | 200 | 2500 |
The example above is only for illustration.
The actual values and row order depend on your Sales table.
When using multiple columns in ORDER BY,
the first column has the highest sorting priority.
Understanding the Difference
| Query | Sorting behavior |
|---|---|
| EVALUATE Sales | Returns the Sales table without a sorting instruction |
| EVALUATE Sales ORDER BY Sales[ResellerKey] DESC | Sorts by ResellerKey in descending order |
| EVALUATE Sales ORDER BY Sales[ResellerKey] DESC, Sales[ProductKey] DESC | Sorts by ResellerKey first, then ProductKey within matching ResellerKey values |
Important Notes
1. ORDER BY controls the query result
It determines the order of rows returned by the DAX query.
2. Multiple columns have a sorting priority
The first column is the primary sort.
The next column is used when the previous column contains matching values.
3. Each column can have its own sort direction
ORDER BY
Sales[ResellerKey] ASC,
Sales[ProductKey] DESC
This sorts ResellerKey in ascending order,
while ProductKey is sorted in descending order
within each reseller.
4. Sorting does not modify the original table
The query returns a sorted result.
It does not permanently rearrange the rows in the Sales table.
📝 Practice Exercise
Try writing a query to retrieve the Sales table
and sort it using the following rules:
- Sort ResellerKey in ascending order.
- Sort ProductKey in descending order.
Expected Query
EVALUATE
Sales
ORDER BY
Sales[ResellerKey] ASC,
Sales[ProductKey] DESC
Conclusion
In this article, we learned how to sort tables in DAX using
ORDER BY.
- EVALUATE Sales retrieves the Sales table.
- ORDER BY sorts the query result.
- ASC sorts values in ascending order.
- DESC sorts values in descending order.
- Multiple columns can be used to create a sorting priority.
- Each sorting column can have its own sort direction.
These concepts are essential for writing DAX queries and understanding
how data is displayed in tools such as DAX Studio.
Next: We can learn how to use
START AT to begin displaying query results
from a specific value.
📥 Practice Data:
Download Practice Data Free
🎥 Prefer learning through video?
DAX YouTube Playlist