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

Power BI OData数据源中为M查询添加参数的问题

问题分析与解决

错误原因

你遇到的Expression.Error: We cannot apply operator & to types Date and Text错误,核心是两个问题:

  1. 类型不匹配:DateTime.Date(ReportingDateFrom)返回的是Date类型值,而&只能用于字符串之间的拼接,直接把Date和字符串拼在一起就会触发类型错误。
  2. 拼接语法错误:你在拼接筛选条件时,错误地用了逗号分隔字符串片段和日期变量,正确的字符串拼接应该全程用&连接。

另外,OData对日期格式有严格要求,直接用地区格式(比如01/12/2022)可能导致解析失败,建议统一转成ISO 8601格式(yyyy-MM-dd)。

修正后的代码

把日期变量转成符合要求的字符串,并修正拼接语法,修改后的完整代码如下:

let
    Source = OData.Feed("https://analytics.dev.azure.com/zzz/xxx/_odata/v4.0-preview/WorkItems?"
    & "$filter=(WorkItemType eq 'User Story' or WorkItemType eq 'Bug' or WorkItemType eq 'Spike' or WorkItemType eq 'Show and tell') and CreatedDate ge '" & DateTime.ToText(ReportingDateFrom, "yyyy-MM-dd") & "' and State ne 'Removed' and State eq 'Closed' "
    & " and Descendants/any()"
    & "& $select=WorkItemId,Title,WorkItemType,State,Custom_CostCode,ChangedOn,Children"
    & "& $expand=AssignedTo($select=UserName),Iteration($select=IterationPath,StartDate,EndDate),ChangedOn($select=Date),Children($select=WorkItemId,Title,WorkItemType,State,CompletedWork;$expand=AssignedTo($select=UserName)),"
    & "Descendants("

    & "$apply=filter(WorkItemType eq 'Task' or WorkItemType eq 'Defect')"
    
    & "/aggregate(CompletedWork with sum as CompletedWork)"

    & ")",

    null, [Implementation="2.0",OmitValues = ODataOmitValues.Nulls,ODataVersion = 4]),
    #"Replaced Errors" = Table.ReplaceErrorValues(Source, {{"AssignedTo", "Unassigned"}})
in
    #"Replaced Errors"

关键修改点

  • 用DateTime.ToText(ReportingDateFrom, "yyyy-MM-dd")把日期参数转成OData兼容的字符串格式
  • 去掉原代码中"CreatedDate ge '"后面的逗号,改用&连接所有字符串片段,确保整个URL是一个完整的字符串

这样修改后,就能正确将日期参数代入OData筛选条件,避免类型错误,同时保证日期格式符合数据源要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:47:19