You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power Query中基于公共列全外连接多子表格的实现方法及大量API查询性能优化咨询

Power Query中基于公共列全外连接多子表格的实现方法及大量API查询性能优化咨询

嗨,我来帮你梳理下这两个问题的解决方案~

一、多子表格基于公共列的全外连接实现

你已经找到的List.Accumulate思路完全正确!因为Table.Join确实只支持两个表格,所以通过迭代累积的方式把列表里的表格逐个连接起来是最直接的方案。我来拆解下你当前代码里的核心逻辑:

let
    Source = Excel.CurrentWorkbook(){[Name="api_requests"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Syntax", type text}, {"protocol", type text}, {"wsEntryPoint", type text}, {"resource", type text}, {"flowRef", type text}, {"key", type text}, {"parameters", type text}, {"Full API request", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Target Sheet] = "Kreditzinsen")),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Custom", each Csv.Document(Web.Contents([Full API request], [Timeout=#duration(0,0,60,0)]),[Delimiter=";", Columns=3, QuoteStyle=QuoteStyle.None])),
    tables_with_headers = Table.AddColumn(#"Added Custom", "step2", each Table.PromoteHeaders([Custom], [PromoteAllScalars=true])),
    tables_without_empty_key = Table.AddColumn(tables_with_headers, "step3", each Table.SelectRows([step2], each ([#""] <> ""))),
    tables = Table.SelectColumns(tables_without_empty_key,{"step3"}),
    Custom = tables[step3],
    // 核心:用List.Accumulate迭代连接所有表格
    combined_results = List.Accumulate(Custom, 0, (state, current) => 
        if state = 0 then current 
        else Table.RemoveColumns(
            Table.Join(
                Table.RenameColumns(state, {"", "Col1"}), 
                "Col1", 
                current, 
                "", 
                JoinKind.FullOuter
            ), 
            "Col1"
        )
    ),
    RemovedColumnList = List.Select(Table.ColumnNames(combined_results), each Text.EndsWith(_, "_FLAGS")),
    RemovedColumns = Table.RemoveColumns(combined_results, RemovedColumnList)
in
    RemovedColumns

核心逻辑解析:

  • Custom = tables[step3]:把列里的所有子表格提取成一个列表,这是后续迭代的数据源。
  • List.Accumulate的作用是从左到右遍历列表,逐步累积结果:
    • 初始状态设为0,第一次迭代时直接把第一个表格作为初始结果;
    • 后续每次迭代,先把累积的结果表格(state)的空列重命名为Col1,然后和当前表格(current)基于原空列(current的""列)做全外连接;
    • 连接完成后移除临时的Col1列,保持列名的一致性。

如果想让代码更易读,也可以把初始状态改成null,逻辑会更直观:

combined_results = List.Accumulate(Custom, null, (state, current) => 
    if state = null then current 
    else Table.RemoveColumns(
        Table.Join(
            Table.RenameColumns(state, {"", "Col1"}), 
            "Col1", 
            current, 
            "", 
            JoinKind.FullOuter
        ), 
        "Col1"
    )
)

二、大量API查询的性能优化建议

你提到Power Query处理大量API请求慢甚至卡住,这确实是常见问题,给你几个实用的优化方向:

  • 批量请求减少调用次数:如果API支持批量查询参数,尽量把多个请求合并成一个,能大幅减少网络开销。比如有些API允许用ids=1,2,3这种方式一次获取多条数据。
  • 缓存API响应结果:如果相同的API请求会重复执行,可以用Table.CacheResult把结果缓存起来,或者把请求结果导出到本地表格,下次直接读取本地数据,避免重复调用API。
  • 尝试并行请求:Power Query默认是串行执行API请求的,你可以用List.ParallelInvoke函数来并行处理请求列表,不过要注意API的速率限制,别触发反爬机制。
  • 提前过滤和精简数据:在调用API之前,先过滤掉不需要的行(比如你已经做了#"Filtered Rows"),只保留必要的列,减少后续数据处理的压力。
  • 调整数据加载策略:如果最终只需要部分数据,可以在API请求后立即筛选列和行,不要等到全部连接完成后再处理。
  • 检查API超时和速率限制:有些API会限制每分钟/小时的请求次数,超过后会变慢或返回错误,你可以在Web.Contents里添加延迟(比如用Function.InvokeAfter),或者联系API提供商调整配额。

备注:内容来源于stack exchange,提问作者Joseph Doob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 15:24:49