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

函数参数报错:预期1-2个参数却传入3个?求技术排查

问题排查:API批量拉取数据时的参数错误

问题场景

需要从无offset参数的公共API批量拉取数据,采用按月指定fromDate/toDate的方式替代单ID调用,但执行时触发错误:

Expression.Error: 3 arguments were passed to a function which expects between 1 and 2. Details: Pattern= Arguments=[List]
错误指向函数定义行getDataForDateRange = (start as date, end as date) =>,无法理解为何提示传入3个参数。

错误根源

问题出在List.Generate的**第二个参数(终止条件判断)**里:

(date) => List.Max(Date.AddDays(getDataForDateRange(date, toDate)[Date], 1))

这里的Date.AddDays函数要求第一个参数是单个日期值,但你传入的getDataForDateRange(date, toDate)[Date]是一个日期列表。Power Query会把列表拆成多个参数传递,相当于给Date.AddDays传了N+1个参数(N是列表元素数),最终触发参数数量不匹配的错误,错误提示里的Arguments=[List]就是这个列表被当成多参数传入的证据。

同时还有两个隐性逻辑错误:

  • List.Generate的终止条件应该返回布尔值(true/false),你现在返回日期值会被强制转成布尔值,导致循环逻辑混乱。
  • 循环中updatedResult = Table.Combine({result, dateRangeData})的result是初始空表,每次循环只会合并当前数据和空表,无法累积之前的结果。

修复方案

1. 修正List.Generate的终止条件

改成判断当前日期是否在范围内,同时检查API是否返回有效数据:

(state) => state[currentDate] <= toDate and List.Count(getDataForDateRange(state[currentDate], toDate)) > 0

2. 修正日期递进逻辑

通过API返回数据的最大日期,计算下一次查询的起始日期,避免重复拉取:

(state) => 
    let
        currentData = getDataForDateRange(state[currentDate], toDate),
        maxDate = List.Max(currentData[Date]),
        nextDate = Date.AddDays(maxDate, 1)
    in
        nextDate

3. 修复结果累积逻辑

用List.Generate的状态变量保存累积的结果,替代外部固定的空表:

  • 初始状态包含当前查询日期和空累积表
  • 每次循环更新累积表和下一次查询日期

完整修复代码

let
    // Set the API URL
    apiUrl = my_API_URL,

    // Set the credentials
    user = User,
    key = Key,
    secret = Secret,

    // Set the initial parameters
    fromDate = #date(2018, 1, 1),
    toDate = #date(2018, 1, 31),
    limit = 10000,

    // Helper function to retrieve data for a specific date range
    getDataForDateRange = (start as date, end as date) =>
        let
            // Convert the dates to the required format (YYYY-MM-DD)
            formattedStartDate = Text.From(start, "yyyy-MM-dd", "en-US"),
            formattedEndDate = Text.From(end, "yyyy-MM-dd", "en-US"),

            // Construct the API URL with the parameters
            url = apiUrl & "?limit=" & Text.From(limit) & "&fromDate=" & formattedStartDate & "&toDate=" & formattedEndDate,

            // Invoke the API and retrieve the data as a list of records
            response = Web.Contents(url, [Headers = [User = user, Key = key, Secret = secret]]),
            data = Json.Document(response, type table [Record = record]),
            records = data[Record]
        in
            records,

    // Use List.Generate with state to accumulate results
    loop = List.Generate(
        // Initial state: current start date + empty accumulated table
        () => [currentDate = fromDate, accumulated = Table.FromRecords({})],
        // Termination condition: stop when current date exceeds toDate or no data returned
        (state) => state[currentDate] <= toDate and List.Count(getDataForDateRange(state[currentDate], toDate)) > 0,
        // Next state: update current date and accumulated table
        (state) => 
            let
                currentData = getDataForDateRange(state[currentDate], toDate),
                maxDate = List.Max(currentData[Date]),
                nextDate = Date.AddDays(maxDate, 1),
                updatedAccumulated = Table.Combine({state[accumulated], Table.FromRecords(currentData)})
            in
                [currentDate = nextDate, accumulated = updatedAccumulated],
        // Extract the accumulated table from each state
        (state) => state[accumulated]
    ),

    // Get the final accumulated table
    finalResult = if List.Count(loop) > 0 then List.Last(loop) else Table.FromRecords({})
in
    finalResult

内容的提问来源于stack exchange,提问作者fatau

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 22:05:15