将 Excel 公式转换为 Power Query(如果有 countif)

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

我在下面的 D 列中有一个使用此公式的 Excel 公式。

=IF(COUNTIFS($B:$B,$B2,$C:$C,"Y")>0,"Y","N")

如何在电源查询中执行相同的操作?我知道使用 group by 可以在 countifs 上完成工作,但是如何将它们组合在一起作为 Power 查询中的新列?

enter image description here

excel powerquery
1个回答
0
投票

假设您的数据位于名为

Table1
的表中,您可以这样做

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    grouped = 
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Grouped Rows" = Table.Group(Source, {"Category", "Used?"}, {{"Count", each _, type table [ID=number, Category=text, #"Used?"=text, Category used=text, Column1=number]}}),
        #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([#"Used?"] = "Y")),
        #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Category Used", each "Y"),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count", "Used?"})
    in
        #"Removed Columns",
    joined = Table.NestedJoin(Source, {"Category"}, #"grouped", {"Category"}, "grouped", JoinKind.LeftOuter),
    #"Expanded grouped" = Table.ExpandTableColumn(joined, "grouped", {"Category Used"}, {"Category Used"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded grouped",null,"N",Replacer.ReplaceValue,{"Category Used"})

in
    #"Replaced Value"

Source
是原始表格
grouped
是按
Used?
分组的表格,然后仅筛选“Y” 之后,我在
Source
grouped
之间进行了左外连接,返回“Y”或 null。然后只需将 null 替换为 'N'

或者,您可以将

grouped
创建为函数并添加返回该值的列

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