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
Jack007
Frequent Visitor

Check if Column contains string with iterator?

I need to verify if a column has certain strings in it. Currently, I am using the code If(containstring(columnname, "X"), "desired", "none").

However, the list of values for X is very large. Is there a method to make DAX iterate through each value of X?

1 ACCEPTED SOLUTION
v-tianyich-msft
Community Support
Community Support

Hi @Jack007 ,

 

Power BI Dax does not support iterate operations on the data.

However, you can use the M language to filter specific strings for this purpose.

I created a simple sample:

vtianyichmsft_8-1696922083655.png

  

First, you can create a parameter for your specific string like this:

vtianyichmsft_6-1696921671073.png

  

Then use this M script:

= Table.SelectRows(each Text.Contains([Large Value], #"Check X")) 

 

Result shown below:

vtianyichmsft_7-1696921712356.png

 

Perhaps this will work, and if you have any questions, please provide me with more information to make sure we can better solve the problem for you!

 

An attachment for your reference. Hope it helps.

 

Best regards,


Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

 

 

 

 

View solution in original post

2 REPLIES 2
v-tianyich-msft
Community Support
Community Support

Hi @Jack007 ,

 

Power BI Dax does not support iterate operations on the data.

However, you can use the M language to filter specific strings for this purpose.

I created a simple sample:

vtianyichmsft_8-1696922083655.png

  

First, you can create a parameter for your specific string like this:

vtianyichmsft_6-1696921671073.png

  

Then use this M script:

= Table.SelectRows(each Text.Contains([Large Value], #"Check X")) 

 

Result shown below:

vtianyichmsft_7-1696921712356.png

 

Perhaps this will work, and if you have any questions, please provide me with more information to make sure we can better solve the problem for you!

 

An attachment for your reference. Hope it helps.

 

Best regards,


Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

 

 

 

 

 

123abc
Community Champion
Community Champion

In Power BI DAX, you can't directly iterate through a list of values within a single calculated column formula. DAX is a language designed for creating calculated columns and measures, and it doesn't have native support for traditional iterative constructs like loops.

However, you can achieve the desired result by using functions that work with tables, such as FILTER and SUMMARIZE, to filter and manipulate your data. Here's a general approach to check if a column contains any of the values in a large list:

  1. Create a table containing the list of values you want to check (let's call it "ValuesTable"). This table should have a column, let's call it "ValueToCheck."

  2. Use DAX functions like FILTER and COUNTROWS to create a calculated column or measure that checks if the column contains any of the values from "ValuesTable."

Here's an example of how you can create a calculated column to check if a column named "ColumnName" contains any values from "ValuesTable":

 

ContainsValue =
IF(
COUNTROWS(
FILTER(
ValuesTable,
CONTAINSSTRING([ColumnName], ValuesTable[ValueToCheck])
)
) > 0,
"desired",
"none"
)

 

In this example, the FILTER function is used to create a filtered table that contains only rows where the "ValueToCheck" from "ValuesTable" is found in the "ColumnName." The COUNTROWS function then checks the number of rows in the filtered table. If it's greater than 0, it means at least one value from "ValuesTable" is found in "ColumnName," so "desired" is returned; otherwise, "none" is returned.

Keep in mind that using this approach can be resource-intensive, especially if you have a large dataset or a large list of values in "ValuesTable." You may want to optimize your data model and queries accordingly for performance reasons.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

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.

Top Solution Authors