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
MCacc
Helper III
Helper III

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
HotChilli
Super User
Super User

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
HotChilli
Super User
Super User

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.

 

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.