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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Is there a Power Query or DAX Equivalent for this code in Excel?

This code gets put into cell A3, and checks A2 for which Fruit is in the list of food items. Once it searches and finds the fruit, it places the fruit name in the cell A3. 

=IFNA(IFS(ISNUMBER(SEARCH("Apple",A2)),"Apple",
ISNUMBER(SEARCH("Banana",A2)),"Banana",
ISNUMBER(SEARCH("Orange",A2)),"Orange",
ISNUMBER(SEARCH("Grape",A2)),"Grape"),"")

Where A2 = "Carrot Bread Orange Eggs"
Result A3 = "Orange"

Is there an equivalent formula that can be created in Power Query or DAX that can do this task? It would be even better if it could reference a list of fruit in a column rather than being hardcoded. 

Thanks!

2 ACCEPTED SOLUTIONS
AlB
Community Champion
Community Champion

Hi @Anonymous 

Yes you can first define the list:

      RefList_ = {"Apple", "Banana", "Orange", "Grape"},

then you can add a custome column with the code

      = if List.Contains(RefList_, [ColumnA2]) then [ColumnA2] else null  

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

 

View solution in original post

PhilipTreacy
Super User
Super User

Hi @Anonymous 

This M code will search for each word in the ItemList and return all matching words from the Items column, in a new column.

Here's a sample PBIX file with the code

text-match.PNG

 

 

 

let
    ItemList = {"Apple", "Banana", "Orange", "Grape"},
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck4sKsovUXAqSk1MUfAvSsxLT1VwTU8vVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Matches", 
    
        each List.Accumulate
            (
                ItemList,

                "",

                (state, current) => 
                
                    if List.Contains(Text.Split([Items], " "), current) 
                    
                    then state & " " & current 
                    
                    else state
                
            ))
in
    #"Added Custom"

 

 

 

String matching is case sensitive.  If you want it to be case insensitive then add Comparer.OrdinalIgnoreCase to List.Contains :

 

 

 List.Contains(Text.Split([Items], " "), current, Comparer.OrdinalIgnoreCase) 

 

 

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

3 REPLIES 3
PhilipTreacy
Super User
Super User

Hi @Anonymous 

This M code will search for each word in the ItemList and return all matching words from the Items column, in a new column.

Here's a sample PBIX file with the code

text-match.PNG

 

 

 

let
    ItemList = {"Apple", "Banana", "Orange", "Grape"},
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck4sKsovUXAqSk1MUfAvSsxLT1VwTU8vVoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Items = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Items", type text}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Matches", 
    
        each List.Accumulate
            (
                ItemList,

                "",

                (state, current) => 
                
                    if List.Contains(Text.Split([Items], " "), current) 
                    
                    then state & " " & current 
                    
                    else state
                
            ))
in
    #"Added Custom"

 

 

 

String matching is case sensitive.  If you want it to be case insensitive then add Comparer.OrdinalIgnoreCase to List.Contains :

 

 

 List.Contains(Text.Split([Items], " "), current, Comparer.OrdinalIgnoreCase) 

 

 

Phil


If I answered your question please mark my post as the solution.
If my answer helped solve your problem, give it a kudos by clicking on the Thumbs Up.



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


wdx223_Daniel
Super User
Super User

Power Query = List.Skip({"Apple", "Banana", "Orange", "Grape"},each not Text.Contains(A2text,_)){0}?

DAX =MAXX(FILTER({"Apple", "Banana", "Orange", "Grape"},SEARCH([Value],A2text,,0)),[Value])

AlB
Community Champion
Community Champion

Hi @Anonymous 

Yes you can first define the list:

      RefList_ = {"Apple", "Banana", "Orange", "Grape"},

then you can add a custome column with the code

      = if List.Contains(RefList_, [ColumnA2]) then [ColumnA2] else null  

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

SU18_powerbi_badge

 

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.