使用 PowerQuery 根据多个条件识别分组行

问题描述 投票:0回答:1

我是 PowerQuery 的新手,只是无法弄清楚这一点,我打赌这是一件简单的事情......我有这样的表

代码 A 栏 B 栏 C 栏 级别 D 栏 E 栏
1234 细胞1 细胞1 细胞1 25 细胞1 细胞1
1234 细胞2 细胞2 细胞2 50 细胞2 细胞2
1234 细胞3 细胞3 细胞3 50 细胞3 细胞3
1234 4 号电池 4 号电池 4 号电池 75 4 号电池 4 号电池
5678 5 号小区 5 号小区 5 号小区 10 5 号小区 5 号小区
5678 6 号电池 6 号电池 6 号电池 已禁用 6 号电池 6 号电池
5678 7 号细胞 7号细胞 7 号细胞 20 7 号细胞 7 号细胞
5678 8 号电池 8 号电池 8 号电池 100 8 号电池 8 号电池
9090 9号细胞 9号细胞 9号细胞 已禁用 9号细胞 9号细胞
9090 10 号电池 10 号电池 10 号电池 已禁用 10 号电池 10 号电池
9090 11 号小区 11 号小区 11 号小区 已禁用 11 号小区 11 号小区
9090 12 号小区 12 号小区 12 号小区 已禁用 12 号小区 12 号小区

我已按“代码”对行进行分组并获取所有值,因此我最终得到了迷你表。我想识别每个迷你表中的“级别”列至少包含“禁用”和其他值的代码;参考上表,我应该得到仅输出代码5678。我设法添加自定义列“HasDisabled”,但我无法过滤掉所有“已禁用”的代码。我怎样才能做到这一点?这是我的 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"

使用 PowerQuery 根据多个条件识别分组行

excel powerbi powerquery powerbi-desktop m
1个回答
0
投票

试试这个:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", Int64.Type}, {"Column A", type text}, {"Column B", type text}, {"Column C", type text}, {"Level", type any}, {"Column D", type text}, {"Column E", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Code"}, {{"All", each _, type table [Code=nullable number, Column A=nullable text, Column B=nullable text, Column C=nullable text, Level=any, Column D=nullable text, Column E=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each 
if List.Contains( [All][Level], "disabled")  and List.Count( List.RemoveItems([All][Level],{"disabled"})) > 0 then true else false)
in
    #"Added Custom"

© www.soinside.com 2019 - 2024. All rights reserved.