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

Power Query/M:如何控制运算顺序实现双OR条件的逻辑判断

Power Query/M 条件替换逻辑修正方案

问题根源

原代码未对逻辑判断进行括号分组,Power Query/M中and的运算优先级高于or,导致原本要求的**"(Country为空/空白) AND (Org Country为空/空白)"**逻辑被错误解析,最终触发错误替换。此外代码还存在语法错误(多余引号、else分支错误)。

修正后的代码方案

方案一:用Let块拆分逻辑(可读性更高)

Table.ReplaceValue(
    #"Previous step",
    each [Country],
    each 
        let
            countryEmpty = [Country] = null or Text.Trim([Country]) = "",
            orgCountryEmpty = [#"Org Country"] = null or Text.Trim([#"Org Country"]) = ""
        in
            if countryEmpty and orgCountryEmpty then "[NOT LISTED]" else [Country],
    Replacer.ReplaceValue,
    {"Country"}
)

方案二:直接括号分组(和SQL逻辑完全一致)

Table.ReplaceValue(
    #"Previous step",
    each [Country],
    each if ([Country] = null or Text.Trim([Country]) = "") and ([#"Org Country"] = null or Text.Trim([#"Org Country"]) = "")
         then "[NOT LISTED]"
         else [Country],
    Replacer.ReplaceValue,
    {"Country"}
)

关键修正点

  1. 括号分组控制运算顺序:用()将每列的null/空白判断逻辑包裹,强制先执行or运算,再执行外层的and,实现预期的双条件同时满足逻辑。
  2. 修复语法错误:移除[Country"]中的多余引号,修正else分支为[Country](保留原有效值,而非错误设为null)。
  3. 逻辑明确化:拆分或包裹逻辑后,避免因运算符优先级导致的逻辑歧义。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 00:33:30