Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
k3rz0rg
Advocate III
Advocate III

Tableau flag code translation?

Hi everyone, 

I created a flag on tableau which works fine but now I have to translate that in DAX and I tried with if-else and it's throwing me error.

Could you please help me with it?

Tableau code:

 

If [Code] = 'D' 
OR

( [Code] = 'C' and [ID] <> '' and NOT ISNULL([ID]) )
THEN 1 ELSE 0 END

 

I tried the following code but didn't work and in DAX, AND or OR operators take only 2 parameters and my second part of OR has 3. 

IF(
    OR( VALUES('NA Pharmacy Distribution'[Sale_Code]) = "D" ,
        AND( VALUES('NA Pharmacy Distribution'[Sale_Code]) = "C", VALUES('NA Pharmacy Distribution'[Contract_ID]) <> "", NOT(ISBLANK(VALUES('NA Pharmacy Distribution'[Contract_ID]))))
    )
    ,1
    ,0
)

Any help would be greatly appreciated.

 

Thanks! 

2 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hi @k3rz0rg 

It looks like you're creating a calculated column - is that right?

 

I would suggest this. I've used variables for readability and you can use the && operator instead of the AND function to handle more than 2 conditions.

Also, VALUES isn't needed if you are referring to a column's value in a row context (assuming this is a calculated column). Just use the column reference directly.

 

Flag = 
VAR SaleCode = 'NA Pharmacy Distribution'[Sale_Code]
VAR ContractID = 'NA Pharmacy Distribution'[Contract_ID]
RETURN
    IF (
        OR (
            SaleCode = "D",
            SaleCode = "C" && ContractID <> "" && NOT ( ISBLANK ( ContractID ) )
        ),
        1,
        0
    )

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

Hi @OwenAuger , thank you for your response! I was trying to create a calculated measure and almost forgot that I could create a column as well since I'm the owner of the dataset. Your suggestion helped me to solved my purpose!

View solution in original post

3 REPLIES 3
v-yilong-msft
Community Support
Community Support

Hi @k3rz0rg ,

I’d like to acknowledge the valuable input provided by the @OwenAuger . His initial ideas were instrumental in guiding my approach. However, I noticed that further details were needed to fully understand the issue.  

In my investigation, I took the following steps:

I create a table as you mentioned.

vyilongmsft_0-1714024492850.png

Then I change your DAX code and get what you want.

 

Column =
IF (
    'NA Pharmacy Distribution'[Sale_Code] = "D"
        || (
            'NA Pharmacy Distribution'[Sale_Code] = "C"
                && NOT ISBLANK ( 'NA Pharmacy Distribution'[Contract_ID] )
                    && 'NA Pharmacy Distribution'[Contract_ID] <> ""
        ),
    1,
    0
)

 

vyilongmsft_1-1714024736558.png

 

 

 

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

OwenAuger
Super User
Super User

Hi @k3rz0rg 

It looks like you're creating a calculated column - is that right?

 

I would suggest this. I've used variables for readability and you can use the && operator instead of the AND function to handle more than 2 conditions.

Also, VALUES isn't needed if you are referring to a column's value in a row context (assuming this is a calculated column). Just use the column reference directly.

 

Flag = 
VAR SaleCode = 'NA Pharmacy Distribution'[Sale_Code]
VAR ContractID = 'NA Pharmacy Distribution'[Contract_ID]
RETURN
    IF (
        OR (
            SaleCode = "D",
            SaleCode = "C" && ContractID <> "" && NOT ( ISBLANK ( ContractID ) )
        ),
        1,
        0
    )

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Hi @OwenAuger , thank you for your response! I was trying to create a calculated measure and almost forgot that I could create a column as well since I'm the owner of the dataset. Your suggestion helped me to solved my purpose!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.