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

如何在M代码函数中设置正确返回类型,解决Binary转Table报错问题

你遇到的报错是因为Web.Contents函数默认返回的是Binary二进制类型,但你的函数声明返回值为table类型,类型不匹配才触发转换失败错误。以下是两种可行的解决方案:

方案1:保留Web.Contents,手动转换二进制为表

OData接口返回的是标准JSON格式,你需要对Web.Contents的返回结果做多层转换,最终匹配table返回类型:

(getInvoiceLines as text,FromYear as text,ToYear as text,FromMonth as text,ToMonth as text, ToDay as text) as table =>

let
      // 调用接口获取二进制结果
      ResponseBinary = Web.Contents("https://odata.uniconta.com/odata/DebtorInvoiceLines?$filter=Date ge datetime'"&FromYear&"-"&FromMonth&"-01T00:00:00' and Date le datetime'"&FromYear&"-"&FromMonth&"-"&ToDay&"T00:00:00'", [Headers=[Authorization = getInvoiceLines ]]),
      // 二进制转文本
      ResponseText = Text.FromBinary(ResponseBinary),
      // 解析为JSON对象
      JsonResult = Json.Document(ResponseText),
      // 提取OData返回的value节点(存储实际业务数据)并转为表
      Kilde = Table.FromRecords(JsonResult[value])
in
    Kilde

如果需要展开嵌套字段,可以在后续步骤添加Table.ExpandRecordColumn操作即可。

方案2:启用注释的OData.Feed(更推荐)

OData.Feed是Power Query为OData接口封装的专用请求函数,本身就会自动解析返回内容为Table类型,不需要手动做类型转换,适配性更高:

(getInvoiceLines as text,FromYear as text,ToYear as text,FromMonth as text,ToMonth as text, ToDay as text) as table =>

let
      Kilde = OData.Feed("https://odata.uniconta.com/odata/DebtorInvoiceLines?$filter=Date ge datetime'"&FromYear&"-"&FromMonth&"-01T00:00:00' and Date le datetime'"&FromYear&"-"&FromMonth&"-"&ToDay&"T00:00:00'", null, [Headers=[Authorization = getInvoiceLines ]])
in
    Kilde

如果调用时出现权限提示,直接在Power Query的凭据设置里,将该站点的访问权限设置为「匿名」即可,因为你已经在请求头里携带了Authorization鉴权参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 16:45:03