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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
ManInBlack
Advocate I
Advocate I

Difference between SEARCH and CONTAINSSTRING

I have to implement the following logic :

If the column Role contains distributor then the result is Yes if not then No.

Now I want to understand why both codes give different results ?

 

ColumnA Calculated = IF(
    CONTAINSSTRING(MyTable[Role],"distributor"),
    "Yes",
    "No"
)

 

 

ColumnB Calculated =
IF( ISERROR( SEARCH("distributor", MyTable[Role]) ), "Yes", "No" )
1 ACCEPTED SOLUTION
AmiraBedh
Super User
Super User

The CONTAINSSTRING directly checks if the specified substring ("distributor" in your case) is present within the string in MyTable[Role].
If the substring is found, it returns truue otherwise, it returns false

 

Meanwhile the SEARCH searches for a substring within a string and returns the position of the substring. Don't forget that it is case-insensitive.
If the substring is found, it returns the starting position of the substring. If not found, it results in an error.
Your code using SEARCH is somewhat inverted. It returns "Yes" when there's an error (when "distributor" is not found), and "No" when "distributor" is found. This is because ISERROR checks if SEARCH resulted in an error.

The main issue seems to be with the logic in your SEARCH implementation.

 

If you want to mirror the CONTAINSSTRING logic, you should return "Yes" when SEARCH does not result in an error (when it finds "distributor"), and "No" otherwise. The corrected code for ColumnB should be:

 

ColumnB Calculated =
IF(
ISERROR(
SEARCH("distributor", MyTable[Role])
),
"No", // When "distributor" is not found
"Yes" // When "distributor" is found
)

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

1 REPLY 1
AmiraBedh
Super User
Super User

The CONTAINSSTRING directly checks if the specified substring ("distributor" in your case) is present within the string in MyTable[Role].
If the substring is found, it returns truue otherwise, it returns false

 

Meanwhile the SEARCH searches for a substring within a string and returns the position of the substring. Don't forget that it is case-insensitive.
If the substring is found, it returns the starting position of the substring. If not found, it results in an error.
Your code using SEARCH is somewhat inverted. It returns "Yes" when there's an error (when "distributor" is not found), and "No" when "distributor" is found. This is because ISERROR checks if SEARCH resulted in an error.

The main issue seems to be with the logic in your SEARCH implementation.

 

If you want to mirror the CONTAINSSTRING logic, you should return "Yes" when SEARCH does not result in an error (when it finds "distributor"), and "No" otherwise. The corrected code for ColumnB should be:

 

ColumnB Calculated =
IF(
ISERROR(
SEARCH("distributor", MyTable[Role])
),
"No", // When "distributor" is not found
"Yes" // When "distributor" is found
)

Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Feb2025 NL Carousel

Fabric Community Update - February 2025

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