Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
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!
Solved! Go to Solution.
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",""
)
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" )
)
)
)
)
)
)
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",""
)
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" )
)
)
)
)
)
)
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 Does the value have to be a number, because this is only returning the "else" condition
should work
Column = SWITCH([prod_name],
"purple", "purple",
"black", "black",
"white", "white",
"green", "green",
"yellow", "green",
"blue", "Blue",
"red", "Red",
"null")
@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?
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..
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
15 | |
11 | |
11 | |
10 | |
10 |
User | Count |
---|---|
19 | |
14 | |
13 | |
11 | |
8 |