简体   繁体   English

Excel Power Query - 计算匹配多列的数量

[英]Excel Power Query - Count number of matching multiple columns

I have a datasource from an external Excel file that I have added to an Excel worksheet.我有一个来自已添加到 Excel 工作表的外部 Excel 文件的数据源。 I need to add new custom columns that compare the data to a table ("My_Table") in another worksheet that is manually updated.我需要添加新的自定义列,将数据与手动更新的另一个工作表中的表(“My_Table”)进行比较。 I used the Power Query Editor and created a new column that checks if there is a matching entry in My_Table based on matching 3 columns and gives a True/False result (ie for each row of the datasource, if the acctName, projectName, and boardName match a corresponding row in My_Table, then it returns true):我使用 Power Query 编辑器并创建了一个新列,该列根据匹配的 3 列检查 My_Table 中是否有匹配条目并给出真/假结果(即对于数据源的每一行,如果 acctName、projectName 和 boardName匹配 My_Table 中的相应行,则返回 true):

#"Added Custom" = Table.AddColumn(#"Reordered Columns", "Tracked", each Table.Contains( My_Table, [Customer=[acctName], Project=[projectName], Board=[boardName]]))

What I would like to do now is do the exact same thing but count how many times those three columns match in "My_Table".我现在想做的是做完全相同的事情,但计算这三列在“My_Table”中匹配的次数。 I thought Tabel.RowCount would work but I'm not sure if that's the right way to do it as I either have an error or a zero result.我认为 Tabel.RowCount 会起作用,但我不确定这是否是正确的方法,因为我要么有错误要么结果为零。

dolomike, Here's another shot at it...多洛米克,这是另一个镜头......

I started with this as Table1:我从表 1 开始:

在此处输入图片说明

...and this as My_Table: ...这作为My_Table:

在此处输入图片说明

...and used this M code: ...并使用此 M 代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Merged Queries" = Table.NestedJoin(Source, {"acctName", "projectName", "boardName"}, My_Table, {"Customer", "Project", "Board"}, "My_Table", JoinKind.LeftOuter),
    #"Expanded My_Table" = Table.ExpandTableColumn(#"Merged Queries", "My_Table", {"Customer", "Project", "Board"}, {"My_Table.Customer", "My_Table.Project", "My_Table.Board"}),
    #"Grouped Rows" = Table.Group(#"Expanded My_Table", {"My_Table.Customer", "My_Table.Project", "My_Table.Board"}, {{"Count", each Table.RowCount(_), type number}, {"AllData", each _, type table [acctName=text, projectName=text, boardName=text, My_Table.Customer=text, My_Table.Project=text, My_Table.Board=text]}}),
    Custom2 = Table.TransformColumns(#"Grouped Rows",{"Count", each if _ = List.Max(#"Grouped Rows"[Count]) then 0 else _}),
    #"Removed Other Columns" = Table.SelectColumns(Custom2,{"Count", "AllData"}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Removed Other Columns", "AllData", {"acctName", "projectName", "boardName", "My_Table.Customer", "My_Table.Project", "My_Table.Board"}, {"acctName", "projectName", "boardName", "My_Table.Customer", "My_Table.Project", "My_Table.Board"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Expanded AllData",{"Count", "acctName", "projectName", "boardName"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Other Columns1",{"acctName", "projectName", "boardName", "Count"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"acctName", "Customer"}, {"projectName", "Project"}, {"boardName", "Board"}}),
    #"Removed Duplicates" = Table.Distinct(#"Renamed Columns")
in
    #"Removed Duplicates"

...to get this result: ...得到这个结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM