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

PowerQuery多嵌套if语句报错:Token Eof expected求助

PowerQuery代码"Token Eof expected"错误修复及优化

原代码中的语法错误点

  • 缺少比较运算符:多处条件判断遗漏=,比如[DEP_PORT_CODE] "LAX"、[DEP_PORT_CODE] "LIS",正确写法应为[DEP_PORT_CODE] = "LAX"、[DEP_PORT_CODE] = "LIS"
  • 逻辑判断不完整:
    • [ARR_PORT_CODE] <> "AEP" or "EZE":or后未指定完整的比较逻辑,需补充为[ARR_PORT_CODE] <> "AEP" or [ARR_PORT_CODE] <> "EZE";若需求是到达机场既不是AEP也不是EZE,则应改为[ARR_PORT_CODE] <> "AEP" and [ARR_PORT_CODE] <> "EZE"
    • [FLIGHT_NO] <= 2000 or >= 3000:or后缺少左操作数,需补充为[FLIGHT_NO] <= 2000 or [FLIGHT_NO] >= 3000
  • 分支结构错误:
    • else if [DEP_PORT_CODE] = "MCO" then "MCO" then if:错误使用then if,应改为else if
    • then if "MEX" then "MEX":缺少[DEP_PORT_CODE] =,且结构错误,应为else if [DEP_PORT_CODE] = "MEX" then "MEX"
  • 类型不统一:最后else 0返回数字,而前面分支均返回字符串,建议统一为字符串类型,比如else ""

修正后的代码

if [DEP_PORT_CODE] = "AKL" then "AKL"
else if [DEP_PORT_CODE] = "BCN" then "BCN"
else if [DEP_PORT_CODE] = "BOG" then "BOG"
else if [DEP_PORT_CODE] = "BOS" then "BOS"
else if [DEP_PORT_CODE] = "CDG" then "CDG"
else if [DEP_PORT_CODE] = "CUN" then "CUN"
else if [DEP_PORT_CODE] = "FCO" then "FCO"
else if [DEP_PORT_CODE] = "FRA" then "FRA"
else if [DEP_PORT_CODE] = "GIG" and ([ARR_PORT_CODE] <> "AEP" and [ARR_PORT_CODE] <> "EZE") then "GRU-BOND"
else if [DEP_PORT_CODE] = "GIG" then "GIG"
else if [DEP_PORT_CODE] = "GRU" and ([FLIGHT_NO] >= 8000 or [FLIGHT_NO] <= 1400) then "GRU-BOND"
else if [DEP_PORT_CODE] = "GRU" then "GRU"
else if [DEP_PORT_CODE] = "GYE" then "GYE"
else if [DEP_PORT_CODE] = "JFK" then "JFK"
else if [DEP_PORT_CODE] = "LAX" then "LAX"
else if [DEP_PORT_CODE] = "LHR" then "LHR"
else if [DEP_PORT_CODE] = "LIM" and ([FLIGHT_NO] <= 2000 or [FLIGHT_NO] >= 3000) then "LIM-ROW"
else if [DEP_PORT_CODE] = "LIM" then "LIM-LP"
else if [DEP_PORT_CODE] = "LIS" then "LIS"
else if [DEP_PORT_CODE] = "MAD" then "MAD"
else if [DEP_PORT_CODE] = "MCO" then "MCO"
else if [DEP_PORT_CODE] = "MDE" then "MDE"
else if [DEP_PORT_CODE] = "MEX" then "MEX"
else if [DEP_PORT_CODE] = "MXP" then "MXP"
else if [DEP_PORT_CODE] = "SCL" then "SCL"
else if [DEP_PORT_CODE] = "UIO" then "UIO"
else ""

优化建议:使用Switch语句简化代码

对于这种多条件匹配的场景,PowerQuery(版本2.88及以上)支持switch语句,代码会更简洁易读:

switch true,
[DEP_PORT_CODE] = "AKL", "AKL",
[DEP_PORT_CODE] = "BCN", "BCN",
[DEP_PORT_CODE] = "BOG", "BOG",
[DEP_PORT_CODE] = "BOS", "BOS",
[DEP_PORT_CODE] = "CDG", "CDG",
[DEP_PORT_CODE] = "CUN", "CUN",
[DEP_PORT_CODE] = "FCO", "FCO",
[DEP_PORT_CODE] = "FRA", "FRA",
[DEP_PORT_CODE] = "GIG" and ([ARR_PORT_CODE] <> "AEP" and [ARR_PORT_CODE] <> "EZE"), "GRU-BOND",
[DEP_PORT_CODE] = "GIG", "GIG",
[DEP_PORT_CODE] = "GRU" and ([FLIGHT_NO] >= 8000 or [FLIGHT_NO] <= 1400), "GRU-BOND",
[DEP_PORT_CODE] = "GRU", "GRU",
[DEP_PORT_CODE] = "GYE", "GYE",
[DEP_PORT_CODE] = "JFK", "JFK",
[DEP_PORT_CODE] = "LAX", "LAX",
[DEP_PORT_CODE] = "LHR", "LHR",
[DEP_PORT_CODE] = "LIM" and ([FLIGHT_NO] <= 2000 or [FLIGHT_NO] >= 3000), "LIM-ROW",
[DEP_PORT_CODE] = "LIM", "LIM-LP",
[DEP_PORT_CODE] = "LIS", "LIS",
[DEP_PORT_CODE] = "MAD", "MAD",
[DEP_PORT_CODE] = "MCO", "MCO",
[DEP_PORT_CODE] = "MDE", "MDE",
[DEP_PORT_CODE] = "MEX", "MEX",
[DEP_PORT_CODE] = "MXP", "MXP",
[DEP_PORT_CODE] = "SCL", "SCL",
[DEP_PORT_CODE] = "UIO", "UIO",
""

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 23:25:18