Search code examples
powerquery

In Powerquery how can i find a text that is not an exact match?


I have the following formula which works and finds an exact match of "1 EA"

= List.Select(ColumnNames, each List.Contains(Table.Column(#"Filled Down",_),"1 EA"))

However i want to modify it so that it will look for "1 EA" within the text. In other words, if there is text or white space to the left or right, it will still return true

I have tried the following courtsey of ChatGPT but they do not work

= List.Select(ColumnNames, each List.AnyTrue(List.Transform(Table.Column(#"Filled Down", _), each Text.Contains(_, "1 EA"))))

In above i get an error. However there is no description of the error apart from showing the name of a single column which actually contains the "1 EA" text.

= List.Select(ColumnNames, each List.Contains(Table.Column(#"Filled Down", _), each Text.Contains(_, "1 EA")))

In above i get returned an empty list which also does not make sense

My Data is below- And in below scenario i am expecting to return Data.Column1, Data.Column2 and Data.Column4 as the list:

Data.Column1 Data.Column2 Data.Column3 Data.Column4
Sample Text 1 EAsdd Sample text 1 EA
1 EA Sample text Sample Text 1 EAff

Solution

  • Given your data, the following will return the column names of those columns containing 1 EA. Note that only Column 2 contains that text. If you want to return columns 1,2 and 4, you will need to test for 1EA as well, as I show in this code:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Data.Column1", type text}, {"Data.Column2", type text}, {"Data.Column3", type text}, {"Data.Column4", type text}}),
    
        #"Columns with 1EA" = 
            List.Accumulate(
               Table.ColumnNames(#"Changed Type"),
                {},
                (s,c)=> if List.AnyTrue(List.Transform(Table.Column(#"Changed Type",c), each 
                    Text.Contains(_,"1 EA") or Text.Contains(_, "1EA")))
                            then s & {c} else s)
        
    in
        #"Columns with 1EA"
    
    

    Data
    enter image description here

    Results
    enter image description here

    *Edit:
    Given your updated data where you translated 1EA => 1 EA, you merely have to change the code to remove the test for 1EA

    ...
        #"Columns with 1EA" = 
            List.Accumulate(
               Table.ColumnNames(#"Changed Type"),
                {},
                (s,c)=> if List.AnyTrue(List.Transform(Table.Column(#"Changed Type",c), each 
                    Text.Contains(_,"1 EA") ))
                            then s & {c} else s)
    ...