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