【问题标题】:Powerquery, does string contain an item in a listPowerquery,字符串是否包含列表中的项目
【发布时间】:2020-06-16 01:48:08
【问题描述】:

我想过滤多个文本列([Name]、[GenericName]、 [SimpleGenericName])是否包含列表中的子字符串。文本也是大小写混合的,所以我也需要在那里做一个 Text.Lower([Column]) 。

我试过公式:

= Table.SelectRows(#"Sorted Rows", each List.Contains(MED_NAME_LIST, Text.Lower([Name])))

但是,这不起作用,因为 [名称] 列与列表中的那些项目不完全匹配(例如,如果列表包含“甲基强的松龙”,它不会选择“甲基强的松龙标签”)

一个工作过滤器的例子,列出了所有的列表:

= Table.SelectRows(#"Sorted Rows", each Text.Contains(Text.Lower([Name]), "methylprednisolone") or Text.Contains(Text.Lower([Name]), "hydroxychloroquine") or Text.Contains(Text.Lower([Name]), "remdesivir") or Text.Contains(Text.Lower([GenericName]), "methylprednisolone") or Text.Contains(Text.Lower([GenericName]), "hydroxychloroquine") or Text.Contains([GenericName], "remdesivir") or Text.Contains(Text.Lower([SimpleGenericName]), "methylprednisolone") or Text.Contains(Text.Lower([SimpleGenericName]), "hydroxychloroquine") or Text.Contains([SimpleGenericName], "remdesivir"))

我想让这个更简洁,而不是把所有这些都写出来,因为我还希望能够从引用的表中扩展列表以使其成为动态搜索。

提前谢谢你

【问题讨论】:

    标签: list filter substring powerquery


    【解决方案1】:

    如果我有药物清单:

    我需要过滤我的表格:

    只保留某些列(我们稍后将具体指定哪些列)包含与上述药物列表中的任何项目不区分大小写、部分匹配的行,那么执行此操作的一种方法可能是:

    let
        MED_NAME_LIST = {"MEthYlprednisolone", "hYdroxychloroquine", "rEMdesivir"},
        initialTable = Table.FromRows({
            {"Methylprednisolone Tab", "train", "car", "bike"},
            {"no", "no", "no", "no"},
            {"tram", "teleport", "hydroxychloroQuine Tab", "jet"},
            {"no", "no", "no", "yes"},
            {"REMdesivir Tab", "bus", "taxi", "concord"}
        }, type table [Name = text, GenericName = text, SimpleGenericName = text, SomeOtherColumn = text]),
        filtered = Table.SelectRows(initialTable, each List.ContainsAny(
            {[Name], [GenericName], [SimpleGenericName]},
            MED_NAME_LIST,
            (rowValue as text, medicineFromList as text) as logical => Text.Contains(rowValue, medicineFromList, Comparer.OrdinalIgnoreCase)
        ))
    in
        filtered
    
    • filtered 中,List.ContainsAny 用于确定任何指定列(NameGenericNameSimpleGenericName)是否包含与MED_NAME_LIST 中任何值的“匹配”。
    • “匹配”的标准是:
      • 必须忽略大小写敏感性(因此使用Comparer.OrdinalIgnoreCase
      • 匹配必须是部分的(因此使用Text.Contains

    上面的代码给了我以下,我相信这是你描述的过滤行为:

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2020-04-02
      • 2020-07-31
      相关资源
      最近更新 更多