I want to combine two columns, say Column1 and Column2 of type any. I think I have three options:
These three approaches are illustrated in the code below.
As far as I can tell, the only way I can get the behavior I want is by using the custom function shown.
So, my question is:
Is there a way to force either Text.Combine or Table.CombineColumns to return null when attempting to combine null with null?
let
Source = #table(
{"Column1","Column2"},
{
{"1",null},
{null,"2"},
{null,null}
}
),
//Merging with Text.Combine makes null and null into ""
merge_using_text_combine = Table.AddColumn(
Source,
"MergeWithTextCombine",
each Text.Combine({Column1,Column2},"")
),
//Merging with Table.CombineColumns - but row three becomes "", not null
merged = Table.CombineColumns(
Source,
{"Column1","Column2"},
Combiner.CombineTextByDelimiter("",QuoteStyle.None),
"MergeWithTableCombineColumns"
),
//function where null & null = null
custom_combine = (columnX as any,columnY as any,optional sep as nullable text) =>
if
columnX is null and columnY is null
then
null
else
Text.Combine(
{columnX,columnY},
if sep = null then "" else sep
),
//Combine using a custom function
merge_using_custom_function = Table.AddColumn(
Source,
"UsingCustomFunction",
each custom_combine([Column1],[Column2])
)
//,
//blah = Text.Combine({null,null}), //produces text
//blurp = null & null, //produces null, but so does 1 & null and so does null & 2
//blahblah = Value.Is(blah,Text.Type) //TRUE
in
merge_using_custom_function
Not sure if this is what you want, but I first tested [Column1] and [Column2] equal to null before combining them. Note that I added a new line {"1","2"} to test it as well:
let
Source = #table(
{"Column1","Column2"},
{
{"1",null},
{null,"2"},
{"1","2"},
{null,null}
}
),
merge_using_text_combine_ListAllTrue = Table.AddColumn(Source, "MergeWithTextCombine", each if List.AllTrue({[Column1]=null,[Column2]=null}) then null else Text.Combine({[Column1],[Column2]},""))
in
merge_using_text_combine_ListAllTrue