Prompt
Answer
DAX Formula for Conditional Logic
Description
Create a DAX formula that uses multiple conditions (similar to IF statements in Excel) to evaluate different scenarios. For instance, if the Sales
value is greater than 1000, return "High"; if the Sales
value is between 500 and 1000, return "Medium"; and if the Sales
value is less than 500, return "Low".
DAX Formula
Category =
SWITCH(
TRUE(),
[Sales] > 1000, "High",
[Sales] >= 500, "Medium",
[Sales] < 500, "Low",
"Unknown"
)
Explanation
SWITCH(TRUE(), ...)
: TheSWITCH
function evaluates a list of expressions and returns the first result where the expression is true. UsingTRUE()
as the first argument makes this function act like multipleIF
statements.- Condition
[Sales] > 1000
: Evaluates whether the sales value is greater than 1000. If true, returns "High". - Condition
[Sales] >= 500
: Evaluates whether the sales value is between 500 and 1000 (since it's checked after the first condition). If true, returns "Medium". - Condition
[Sales] < 500
: Evaluates whether the sales value is less than 500. If true, returns "Low". - Default "Unknown": Provides a default value in case none of the conditions are met.
Practical Example
Consider a table SalesData
with the following data:
Sales |
---|
1200 |
700 |
300 |
Using the formula above, the resulting table with the new Category
column would be:
Sales | Category |
---|---|
1200 | High |
700 | Medium |
300 | Low |
Application and Benefits
- Categorization: Useful for categorizing numerical data into different segments based on defined thresholds.
- Simplification: Reduces the need for nested
IF
statements and makes the DAX code easier to read and maintain.
For further learning and advanced topics in DAX, consider exploring courses and resources on the Enterprise DNA Platform.
Description
This DAX formula categorizes sales values into "High", "Medium", and "Low" based on specified thresholds, simplifying conditional logic while enhancing data analysis in Power BI.