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

如何解决Power Query自定义函数处理Null值的文本转换错误?

Power Query处理Null值导致的文本转换错误解决方案

问题场景

在Power Query中添加自定义列,通过Text.Combine合并Col3、Col4列值与自定义函数fnMyFunction()的结果,代码如下:

#"Added Custom" = Table.AddColumn(#"Previous Step", "Custom Column", 
     each 
      Text.Combine( 
        {
            [Col3],
            [Col4],
            fnMyFunction([Col5],[Col6])
         }
        )),

错误信息

当函数处理Null值时,触发以下错误:

Expression.Error: We cannot convert the value null to type Text.
Details:
    Value=
    Type=[Type]

函数现状

原自定义函数代码:

(input1 as text, input2 as text)=>
let
    Inputs = {input1, input2},
    SplitAndZip = List.Zip(List.Transform(Inputs, each Text.ToList(_))),
    OtherStep
    ...
    ..
    LastStep
in
    LastStep

尝试在函数内部添加if else逻辑处理Null,但未生效:

(input1 as text, input2 as text)=>
let
    Inputs = if input1 <> null then {input1, input2} else {"",""}, //Added "if else" here
    SplitAndZip = List.Zip(List.Transform(Inputs, each Text.ToList(_))),
    OtherSteps
    ...
    ..
    LastStep
in
    LastStep

问题根源

函数参数定义为input1 as text, input2 as text,Power Query会在调用函数前执行类型检查,Null不属于text类型,因此直接抛出错误,函数内部的处理逻辑根本不会被执行。这就是你添加的if else无效的原因。

解决方案

方案1:修改函数参数类型,允许Null并内部处理

将函数参数类型改为nullable text,允许传入Null,再在函数内部将Null转换为空文本后执行后续逻辑:

(input1 as nullable text, input2 as nullable text)=>
let
    // 先把Null转换为空文本
    CleanInput1 = if input1 = null then "" else input1,
    CleanInput2 = if input2 = null then "" else input2,
    Inputs = {CleanInput1, CleanInput2},
    SplitAndZip = List.Zip(List.Transform(Inputs, each Text.ToList(_))),
    // 保留原有的其他步骤
    OtherStep = ...,
    LastStep = ...
in
    LastStep

方案2:调用函数前提前处理Null值

如果不想修改函数参数类型,就在调用函数前,将Col5、Col6的Null转换为空文本,同时也要处理Col3、Col4的Null(否则Text.Combine也会因为Null报错):

#"Added Custom" = Table.AddColumn(#"Previous Step", "Custom Column", 
     each 
      Text.Combine( 
        {
            // 处理Col3的Null
            if [Col3] = null then "" else [Col3],
            // 处理Col4的Null
            if [Col4] = null then "" else [Col4],
            // 处理Col5、Col6的Null后再传入函数
            fnMyFunction(
                if [Col5] = null then "" else [Col5],
                if [Col6] = null then "" else [Col6]
            )
         }
        )),

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 17:01:23