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

Join 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.

Reply
Anonymous
Not applicable

Power Query - IF function related to a table (Parameters??)

Hello everyone, 

 

I´ve been working around but i don´t have enought M knowledge. 

 

I want to categorize some of my products depending on which text they contains. Until here i am doing that with the conditional feature of PowerQuery (same as If). But at the end it works as a table of two columns where if you find A  give me B. 

 

My question es , can i do that with Power query and one table in Excel? It would be really helpful because if i can do that i can give the hability of categorization to my end user. 

 

Thank you in advance!! 

2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

If I interpret your information correctly, you want to use a lookup table.

 

After Table1 and table Categories are loaded in Power Query (connection only), the following query adds a column to table Table1 with the category from table Categories:

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Category", (x) => Text.Combine(Table.SelectRows(Categories, each Text.Contains(x[SomeText],[TextPart],Comparer.OrdinalIgnoreCase))[Category],", "))
in
    #"Added Custom"

 

The texts are compared case-insensitive; in case a text contains multiple TextParts, it will get all associated categories,

 

Examples:

 

If function related to a table.png

 

No Category found for Sean.

Specializing in Power Query Formula Language (M)

View solution in original post

You can extend the formula with List.Distinct.

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Category", (x) => Text.Combine(List.Distinct(Table.SelectRows(Categories, each Text.Contains(x[SomeText],[TextPart],Comparer.OrdinalIgnoreCase))[Category]),", "))
in
    #"Added Custom"

 

Remark: the close parenthesis for List.Distinct is behind "[Category]"

Specializing in Power Query Formula Language (M)

View solution in original post

5 REPLIES 5
MarcelBeug
Community Champion
Community Champion

If I interpret your information correctly, you want to use a lookup table.

 

After Table1 and table Categories are loaded in Power Query (connection only), the following query adds a column to table Table1 with the category from table Categories:

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Category", (x) => Text.Combine(Table.SelectRows(Categories, each Text.Contains(x[SomeText],[TextPart],Comparer.OrdinalIgnoreCase))[Category],", "))
in
    #"Added Custom"

 

The texts are compared case-insensitive; in case a text contains multiple TextParts, it will get all associated categories,

 

Examples:

 

If function related to a table.png

 

No Category found for Sean.

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Hello MarcelBeug,

 

Everything works perfect, it helps me to avoid a lot of manual stuff... 

 

I have one more question about it. I want to avoid the ones which the categorization is duplicated. I have several Key_Words for each category and sometimes they have more than one. 

 

I.e: Neobaron is categorized as Visitor, Visitor. is there any way to avoid that? For the ones that the categorization is different is fine, but i want to avoid the ones those are duplicated. .

 

Again thank you, 

You can extend the formula with List.Distinct.

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Category", (x) => Text.Combine(List.Distinct(Table.SelectRows(Categories, each Text.Contains(x[SomeText],[TextPart],Comparer.OrdinalIgnoreCase))[Category]),", "))
in
    #"Added Custom"

 

Remark: the close parenthesis for List.Distinct is behind "[Category]"

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

You are genius!!!! 
And your remark was great too Smiley Wink

Anonymous
Not applicable

Thank you very much!! It is perfect, i will try to modify your code to add it directly to table one. And your coderis better because i am getting more categories than one. I bought M is for Data Monkey. do you know where i could learn more about M? Thank you in advance! 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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