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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
MishiAb
Regular Visitor

PowerBI help: Formula to categorise data based on keywords

Hiya, I'm looking for some help creating a DAX formula to help with the following (example data below):

 

If the Classification column contains the words 'Monkey', or 'Bear' or 'Goat' or any combination of these words, I want the 'category' column to return 'Animals'.

  • If the Classification column contains the words 'Pear', or 'Apple' or 'Lychee' or any combination of these words, I want the 'category' column to return 'Fruit'
  • If the Classification column contains Blank or 'No classification' I want the 'category' column to return 'None'
  • And if it contains a combination of words from the 'animal' and 'fruit' groups e.g 'Monkey, Pear,  I want the 'category' column to return 'Multiple'

 

How the data looks

Classification
Monkey
Pear
Monkey, Bear
Pear, Apple, Lychee
Goat, Bear
Goat

 

I have gone round and round trying to figure this out and tried ChatGPT but keep failing. Please help!

 

Thank you.

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

@MishiAb 
Please refer to attached sample file with the proposed solution

1.png

Category = 
VAR Animals = { "Monkey", "Bear", "Goat" } -- Here you need to place all the possible values of the 1st category or reference a disconneted table
VAR Fruits = { "Apple", "Pear", "Lychee" } -- Here you need to place all the possible values of the 2nd category or reference a disconneted table
VAR String = 'Table'[Classification]
VAR Items = SUBSTITUTE ( String, ", ", "|" )
VAR Length = COALESCE ( PATHLENGTH ( Items ), 1 )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
VAR ConditionAnimals = NOT ISEMPTY ( INTERSECT ( T2, Animals ) )
VAR ConditionAFruits = NOT ISEMPTY ( INTERSECT ( T2, Fruits ) )
RETURN
    SWITCH ( 
        TRUE ( ),
        ConditionAnimals && ConditionAFruits, "Multiple",
        ConditionAnimals, "Animals",
        ConditionAFruits, "Fruits",
        "None"
    )

View solution in original post

3 REPLIES 3
MishiAb
Regular Visitor

Thank you both much for your help. I used the solution from tamerj1 and it worked perfectly!

ThxAlot
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3Py06tVIrViVYKSE0sAjMgYjoKTjABkIyOgmNBQU6qjoJPZXJGaipY3D0/sQRJGYgLZiiASSewrkSQLqXYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Classification = _t]),
    Fruit = {"Pear","Apple","Lychee"},
    Animal = {"Monkey","Bear","Goat"},

    Categorized = Table.AddColumn(
            Source,
            "category",
            each let
                frt = List.Contains(Fruit, [Classification], (x,y) => Text.Contains(y,x,Comparer.OrdinalIgnoreCase)),
                anm = List.Contains(Animal, [Classification], (x,y) => Text.Contains(y,x,Comparer.OrdinalIgnoreCase))
            in
                if frt and anm then "Multiple"
                    else if frt then "fruit"
                        else if anm then "animal" else "None"
    )
in
    Categorized

ThxAlot_0-1688539746601.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


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


)



tamerj1
Super User
Super User

@MishiAb 
Please refer to attached sample file with the proposed solution

1.png

Category = 
VAR Animals = { "Monkey", "Bear", "Goat" } -- Here you need to place all the possible values of the 1st category or reference a disconneted table
VAR Fruits = { "Apple", "Pear", "Lychee" } -- Here you need to place all the possible values of the 2nd category or reference a disconneted table
VAR String = 'Table'[Classification]
VAR Items = SUBSTITUTE ( String, ", ", "|" )
VAR Length = COALESCE ( PATHLENGTH ( Items ), 1 )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = SELECTCOLUMNS ( T1, "@Item", PATHITEM ( Items, [Value] ) )
VAR ConditionAnimals = NOT ISEMPTY ( INTERSECT ( T2, Animals ) )
VAR ConditionAFruits = NOT ISEMPTY ( INTERSECT ( T2, Fruits ) )
RETURN
    SWITCH ( 
        TRUE ( ),
        ConditionAnimals && ConditionAFruits, "Multiple",
        ConditionAnimals, "Animals",
        ConditionAFruits, "Fruits",
        "None"
    )

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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