cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper I
Helper I

Create a calculate column adding a text depending on IF conditions from another column

Hello everyone, I'm trying to create a calculated column to add a description to a column of codes you already have.

 

I tried something like this:

Commodity Code Desc = var x = [table_code]
return IF( x = VALUE(CONTAINSSTRING(table[table_code]; "M01")); "Dec 1";
IF( x = VALUE(CONTAINSSTRING(table[table_code];" M02"); "Dic2";
IF(x = VALUE(CONTAINSSTRING(table[table_code]; "M03")); "Dec 3";
IF(x = VALUE(CONTAINSSTRING(table[table_code]; "M04")); "Dec 4";
IF( x = VALUE(CONTAINSSTRING(table[table_code]; "S02")); "Desc 5" ;
IF(x = VALUE(CONTAINSSTRING(table[table_code];" MBL")); "Dec 6";
IF( x  =VALUE(CONTAINSSTRING(table[table_code];" MVA")); "Desc 7"; "General")))))))))

 

But this is the message I get from Power Bi

"DAX comparison operations do not support comparing Text values to values of type Number. Consider using the VALUE or FORMAT function to convert one of the values."

 

I used CONTAINSSTRING because I have many records and I have to follow the logic to add the correct description depending on how each of my codes starts.

 

Do you guys have any other ideas? Thank you very much for your help

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User I
Super User I

Re: Create a calculate column adding a text depending on IF conditions from another column

CONTAINSSTRING returns true or false. VALUE converts a text string that represents a number to a number.  So that's why the error results.

 

If you want to use the technique (using a switch instead of the IFs, the code will probably look like:

Commodity Code Desc = 
SWITCH (TRUE(),
 CONTAINSSTRING(Table[table_code], "M01"), "Dec 1",
 CONTAINSSTRING(Table[table_code], " M02"), "Dic2",
 CONTAINSSTRING(table[table_code], "M03"), "Dec 3",
 CONTAINSSTRING(table[table_code], "M04"), "Dec 4",
 CONTAINSSTRING(table[table_code], "S02"), "Desc 5" ,
 CONTAINSSTRING(table[table_code]," MBL"), "Dec 6",
 CONTAINSSTRING(table[table_code]," MVA"), "Desc 7", "General")

If all the table_code fields have the 3 letter code on the left, you might use LEFT( thefield , 3).  Good luck.

 

View solution in original post

1 REPLY 1
Highlighted
Super User I
Super User I

Re: Create a calculate column adding a text depending on IF conditions from another column

CONTAINSSTRING returns true or false. VALUE converts a text string that represents a number to a number.  So that's why the error results.

 

If you want to use the technique (using a switch instead of the IFs, the code will probably look like:

Commodity Code Desc = 
SWITCH (TRUE(),
 CONTAINSSTRING(Table[table_code], "M01"), "Dec 1",
 CONTAINSSTRING(Table[table_code], " M02"), "Dic2",
 CONTAINSSTRING(table[table_code], "M03"), "Dec 3",
 CONTAINSSTRING(table[table_code], "M04"), "Dec 4",
 CONTAINSSTRING(table[table_code], "S02"), "Desc 5" ,
 CONTAINSSTRING(table[table_code]," MBL"), "Dec 6",
 CONTAINSSTRING(table[table_code]," MVA"), "Desc 7", "General")

If all the table_code fields have the 3 letter code on the left, you might use LEFT( thefield , 3).  Good luck.

 

View solution in original post

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Galleries

Galleries

Looking for inspiration on how to present your data? Need instructional videos? Check out our Galleries!

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors