Working with conditional functions in DAX

By Riya Jain & Abhinash Jena on September 2, 2024

Conditional functions in DAX (Data Analysis Expressions) are essential tools for performing conditional operations in your data models. These functions allow for more dynamic calculations, such as creating conditional columns, and measures, or performing data filtering based on specific criteria.

Conditional functions are integral to decision-making processes within data models, providing the foundation for complex conditional logic. They help with calculated columns and measures, enabling complex business rules and custom metrics. Conditional functions also help model scenarios where decisions need to be made based on data, such as determining whether a customer qualifies for a discount or flagging high-risk transactions.

IF is the key conditional function in DAX

The IF function is one of the most basic conditional functions in DAX. It evaluates a condition and returns one value if the condition is true and another value if the condition is false.

IF(condition, value if true, [value if false])
EXAMPLE

In this sample file create a calculated column that labels sales as “High” if they are greater than 20 and “Low” otherwise.

Sales label = IF(transactions[Units sold] > 20, "High", "Low")
Creating calculated columns with conditional functions
Creating calculated columns with conditional functions

Furthermore, AND and OR functions are used to combine multiple logical conditions. AND returns TRUE if all conditions are true, while OR returns TRUE if at least one condition is true.

IF(AND(<condition 1>, <condition 2>), value if true, value if false)
IF(OR(<condition 1>, <condition 2>), value if true, value if false)
EXAMPLE

Create a measure to sum the revenue from the categories ‘Accessories’ & ‘Office Supplies’.

officeAccessoryTotal = SUMX(
 transactions,
 IF(
    OR(transactions[Category] = "Accessories", 
    transactions[Category] = "Office Supplies"
   ),
   transactions[Revenue],
 0)
)
Result: 11K
Creating calculated measures with SUMX
Creating calculated measures with SUMX

Consequently, the NOT function is commonly used to reverse the result of a logical condition. This function is used to check if the condition is FALSE.

NOT(<condition>)
EXAMPLE

In the sample file, create a measure to find the total revenue from High and Medium transactions that are not discounted.

HighMediumNonDiscountSale = SUMX(
  transactions,
  IF(
     AND(NOT(transactions[Sales label] = "Low"), 
     transactions[Discount] = 0), 
     transactions[Revenue], 
     0
  )
)
Result: 250

Read more about SUMX.

Using SWITCH for multiple conditions

The SWITCH function is a powerful alternative to nested IF statements in DAX. It evaluates an expression against a list of possible values and returns the result corresponding to the matching value. If no match is found, an optional default value can be returned.

SWITCH(<expression>, <value 1>, <result 1>, <value 2>, <result 2>, ..., [<else>])
#Explanation
<expression>: The expression to evaluate.
value 1, value 2: Possible values of the expression.
result 1, result 2: Results corresponding to each value.
else: (Optional) The result if no values match.
EXAMPLE

From the sample file, create a calculated column to categorise each sales transaction label as High, Medium or Low.

Sales label = SWITCH(
    TRUE(),
    transactions[Units sold] > 50, "High", 
    transactions[Units sold] > 20, "Medium", 
    "Low"
)

Information functions based on type, state or value of the result

IFERROR function returns a specified value if the expression results in an error; otherwise, it returns the value of the expression. It is useful in cases such as division by 0.

IFERROR (<expression>, <result if error>)
  • ISBLANK(<value>): Returns TRUE if the value is blank.
  • ISNUMBER(<value>): Returns TRUE if the value is a number.
  • ISTEXT(<value>): Returns TRUE if the value is text.
  • ISERROR(<value>): Returns TRUE if the value results in an error.

Furthermore, the CONTAINS function checks if a specified value exists within a column of a table.

CONTAINS (<table>, <column name>, <value to find>)
NOTES

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.

I am a Senior Analyst at Project Guru, a research and analytics firm based in Gurugram since 2012. I hold a master’s degree in economics from Amity University (2019). Over 4 years, I have worked on worked on various research projects using a range of research tools like SPSS, STATA, VOSViewer, Python, EVIEWS, and NVIVO. My core strength lies in data analysis related to Economics, Accounting, and Financial Management fields.

Discuss