Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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" )
Solved! Go to Solution.
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
)
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
)
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 |
---|---|
58 | |
55 | |
55 | |
36 | |
30 |
User | Count |
---|---|
79 | |
66 | |
45 | |
44 | |
42 |