You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Power Query中筛选Level列含disabled及其他值的Code

需求说明

现有表格如下:

CodeColumn AColumn BColumn CLevelColumn DColumn E
1234Cell 1Cell 1Cell 125Cell 1Cell 1
1234Cell 2Cell 2Cell 250Cell 2Cell 2
1234Cell 3Cell 3Cell 350Cell 3Cell 3
1234Cell 4Cell 4Cell 475Cell 4Cell 4
5678Cell 5Cell 5Cell 510Cell 5Cell 5
5678Cell 6Cell 6Cell 6disabledCell 6Cell 6
5678Cell 7Cell 7Cell 720Cell 7Cell 7
5678Cell 8Cell 8Cell 8100Cell 8Cell 8
9090Cell 9Cell 9Cell 9disabledCell 9Cell 9
9090Cell 10Cell 10Cell 10disabledCell 10Cell 10
9090Cell 11Cell 11Cell 11disabledCell 11Cell 11
9090Cell 12Cell 12Cell 12disabledCell 12Cell 12

我已经按Code列分组得到子表,现在要筛选出子表里Level列既有"disabled"又有其他值的Code(比如示例里的5678)。目前已经加了自定义列HasDisabled,请问怎么过滤掉那些Level全是"disabled"的Code?

现有M代码如下:

let
    Source = #"Import",
    #"Sorted Rows" = Table.Sort(Source,{{"Code", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Code"}, {{"Tables", each _, type table [#"Code"=nullable text, #"Column A"=nullable text, #"Column B"=nullable text, #"Column C"=nullable text, #"Level"=nullable text, #"Column D"=nullable text, #"Column E"=nullable text]}}),

     #"Added Custom" = Table.AddColumn(#"Grouped Rows", "HasDisabled", each
    let
        Levels = [Tables][#"Level"], ContainsDisabled = List.Contains(Levels, "disabled", Comparer.OrdinalIgnoreCase)
    in
        ContainsDisabled)
in
    #"Added Custom"

解决方法

要实现这个需求,你需要在现有代码基础上再加两个判断:一是确认分组里有非"disabled"的Level值,二是同时满足HasDisabled为true(也就是存在disabled值)。

修改后的完整M代码如下:

let
    Source = #"Import",
    #"Sorted Rows" = Table.Sort(Source,{{"Code", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Code"}, {{"Tables", each _, type table [#"Code"=nullable text, #"Column A"=nullable text, #"Column B"=nullable text, #"Column C"=nullable text, #"Level"=nullable text, #"Column D"=nullable text, #"Column E"=nullable text]}}),

    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "HasDisabled", each
    let
        Levels = [Tables][#"Level"], ContainsDisabled = List.Contains(Levels, "disabled", Comparer.OrdinalIgnoreCase)
    in
        ContainsDisabled),

    // 新增列:判断是否存在非disabled的Level值
    #"Added HasNonDisabled" = Table.AddColumn(#"Added Custom", "HasNonDisabled", each
    let
        Levels = [Tables][#"Level"],
        ContainsNonDisabled = List.AnyTrue(List.Transform(Levels, each _ <> "disabled"))
    in
        ContainsNonDisabled),

    // 筛选同时满足有disabled和非disabled值的分组
    #"Filtered Rows" = Table.SelectRows(#"Added HasNonDisabled", each [HasDisabled] = true and [HasNonDisabled] = true),

    // 可选步骤:删掉辅助列,只留Code和子表
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows", {"HasDisabled", "HasNonDisabled"})
in
    #"Removed Columns"

代码解释

  • HasNonDisabled列:先遍历Level列表里的每个值,判断是否不等于"disabled",再用List.AnyTrue检查有没有至少一个符合条件的值,以此确认分组里存在非disabled的Level。
  • Table.SelectRows:只保留同时满足HasDisabled和HasNonDisabled都是true的行,也就是既有disabled又有其他值的分组。
  • 最后一步删除辅助列是可选的,如果你不需要保留这两个判断列,就加上这一步;如果需要留着看判断结果,可以去掉这一步。

内容的提问来源于stack exchange,提问作者d365b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:13:23