为什么这个 Power Query 操作需要这么长时间才能完成?
Why does this Power Query operation take so much time to complete?
我从@teylyn 那里得到了一些帮助,让这段代码工作。
关于查询的操作时间,我 运行 遇到了更多麻烦。
任务:
任务是将两个单独的联系人列表合并为一个全新的列表。
我还需要删除共享相同名称和公司名称的重复项。
最后,我需要删除共享相同 CompanyID 但公司名称不同的联系人。应保留主要列表中的联系人,以防发生冲突。
下面的代码有效,但需要很长时间才能完成。
该列表总共包含大约 8500 个联系人。
遍历每个联系人需要 2.5 秒,加起来大约需要 6 小时才能完成。
我的问题:
为什么这个操作要花这么长时间,有什么办法可以让它更快吗?
let
Source = Table.Combine({PrimaryContacts, SecondaryContacts}),
#"Removed duplicates" = Table.Distinct(Source, {"CompanyID", "FirstName", "LastName"}),
#"Sorted rows" = Table.Sort(#"Removed duplicates",{{"CompanyID", Order.Ascending}, {"Email", Order.Descending}}),
#"Filtered rows" = Table.SelectRows(#"Sorted rows", each ["FirstName"] <> null and ["FirstName"] <> ""),
#"Added index" = Table.AddIndexColumn(#"Filtered rows", "Index", 10000, 1),
#"Renamed columns" = Table.RenameColumns(#"Added index",{{"Index", "ContactID"}}),
#"Reordered columns" = Table.ReorderColumns(#"Renamed columns",{"ContactID", "CompanyID", "CompanyName", "FirstName", "LastName}), // I have removed 10 columns for privacy reasons
#"Added index1" = Table.AddIndexColumn(#"Reordered columns", "Index", 0, 1),
#"Filtered rows1" = Table.SelectRows(#"Added index1", each (["ContactID"] = 10000 or ["ContactID"] = 10001 or ["ContactID"] = 10002 or ["ContactID"] = 10003 or ["ContactID"] = 10004 or ["ContactID"] = 10005)),
/* The filter above is temporary to be able to check if next step works */
/* It is the step below that takes too much time to finish */
#"Add custom" = Table.AddColumn(#"Filtered rows1", "Delete", each if ["CompanyID"]= #"Reordered columns"{[Index]-1}["CompanyID"] and ["CompanyName"]<> #"Reordered columns"{[Index]-1}["CompanyName"] then "Delete" else null)
in
#"Add custom"
第一行可能有问题,其中 [Index] = 0 所以 [Index] - 1 = -1 超出范围。
一般来说,使用行索引引用 table 行的效率不是很高。
相反,最好添加 2 个索引列:1 个以 0 开头,另一个以 1 开头。接下来将 table 与其自身合并,使用基于 0 的索引作为第一个 table 的键和基于 1 的索引作为第二个 table 的键。结果,您得到一个嵌套 table 的列,其中包含前一行的数据。如果将此列命名为 "Previous" 并展开所需的列,使用原始名称作为前缀,则当前行的字段与前一行的字段(前缀为 "Previous.")相邻其他并从那里开始。
我从@teylyn
关于查询的操作时间,我 运行 遇到了更多麻烦。
任务:
任务是将两个单独的联系人列表合并为一个全新的列表。
我还需要删除共享相同名称和公司名称的重复项。
最后,我需要删除共享相同 CompanyID 但公司名称不同的联系人。应保留主要列表中的联系人,以防发生冲突。
下面的代码有效,但需要很长时间才能完成。
该列表总共包含大约 8500 个联系人。
遍历每个联系人需要 2.5 秒,加起来大约需要 6 小时才能完成。
我的问题:
为什么这个操作要花这么长时间,有什么办法可以让它更快吗?
let
Source = Table.Combine({PrimaryContacts, SecondaryContacts}),
#"Removed duplicates" = Table.Distinct(Source, {"CompanyID", "FirstName", "LastName"}),
#"Sorted rows" = Table.Sort(#"Removed duplicates",{{"CompanyID", Order.Ascending}, {"Email", Order.Descending}}),
#"Filtered rows" = Table.SelectRows(#"Sorted rows", each ["FirstName"] <> null and ["FirstName"] <> ""),
#"Added index" = Table.AddIndexColumn(#"Filtered rows", "Index", 10000, 1),
#"Renamed columns" = Table.RenameColumns(#"Added index",{{"Index", "ContactID"}}),
#"Reordered columns" = Table.ReorderColumns(#"Renamed columns",{"ContactID", "CompanyID", "CompanyName", "FirstName", "LastName}), // I have removed 10 columns for privacy reasons
#"Added index1" = Table.AddIndexColumn(#"Reordered columns", "Index", 0, 1),
#"Filtered rows1" = Table.SelectRows(#"Added index1", each (["ContactID"] = 10000 or ["ContactID"] = 10001 or ["ContactID"] = 10002 or ["ContactID"] = 10003 or ["ContactID"] = 10004 or ["ContactID"] = 10005)),
/* The filter above is temporary to be able to check if next step works */
/* It is the step below that takes too much time to finish */
#"Add custom" = Table.AddColumn(#"Filtered rows1", "Delete", each if ["CompanyID"]= #"Reordered columns"{[Index]-1}["CompanyID"] and ["CompanyName"]<> #"Reordered columns"{[Index]-1}["CompanyName"] then "Delete" else null)
in
#"Add custom"
第一行可能有问题,其中 [Index] = 0 所以 [Index] - 1 = -1 超出范围。
一般来说,使用行索引引用 table 行的效率不是很高。 相反,最好添加 2 个索引列:1 个以 0 开头,另一个以 1 开头。接下来将 table 与其自身合并,使用基于 0 的索引作为第一个 table 的键和基于 1 的索引作为第二个 table 的键。结果,您得到一个嵌套 table 的列,其中包含前一行的数据。如果将此列命名为 "Previous" 并展开所需的列,使用原始名称作为前缀,则当前行的字段与前一行的字段(前缀为 "Previous.")相邻其他并从那里开始。