Forum Discussion

thomasreick's avatar
thomasreick
Icon for Resolver I rankResolver I
1 year ago
Solved

Correct Use of Text.Contains in Combination with List.Contains??

Hi there, I would like to aggregate Item from Contoso Products to "ProductGroups" using CategorizeItem = if Text.Contains( [Item], List.Contains({"Gloves", "Vest", "Shorts"}, [Item] ) ) then "Clothe...
  • techies's avatar
    1 year ago

    Hi thomasreick pls try this custom column

     

    = if List.AnyTrue(List.Transform({"Gloves", "Vest", "Shorts"},
    (x) => Text.Contains([item], x, Comparer.OrdinalIgnoreCase)))
    then "Clothes"
    else "Other"

  • lbendlin's avatar
    lbendlin
    1 year ago

    That's a well-known "feature" aka bug.  The column statistics are cached and remember prior errors. Switch them off and on again to reset.

  • techies's avatar
    techies
    1 year ago

    Hi thomasreick please check this, in the power query, on the advanced editor

    let


    CategoryMappings = [
    Clothes = {"Cap", "Sleeve", "Gloves", "Vest", "Shorts", "Socks"},
    Bikes = {"Mountain-", "Road-", "Race-", "Touring-"},
    Accessories = {"Cage", "Dissolver", "Stand", "Rack", "Helmet", "Bottle", "Fender"},
    SpareParts = {"Tire", "Patches"},
    Nutrition = {} 
    ],


    GetCategory = (item as text) as text =>
    let
    Category = List.First(
    List.Select(
    Record.FieldNames(CategoryMappings),
    each List.AnyTrue(
    List.Transform(Record.Field(CategoryMappings, _),
    (x) => Text.Contains(item, x, Comparer.OrdinalIgnoreCase))
    )
    )
    )
    in
    if Category = null then "Nutrition" else Category,


    AddedProductCategories_techies = Table.AddColumn(#"Changed Type1", "techies", each GetCategory([Item]), type text)

    in
    AddedProductCategories_techies