在Power Query中筛选Level列含disabled及其他值的Code
需求说明
现有表格如下:
| Code | Column A | Column B | Column C | Level | Column D | Column E |
|---|---|---|---|---|---|---|
| 1234 | Cell 1 | Cell 1 | Cell 1 | 25 | Cell 1 | Cell 1 |
| 1234 | Cell 2 | Cell 2 | Cell 2 | 50 | Cell 2 | Cell 2 |
| 1234 | Cell 3 | Cell 3 | Cell 3 | 50 | Cell 3 | Cell 3 |
| 1234 | Cell 4 | Cell 4 | Cell 4 | 75 | Cell 4 | Cell 4 |
| 5678 | Cell 5 | Cell 5 | Cell 5 | 10 | Cell 5 | Cell 5 |
| 5678 | Cell 6 | Cell 6 | Cell 6 | disabled | Cell 6 | Cell 6 |
| 5678 | Cell 7 | Cell 7 | Cell 7 | 20 | Cell 7 | Cell 7 |
| 5678 | Cell 8 | Cell 8 | Cell 8 | 100 | Cell 8 | Cell 8 |
| 9090 | Cell 9 | Cell 9 | Cell 9 | disabled | Cell 9 | Cell 9 |
| 9090 | Cell 10 | Cell 10 | Cell 10 | disabled | Cell 10 | Cell 10 |
| 9090 | Cell 11 | Cell 11 | Cell 11 | disabled | Cell 11 | Cell 11 |
| 9090 | Cell 12 | Cell 12 | Cell 12 | disabled | Cell 12 | Cell 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
相关产品推荐
相关产品推荐

