Power BI - If with AND and OR Statements

Viewed 27

I have the following DAX Calculated column formula which isn't executing correctly

CheckStatus =
IF('Data'[DaysUntilExpiry] >-100 && 'Data'[DaysUntilExpiry]<=0 && 'Data'[DaysUntilExpiry_PreviousMonth] <-100 || 'Data'[DaysUntilExpiry_PreviousMonth]>0, "Check Product")

Formula break down: I have 2 columns: DaysUntilExpiry & DaysUntilExpiry_PreviousMonth.

What I want to do is output "Check Product"

if DaysUntilExpiry is >-100

AND DaysUntilExpiry < 0

AND

DaysUntilExpiry_PreviousMonth is <-100

OR

DaysUntilExpiry_PreviousMonth >0

The formula works for DaysUntilExpiry is >-100 AND < 0 But when the second part is added it executes but the output is incorrect.

Data Table won't format correctly for me (see pic below) | DaysUntilExpiry | DaysUntilExpiryPreviousMonth | | --------------- | ---------------------------- | | -2 | 1 | | -102 | 2 | | -5 | -10 |

Check Product should only output for the first row.

Data Thanks

1 Answers

Add a parenthesis around your predicate groups, else you will quickly run into issues where your logic is no longer obvious:

CheckStatus =
IF (
    'Data'[DaysUntilExpiry] >-100 
      && 'Data'[DaysUntilExpiry]<=0 
      && 
       (
         'Data'[DaysUntilExpiry_PreviousMonth] <-100 
          || 'Data'[DaysUntilExpiry_PreviousMonth] > 0 
       ),
    "Check Product"
)

Edit: I think I got it right this time.. ;)

Edit 2: Works for me. Perhaps you should reconsider how you phrase your question? In any event, this is a simple logic gate. Use parenthesis correctly and your calculations will be correct.

enter image description here

Related