1

I'm trying to add a custom column to combine values of 2 columns (Col3 and Col4) with the result of a custom function fnMyFunction() in this way

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

I'm getting this error when function handles null values

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

The function fnMyFunction is like this:

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

I've tried to add the if else in Input step in order to get empty as output for the function but doesn't work

(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
    

How can be fix this?

3 Answers 3

3

Change your function definition to be the following:

(optional input1 as text, optional input2 as text)=>
Sign up to request clarification or add additional context in comments.

1 Comment

See Optional Parameters section bengribaudo.com/blog/2017/11/28/4199/…
0
  1. PQ has a null-coalesce operator ??. Try this: Inputs = {input1 ?? "", input2 ?? ""}

edit from the future - #2 is wrong. My bad. Still, read Ben's guide. It's the best PQ text-book there is.

  1. Once you fix the fn, each... Combine([col3],[col4]..) will break because you forgot to _ before [col3] and [col4]. each..._ is syntactic sugar for a single-argument function (eating a whole row, in this case). See here: https://bengribaudo.com/blog/2017/12/08/4270/power-query-m-primer-part3-functions-function-values-passing-returning-defining-inline-recursion

2 Comments

Thanks for answer. I´ve added what you suggested Inputs = {input1 ?? "", input2 ?? ""} but still I get the error. I didn't understand what to do for what you mentioned in item 2.
Yup, my bad on the _[ ]. PQ will try and access the column value from the record that is passed to it in a general namespace. So _[col3] = [col3]
0

Use

each try Text.Combine() otherwise ""

2 Comments

I tried you suggestion but I got error like TRY and OTHERWISE are not understood
That's because try and otherwise need to be lowercase

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.