CountIfs() 在 Power Query M 中等效,在 self 中每行计数

ak1*_*358 4 excel m excel-formula powerquery

我正在尝试在 Power Query 查询中实现类似 countifs() 的逻辑,但不在引用源表的汇总表中实现。相反,我想显示所有记录并将其作为另一列(在我的实际用例中这是必要的)。这就是我的意思...

输入数据:

ID | Animal | Color
-- | ------ | -----
 1 | Zebra  | Red
 2 | Zebra  | Blue
 3 | Zebra  | Red
 4 | Zebra  | Red
Run Code Online (Sandbox Code Playgroud)

期望的输出:

ID | Animal | Color | Count of others with same color
-- | ------ | ----- | -------------------------------
 1 | Zebra  | Red   | 3
 2 | Zebra  | Blue  | 1
 3 | Zebra  | Red   | 3
 4 | Zebra  | Red   | 3
Run Code Online (Sandbox Code Playgroud)

在 Excel 内联公式中,要计算“具有相同颜色的其他人的计数”列,我将使用

=COUNTIFS([Animal],[@Animal],[Color],[@Color])
Run Code Online (Sandbox Code Playgroud)

我如何使用 M 语言在 Power Query 中执行此操作?

Ale*_*son 5

对已过滤的表使用计数。


自定义列的公式如下:

List.Count(
    Table.SelectRows(
        #"Previous Step Name Goes Here",
        (C) => [Animal] = C[Animal] and [Color] = C[Color]
    )[ID]
)
Run Code Online (Sandbox Code Playgroud)

函数() =>构造是必需的,因为您需要引用两个单独的上下文。一个是您正在评估其中的函数的行,另一个是您正在过滤的表的行Table.SelectRows。乍一看这有点奇怪,所以我建议搜索“ Power Query every function ”并进行一些阅读。

注意:追加[ID]到表后,通过选择单个列将其转换为列表。