Daily DAX : Day 390 ORDERBY
Power BI DAX: ORDERBY Function
Overview
The ORDERBY function is used in DAX (Data Analysis Expressions) to define the sort order of columns in a table when creating calculated tables or modifying table structures.
Note:
ORDERBY is not a standalone function you use in measures or regular calculations. It is used inside table functions like SUMMARIZECOLUMNS to control sorting.
Syntax
ORDERBY ( <expression> [, ASC|DESC] [, <expression> [, ASC|DESC] ]... )
<expression>: A column or expression to sort by.ASC(default): Sort in ascending order.DESC: Sort in descending order.
Use Case Example
Suppose you want to create a summary table of Total Sales by Product Category, sorted by Sales descending, then by Category Name ascending.
Sample DAX Code
Sales Summary =
SUMMARIZECOLUMNS(
Products[Category],
"Total Sales", SUM(Sales[Amount]),
ORDERBY("Total Sales", DESC, Products[Category], ASC)
)
Result
The resulting table will show:
- Highest-selling categories first
- Within same sales amount, categories sorted alphabetically
Key Points
- Only works inside
SUMMARIZECOLUMNS(or other table functions that support it). - Multiple sort levels are allowed.
- Improves report performance and readability when pre-sorting data.
- Does not affect visual-level sorting — use visual settings for that.
When to Use ORDERBY?
Use it when:
- Building calculated tables for dashboards
- Pre-sorting data for consistent output
- Combining with
TOPNor ranking logic
Pro Tip: Use
ORDERBY in calculated tables to avoid relying on visual-level sorting, ensuring consistent results across reports.
Comments
Post a Comment