【问题标题】:How to handle null values in custom function?如何处理自定义函数中的空值?
【发布时间】:2022-12-12 14:44:21
【问题描述】:

我正在尝试添加一个自定义列以通过这种方式将 2 列(Col3 和 Col4)的值与自定义函数 fnMyFunction() 的结果组合起来

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

当函数处理空值时出现此错误

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

函数fnMyFunction是这样的:

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

我试图在 Input 步骤中添加 if else 以获取空作为函数的输出但不起作用

(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
    

怎么解决这个问题?

【问题讨论】:

    标签: powerbi powerquery m


    【解决方案1】:
    1. PQ 有一个空合并运算符??。尝试这个: Inputs = {input1 ?? "", input2 ?? ""}

    2. 一旦你修复了 fn,each... Combine([col3],[col4]..) 就会中断,因为你忘记了在 [col3] 和 [col4] 之前 _each..._ 是单参数函数的语法糖(在本例中是整行)。看这里:https://bengribaudo.com/blog/2017/12/08/4270/power-query-m-primer-part3-functions-function-values-passing-returning-defining-inline-recursion

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      相关资源
      最近更新 更多