DAX Function Documents (1)
DAX Function Documents (1)
SSAS Tabular models. It is crucial for creating custom calculations, aggregations, and complex
business logic within your Power BI reports and dashboards. DAX functions and their real-time
usage scenarios are diverse. Below are some of the most commonly used DAX functions, their
types, and real-time scenarios for using them:
1. Basic Functions
2. Logical Functions
IF(): Evaluates a condition and returns one value if true, another if false.
o Usage: Conditional flagging (e.g., "Above Target", "Below Target").
o Example: Target Status = IF(Sales[Amount] > 5000, "Above Target",
"Below Target")
SWITCH(): Evaluates an expression against a list of values and returns a result based on
the match.
o Usage: Multiple condition checks, such as categorizing scores or sales
performance.
o Example: Performance = SWITCH(TRUE(), Sales[Amount] > 5000,
"Excellent", Sales[Amount] > 2000, "Good", "Needs Improvement")
4. Filter Functions
CALCULATE(): Changes the context of a calculation by applying filters.
o Usage: Filtering sales within specific regions, periods, or conditions.
o Example: Sales in Region A = CALCULATE(SUM(Sales[SalesAmount]),
Sales[Region] = "A")
FILTER(): Returns a table filtered by a given expression.
o Usage: Filtering data based on complex conditions, like filtering high-value
transactions.
o Example: High Sales = FILTER(Sales, Sales[SalesAmount] > 10000)
5. Statistical Functions
6. Text Functions
7. Mathematical Functions
8. Relationship Functions
1. CALCULATE()
Description:
The CALCULATE() function is one of the most powerful and frequently used DAX functions. It
modifies the filter context of a calculation. It allows you to change the context of your
calculation, adding new filters or modifying existing ones.
Real-time Scenario:
Let’s say you want to calculate sales for a specific region or product while ignoring other filters
in your report.
Example:
You have a dataset with sales data, and you want to calculate the total sales for the "East" region,
regardless of the filters applied on the report.
DAX
CopyEdit
Sales in East Region =
CALCULATE(
SUM(Sales[SalesAmount]),
Sales[Region] = "East"
)
In this case, CALCULATE() forces the calculation of the total sales for the East region, even if
there are filters applied to the report that might restrict this data.
2. FILTER()
Description:
The FILTER() function returns a table that has been filtered based on a specific condition. It’s
often used in conjunction with CALCULATE() to modify the filter context of a calculation.
Real-time Scenario:
Suppose you want to calculate the total sales for products where the sales amount is greater than
$1000. You can use FILTER() to isolate the sales data meeting this condition.
Example:
DAX
CopyEdit
High Sales =
CALCULATE(
SUM(Sales[SalesAmount]),
FILTER(Sales, Sales[SalesAmount] > 1000)
)
In this example, FILTER(Sales, Sales[SalesAmount] > 1000) returns a filtered table where
only sales greater than $1000 are included, and CALCULATE() then sums those values.
Description:
ALL() removes all filters from a column or table, allowing you to calculate totals
regardless of any slicers or filters applied in your report.
ALLEXCEPT() removes all filters except for the specified columns, which can be useful
when you want to preserve certain filter contexts.
Real-time Scenario:
You want to calculate the total sales across all regions, ignoring any regional filters. ALL() will
remove any filter on the "Region" column.
Example:
DAX
CopyEdit
Total Sales All Regions =
CALCULATE(
SUM(Sales[SalesAmount]),
ALL(Sales[Region])
)
If you wanted to keep the filter context for product categories but ignore the region filter, you
would use ALLEXCEPT().
DAX
CopyEdit
Sales by Category (Ignoring Region Filter) =
CALCULATE(
SUM(Sales[SalesAmount]),
ALLEXCEPT(Sales, Sales[ProductCategory])
)
This will ignore the region filter but keep the category context.
4. RANKX()
Description:
RANKX() ranks the values in a column based on a specified expression. It can be used to rank
products, salespeople, or regions based on their performance.
Real-time Scenario:
You want to rank products based on their total sales and show the rank of each product in the
report.
Example:
DAX
CopyEdit
Product Rank =
RANKX(
ALL(Products),
SUM(Sales[SalesAmount]),
,
DESC
)
Here, RANKX() ranks products based on the total sales in descending order (highest sales first).
ALL(Products) ensures that the ranking ignores any product filters on the report.
5. SAMEPERIODLASTYEAR()
Description:
SAMEPERIODLASTYEAR() shifts the date context back by one year, making it easy to compare
current data to the same period in the previous year.
Real-time Scenario:
You want to compare this year's sales to last year's sales for the same month or quarter.
Example:
DAX
CopyEdit
Sales Last Year =
CALCULATE(
SUM(Sales[SalesAmount]),
SAMEPERIODLASTYEAR(Dates[Date])
)
This will calculate the total sales for the same period in the previous year based on the current
date context (e.g., if you have a filter for April 2025, it will compare it to April 2024).
6. DIVIDE()
Description:
DIVIDE() is a safer alternative to the standard division operator (/) because it handles division
by zero by returning a specified value (e.g., blank or 0) instead of an error.
Real-time Scenario:
You want to calculate the profit margin for each product but ensure that you don’t get errors
when dividing by zero.
Example:
DAX
CopyEdit
Profit Margin =
DIVIDE(
SUM(Sales[Profit]),
SUM(Sales[SalesAmount]),
0
)
In this case, if SalesAmount is zero for any product, DIVIDE() will return 0 instead of throwing
an error.
7. TOTALYTD()
Description:
TOTALYTD() calculates the cumulative total of a measure from the beginning of the year to the
current period in the context.
Real-time Scenario:
You want to calculate the Year-to-Date (YTD) sales for each product category.
Example:
DAX
CopyEdit
YTD Sales =
TOTALYTD(
SUM(Sales[SalesAmount]),
Dates[Date]
)
This will calculate the total sales from the start of the year to the current date, given the date
column Dates[Date].
8. USERELATIONSHIP()
Description:
USERELATIONSHIP() is used to change the relationship context when you have multiple
relationships between tables and want to use an alternative relationship.
Real-time Scenario:
You have a fact table with multiple relationships to a date table, and you want to use a specific
relationship for calculating sales based on a different date.
Example:
DAX
CopyEdit
Sales Using Alternate Date =
CALCULATE(
SUM(Sales[SalesAmount]),
USERELATIONSHIP(Sales[AlternateDate], Dates[Date])
)
Here, USERELATIONSHIP() specifies that the calculation should use the relationship between
Sales[AlternateDate] and Dates[Date], instead of the default relationship.