Forum Discussion
IF statement and conditions
Hello, what would be the best way to run both these in DAX within the same statement. Switch / nested? I can't quite work it through, grateful for pointers as a newbie.
Although only 2 conditions (at the moment), and looked at OR || as well, not sure on most appropriate method.
IF(CONTAINSSTRING(Table[CompanyNumber],"Company2"),IF(Table[Letter Received]="No" && ISBLANK(Table[Responded],
IF(Table[CompanyNumber]<>"Company2"),IF(Table[Letter Received]<>"No" && ISBLANK(Table[Responded]),
"Check",
"Pass"
)
- Anonymous1 year ago
Hi JK-1,
Thank you for reaching out to the Microsoft Fabric Forum Community. And also thanks to Nasif_Azam , techies and DataNinja777 for Prompt and helpful response.
Just following up to see if the solution provided was helpful in resolving your issue. Please feel free to let us know if you need any further assistance.
If the response addressed your query, kindly mark it as Accepted Solution and click Yes if you found it helpful this will benefit others in the community as well.
Best regards,
Prasanna Kumar
5 Replies
- DataNinja777Super User
Hi JK-1 ,
The best and most recommended way to write this logic in DAX is by using the SWITCH function.
This approach is cleaner, easier to read, and more scalable than nested IF statements, making it a best practice for handling multiple conditions. It evaluates each condition in order and returns the result for the first one that is true.
YourNewColumn = SWITCH( TRUE(), CONTAINSSTRING(Table[CompanyNumber], "Company2") && Table[Letter Received] = "No" && ISBLANK(Table[Responded]), "Check", Table[CompanyNumber] <> "Company2" && Table[Letter Received] <> "No" && ISBLANK(Table[Responded]), "Check", "Pass" // This is the default result if no other condition is met )Best regards,
- JK-1Helper II
Thank you all for these suggestions. If I just ignored CompanyNumber for now, and although wouldn't expect the Letter Received or Responded data to be blank would I have to list the variables below for a Check result achieved where either: Letter Received or Responded blank, or both blank.
I've tested them as new - and think all are necessary, did some adaptations (separately) - so has been very useful in understanding the construction but wanted to make sure I wasn't adding in something extra in this slight follow on.
reassessed = SWITCH(TRUE(),ISBLANK([Letter Received]) && NOT ISBLANK([Responded]),"Check",NOT ISBLANK([Letter Received]) && ISBLANK([Responded]),"Check",ISBLANK([Letter Received]) && ISBLANK([Responded]),"Check","Pass")
- Nasif_AzamSuper User
Hey JK-1 ,
You're on the right track, and yes you can handle this logic using SWITCH, IF, or a combination of both, but in your case with only two conditions and some AND/ISBLANK logic, a nested IF structure is easier and more appropriate than SWITCH.
Let's first clarify your intent in plain logic:
If CompanyNumber = "Company2" AND Letter Received = "No" AND Responded is blank → "Check"
If CompanyNumber ≠ "Company2" AND Letter Received ≠ "No" AND Responded is blank → "Check"
Else → "Pass"
Corrected and Optimized DAX
Result = IF ( (Table[CompanyNumber] = "Company2" && Table[Letter Received] = "No" && ISBLANK(Table[Responded])) || (Table[CompanyNumber] <> "Company2" && Table[Letter Received] <> "No" && ISBLANK(Table[Responded])), "Check", "Pass" )Explanation
I combined your two conditions using an OR (||) operator at the outer level.
Each condition group is clearly separated using parentheses.
This avoids deeply nested IFs and improves readability.
Why Not SWITCH?
SWITCH works best when you're checking a single column against many constant values. Since you're testing multiple columns and a combination of boolean expressions, SWITCH would require complex nesting and doesn't simplify the logic here.
If you expect to scale this logic with more conditions, you could modularize the logic into variables using a calculated column:
Result = VAR IsCompany2Check = Table[CompanyNumber] = "Company2" && Table[Letter Received] = "No" && ISBLANK(Table[Responded]) VAR IsOtherCheck = Table[CompanyNumber] <> "Company2" && Table[Letter Received] <> "No" && ISBLANK(Table[Responded]) RETURN IF (IsCompany2Check || IsOtherCheck, "Check", "Pass")If you found this solution helpful, please consider accepting it and giving it a kudos (Like) it’s greatly appreciated and helps others find the solution more easily.
Best Regards,
Nasif Azam - AnonymousNot applicable
Hi JK-1,
Thank you for reaching out to the Microsoft Fabric Forum Community. And also thanks to Nasif_Azam , techies and DataNinja777 for Prompt and helpful response.
Just following up to see if the solution provided was helpful in resolving your issue. Please feel free to let us know if you need any further assistance.
If the response addressed your query, kindly mark it as Accepted Solution and click Yes if you found it helpful this will benefit others in the community as well.
Best regards,
Prasanna Kumar