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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
jerryr125
Helper I
Helper I

Asssign category based upon text in a field

Hi - I am looking to do the following :

(I posted this question before but cannot seem to locate it!)

 

I would like to assign a category to the master table (Master Table) based upon search criteria in another table (Table 1)

 

Example

Master Table (input)

IDTextCategory
1I like ice cream 
2Apple trees are growing in the yard 
3Here is a bowl of nuts 
4 I ate some ice cream on Sunday 

 

 

Table 1

 

Search TextCategory
Ice CreamDessert
CakeDessert
BananaFruit
AppleFruit
PetzelsSnack
NutsSnack

 

Final - Master Table (output) - search Text field so to speak and determine the category

 

IDTextCategory
1I like ice creamDessert
2Apple trees are growing in the yardFruit
3Here is a bowl of nutsSnack
4I ate some ice cream on SundayDessert

 

Any help would be appreciated - Jerry

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

You can use Comparer.OrdinalIgnoreCase if you do not want to deal with the case of the values.

The code below provides an example of this.

    Table.AddColumn(
        #"Changed Type",                            //previous step in the query
        "Category", 
        each 
        let 
            searchString = [Text]                   //set the current row [Text] column into a variable
        in 
            Text.Combine(                           //combines the values of the matching Category column list to a single string with a comma delimiter
                Table.SelectRows(
                    Table2,                         //select rows in Table 2 that have matching text
                    each 
                    Text.Contains(
                        searchString,               //look in the [Text] column of the current row of Table
                        Record.Field(               
                            _,                      //the current row record in Table2
                            "Search Text"           //column name of the values to search for in Table2
                        ), 
                        Comparer.OrdinalIgnoreCase  //ignores the case of the text during the comparison
                    )
                )[Category],                        //converts the Category column from the resulting selected rows from Table2 into a list
                ","
            )
        )

 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

5 REPLIES 5
ThxAlot
Super User
Super User

Simple enough,

ThxAlot_0-1732975029074.png

let
    Master = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc2xCsIwFIXhVzlkLoLaF3AQdHFpt5Ahptcm2OSWJKX07b1kcj4f59danVWnnljClxAcwWWyUZlOq4sMt3VdCDUTFdhMmDPvIc0ICdUTDpunZq9iHyQgiMOb9wX8QdpqaXPfGrYSCse/Djhh2NJkj8bG+zCKfDGirc63UKVY4OX6pIz5AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Text = _t]),
    Search = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kxOVXAuSk3MVdJRckktLk4tKlGK1YlWck7MTkUTckrMA0KgoFtRaSZEyLGgICcVRSQgtaQqNacYKBacl5icDRbzKy1BEogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Search Text" = _t, Category = _t]),
    #"===========" = null,

    Categorized = let cols = Table.ToColumns(Search) in Table.AddColumn(Master, "Category", each try cols{1}{List.PositionOf(cols{0}, [Text], 0, (x,y) => Text.Contains(y, x, Comparer.OrdinalIgnoreCase))} otherwise "", type text)
in
    Categorized


Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



jgeddes
Super User
Super User

You can use Comparer.OrdinalIgnoreCase if you do not want to deal with the case of the values.

The code below provides an example of this.

    Table.AddColumn(
        #"Changed Type",                            //previous step in the query
        "Category", 
        each 
        let 
            searchString = [Text]                   //set the current row [Text] column into a variable
        in 
            Text.Combine(                           //combines the values of the matching Category column list to a single string with a comma delimiter
                Table.SelectRows(
                    Table2,                         //select rows in Table 2 that have matching text
                    each 
                    Text.Contains(
                        searchString,               //look in the [Text] column of the current row of Table
                        Record.Field(               
                            _,                      //the current row record in Table2
                            "Search Text"           //column name of the values to search for in Table2
                        ), 
                        Comparer.OrdinalIgnoreCase  //ignores the case of the text during the comparison
                    )
                )[Category],                        //converts the Category column from the resulting selected rows from Table2 into a list
                ","
            )
        )

 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Hi - I copied the code above into the advance editor of the table - I get a EOF error.

Here is what the code looks like:

 
let
  Source = Excel.Workbook(Web.Contents("test.xlsx"), null, true),
  #"Navigation 1" = Source{[Item = "Master", Kind = "Sheet"]}[Data],
  #"Promoted headers" = Table.PromoteHeaders(#"Navigation 1", [PromoteAllScalars = true]),
  #"Transform columns" = Table.TransformColumnTypes(#"Promoted headers", {{"ID", type text}, {"Text", type text}}),
  #"Replace errors" = Table.ReplaceErrorValues(#"Transform columns", {{"ID", null}, {"Text", null}})
in
  #"Replace errors"

  Table.AddColumn(
        #"Changed Type",                            //previous step in the query
        "Category",
        each
        let
            searchString = [Text]                   //set the current row [Text] column into a variable
        in
            Text.Combine(                           //combines the values of the matching Category column list to a single string with a comma delimiter
                Table.SelectRows(
                    Table1,                         //select rows in Table 1 that have matching text
                    each
                    Text.Contains(
                        searchString,               //look in the [Text] column of the current row of Table
                        Record.Field(              
                            _,                      //the current row record in Table1
                            "Search Text"           //column name of the values to search for in Table2
                        ),
                        Comparer.OrdinalIgnoreCase  //ignores the case of the text during the comparison
                    )
                )[Category],                        //converts the Category column from the resulting selected rows from Table1 into a list
                ","
            )
        )
 
***********************
Tables:
 Master
jerryr125_0-1732977513597.png

 

Table 1

jerryr125_1-1732977728408.png

 

Hi- I would like to give this a try - where do I paste the code ? 

ZhangKun
Resolver III
Resolver III

Words need to be lowercase:

// Table1
let
    源 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kxOVXAuSk3MVdJRckktLk4tKlGK1YlWck7MTkUTckrMA0KgoFtRaSZEyLGgICcVRSQgtaQqNacYKBacl5icDRbzKy1BEogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Search Text" = _t, Category = _t]),
    更改的类型 = Table.TransformColumnTypes(源,{{"Search Text", type text}, {"Category", type text}}),
    已添加自定义 = Table.AddColumn(更改的类型, "Lower", each Text.Lower([Search Text]))
in
    已添加自定义

The sentence also needs to be converted to lowercase. If there are multiple results (the last row), they will be concatenated with commas

// Master Table
let
    源 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc47DsIwEIThq4y2TsPrAHSkprRcLPESLJx15DiKcnsWN1D/nzTjHB2oox4pvgVxEAxFeCLfOTpauM5zEtQisoCLYCx5izoiKupLsHMJzZ7M3sRANIdH3hLyE7rWpeWzZfTgKljy9DeErLivGnhv7mLu11gD+HuAvP8A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Text = _t]),
    更改的类型 = Table.TransformColumnTypes(源,{{"ID", Int64.Type}, {"Text", type text}}),
    已添加自定义 = Table.AddColumn(更改的类型, "match", each Text.Combine(Table.SelectRows(Table1, (r) => Text.Contains(Text.Lower([Text]), r[Lower]))[Category], ","))
in
    已添加自定义

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.