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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Conditional DAX equivalent of M?

Hello I am trying to get the equivalent of the following M code in DAX.

 

M:

if Text.Contains(Text.Lower([prod_name]),"purple") then "purple" else if Text.Contains(Text.Lower([prod_name]),"black") then "black" else if Text.Contains(Text.Lower([prod_name]),"white") then "white" else if Text.Contains(Text.Lower([prod_name]),"green") then "green"
else if Text.Contains(Text.Lower([prod_name]),"yellow") then "green" else if Text.Contains(Text.Lower([prod_name]),"blue") then "Blue" else if Text.Contains(Text.Lower([prod_name]),"red") then "Red" else Null.Type

 

I tried this in DAX but it only prints the final condition

DAX:

Flavors = if(CONTAINS(table,table[prod_name],"purple"),"purple",IF(CONTAINS(table,table[prod_name],"black"),"black",IF(CONTAINS(table,table[prod_name],"white"),"white",if(CONTAINS(table,table[prod_name],"yellow",table[prod_name],"green"),"green",if(CONTAINS(table,v[prod_name],"red"),"Red","Blue")))))

 

Any idea how I could do this? Thanks!

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

This worked

Flavors = SWITCH(
    TRUE(),
    ISERROR( FIND("purple", table[prod_name]))<> TRUE(), "purple",
    ISERROR( FIND("white", table[prod_name]))<> TRUE(), "white",
    ISERROR( FIND("black", table[prod_name]))<> TRUE(), "black",
    ISERROR( FIND("yellow", table[prod_name]))<> TRUE(), "green",
    ISERROR( FIND("green", table[prod_name]))<> TRUE(), "green",
    ISERROR( FIND("blue", table[prod_name]))<> TRUE(), "Blue",
    ISERROR( FIND("red", table[prod_name]))<> TRUE(), "Red",""
)

View solution in original post

Anonymous
Not applicable

The following would have also worked. 
Please ensure you accept a solution to close the topic. 

Column 2 =
IF (
    CONTAINSSTRING ( 'Table (2)'[prod_name], "purple" ),
    "purple",
    IF (
        CONTAINSSTRING ( 'Table (2)'[prod_name], "black" ),
        "black",
        IF (
            CONTAINSSTRING ( 'Table (2)'[prod_name], "white" ),
            "white",
            IF (
                CONTAINSSTRING ( 'Table (2)'[prod_name], "green" ),
                "green",
                IF (
                    CONTAINSSTRING ( 'Table (2)'[prod_name], "yellow" ),
                    "green",
                    IF (
                        CONTAINSSTRING ( 'Table (2)'[prod_name], "blue" ),
                        "Blue",
                        IF ( CONTAINSSTRING ( 'Table (2)'[prod_name], "red" ), "Red", "null" )
                    )
                )
            )
        )
    )
)

 

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

This worked

Flavors = SWITCH(
    TRUE(),
    ISERROR( FIND("purple", table[prod_name]))<> TRUE(), "purple",
    ISERROR( FIND("white", table[prod_name]))<> TRUE(), "white",
    ISERROR( FIND("black", table[prod_name]))<> TRUE(), "black",
    ISERROR( FIND("yellow", table[prod_name]))<> TRUE(), "green",
    ISERROR( FIND("green", table[prod_name]))<> TRUE(), "green",
    ISERROR( FIND("blue", table[prod_name]))<> TRUE(), "Blue",
    ISERROR( FIND("red", table[prod_name]))<> TRUE(), "Red",""
)
Anonymous
Not applicable

The following would have also worked. 
Please ensure you accept a solution to close the topic. 

Column 2 =
IF (
    CONTAINSSTRING ( 'Table (2)'[prod_name], "purple" ),
    "purple",
    IF (
        CONTAINSSTRING ( 'Table (2)'[prod_name], "black" ),
        "black",
        IF (
            CONTAINSSTRING ( 'Table (2)'[prod_name], "white" ),
            "white",
            IF (
                CONTAINSSTRING ( 'Table (2)'[prod_name], "green" ),
                "green",
                IF (
                    CONTAINSSTRING ( 'Table (2)'[prod_name], "yellow" ),
                    "green",
                    IF (
                        CONTAINSSTRING ( 'Table (2)'[prod_name], "blue" ),
                        "Blue",
                        IF ( CONTAINSSTRING ( 'Table (2)'[prod_name], "red" ), "Red", "null" )
                    )
                )
            )
        )
    )
)

 

Anonymous
Not applicable

Try the switch function 

https://docs.microsoft.com/en-us/dax/switch-function-dax

Something along the lines of 

Column = SWITCH([prod_name], 
"purple", "purple",
"black", "black",
"white", "white",
"green", "green",
"yellow", "green",
"blue", "Blue",
"red", "Red",
"null")
Anonymous
Not applicable

@Anonymous  Does the value have to be a number, because this is only returning the "else" condition

Anonymous
Not applicable

should work 

Karlos_0-1596115393958.png

 

Column = SWITCH([prod_name], 
"purple", "purple",
"black", "black",
"white", "white",
"green", "green",
"yellow", "green",
"blue", "Blue",
"red", "Red",
"null")

 

Anonymous
Not applicable

@Anonymous  Oh ok, I think this doesn't work in my case becuase there are multiple details in the [prod_name] column, so instead of purple it will say "purple(S)" or "purple[M]". Is there a way to integrate CONTAINS?

Anonymous
Not applicable

It's important to note, that it's ALL case sensitive + it'll include any trailing or leading spaces. for example 

 

"black" is NOT equal to "Black" or "black " or " black" etc..

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.