Text.Contains 如果文本包含关键字作为子文本

Text.Contains if Text contains keyword as subtext

我希望使用 Text.Contains:

从所需列中显示的句子中拆分动物的性别

在这种稍微不寻常的情况下,男性包含在女性中,因此所有结果 return 男性。

如何修改此代码以实现此目的?

当前 M 代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1], "male", Comparer.OrdinalIgnoreCase) then "Male" else null)
in
    #"Added Custom"

更新:

这是我目前的解决方案:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Females", each if Text.Contains([Column1], "Female", Comparer.OrdinalIgnoreCase) then "Female" else null),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Males", each if Text.Contains([Column1], "Male", Comparer.OrdinalIgnoreCase) then "Male" else null),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Sex", each if [Females] = "Female" then "Female" else if [Males] = "Male" then "Male" else null),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom2",{"Column1", "Sex"})
in
    #"Removed Other Columns"

但是,我不太喜欢需要多个自定义列,我觉得会有更优雅的解决方案。

我建议将输入分成 list-object 并检查列表是否包含“女性”或“男性”(或两者都不包含):

if List.Contains(Text.Split([column1]," "), "female") then "female" else if List.Contains(Text.Split([current]," "), "male") then "male" else null

或者,在M-Code中:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"column1", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if List.Contains(Text.Split([column1]," "), "female") then "female" else if List.Contains(Text.Split([column1]," "), "male") then "male" else null)
in
    #"Added Custom"


也许,如果您想 运行 检索误报的风险,请尝试 List.FindText:

List.First(List.FindText(Text.Split([column1]," "),"male"))

或在M-Code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"current", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.First(List.FindText(Text.Split([column1]," "),"male")))
in
    #"Added Custom"