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

Power Query自定义列拼接报错:Token 'else' expected,求排查

解决Power Query自定义列if语句的“Token 'else' expected”错误

我想要添加一个自定义列,通过if语句拼接多个列,但出现了“Token 'else' expected”的错误提示。Power Query将错误标记在末尾的最后一个括号处,代码如下:

if Text.Length([PRODUCT]) = 0 and Text.Length([[PROJECT_ID]]) = 0 then Text.Combine({Text.Start([Bud Type],1),[Dept],[ACCOUNT],[PROGRAM_CODE],"No Product","No Project"}) else

if Text.Length([PRODUCT]) >1 and Text.Length([[PROJECT_ID]]) >1 then Text.Combine({Text.Start([Bud Type],1),[Dept],[ACCOUNT],[PROGRAM_CODE],[PRODUCT],[PROJECT_ID]}) else 

if Text.Length([PRODUCT]) > 1 and ext.Length([[PROJECT_ID]]) = 0 then Text.Combine({Text.Start([Bud Type],1),[Dept],[ACCOUNT],[PROGRAM_CODE],[PRODUCT],"No Project"}) else

if Text.Length([PRODUCT]) = 0 and Text.Length([[PROJECT_ID]]) > 1 then Text.Combine({Text.Start([Bud Type],1),[Dept],[ACCOUNT],[PROGRAM_CODE],"No Product",[PROJECT_ID]})

错误原因及修正点:

  • 缺少最终else分支:Power Query要求if语句必须是完整的if-then-else结构,所有可能的场景都需要被覆盖。你的代码最后一个if没有对应的else兜底,导致语法报错。
  • 函数拼写错误:第三行的ext.Length是笔误,正确写法应为Text.Length。
  • 列引用格式错误:所有[[PROJECT_ID]]多了一层方括号,Power Query中引用列只需使用单层方括号[PROJECT_ID]。

修正后的完整代码:

if Text.Length([PRODUCT]) = 0 and Text.Length([PROJECT_ID]) = 0 then 
    Text.Combine({Text.Start([Bud Type],1), [Dept], [ACCOUNT], [PROGRAM_CODE], "No Product", "No Project"}) 
else if Text.Length([PRODUCT]) > 1 and Text.Length([PROJECT_ID]) > 1 then 
    Text.Combine({Text.Start([Bud Type],1), [Dept], [ACCOUNT], [PROGRAM_CODE], [PRODUCT], [PROJECT_ID]}) 
else if Text.Length([PRODUCT]) > 1 and Text.Length([PROJECT_ID]) = 0 then 
    Text.Combine({Text.Start([Bud Type],1), [Dept], [ACCOUNT], [PROGRAM_CODE], [PRODUCT], "No Project"}) 
else if Text.Length([PRODUCT]) = 0 and Text.Length([PROJECT_ID]) > 1 then 
    Text.Combine({Text.Start([Bud Type],1), [Dept], [ACCOUNT], [PROGRAM_CODE], "No Product", [PROJECT_ID]}) 
else 
    // 兜底分支,处理PRODUCT或PROJECT_ID长度为1等未覆盖的情况
    Text.Combine({Text.Start([Bud Type],1), [Dept], [ACCOUNT], [PROGRAM_CODE], 
        if Text.Length([PRODUCT]) = 1 then [PRODUCT] else "No Product", 
        if Text.Length([PROJECT_ID]) = 1 then [PROJECT_ID] else "No Project"})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 02:43:10