Forum Discussion

AC246's avatar
AC246
Frequent Visitor
1 year ago
Solved

Conditional M statement to concatenate two columns with additional text only where both are not null

I'm an 'M' novice hoping for your kind help with a Power Query issue. I have some data in which each row may have one, two, or no identifiers, currently housed in two separate columns (let's call the...
  • v-saisrao-msft's avatar
    1 year ago

    Hi AC246,

    I have replicated your scenario using the sample data provided. Below is the output I obtained, and I have also attached the pbix file for your reference.


    Hope this helps
    Thank you.

  • AC246's avatar
    AC246
    1 year ago

    Thank you for this, it's very helpful!

    In the meantime, some attempts with ChatGPT yielded a statement which also works quite well:

     

    = Table.AddColumn(#"Renamed Columns1", "Combined identifiers", each
    let
    id1 = Text.Trim(Text.From([#"Data source.Identifier 1"])),
    id2 = Text.Trim(Text.From([#"Data source.Identifier 2"]))
    in
    if id1 <> "" and id2 <> "" then
    id1 & ", " & id2
    else if id1 <> "" then
    id1
    else if id2 <> "" then
    id2
    else
    null
    )