Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
AC246
Frequent Visitor

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 them 'Identifier 1' and 'Identifier 2'), as per the below screenshot example.

 

Example data.png

I'm trying to create a new column which lists all available identifiers for each row, with multiple values separated by a comma and space where relevant. Or in other words:

 

  • If (Identifier 1 column) has a value AND (Identifier 2 column) has a value, then return the text string "[Identifier 1 value], [Identifier 2]".
  • If only (Identifier 1 column) has a value, then return [Identifier 1 value].
  • If only (Identifier 2 column) has a value, then return [Identifier 2 value].
  • If neither column has a value, return null.

I've been trying to write this as an 'If, then' statement but that first scenario, checking if both columns have a value, seems to be causing problems. I keep getting values in my final column along the lines of ", " or ", [Identifier 2 value]", so my M statement seems to be applying the text string output to any scenario, not just to the scenario I want (both Identifier 1 and Identifier 2 having a value).

 

Might anyone have ideas to help with this? Additionally, if you have the time, a quick explanation as to why/how your solution works (or a link to further information) would be helpful just to assist me in learning. Thank you!

 

My draft statement looks something like this:

 

= Table.AddColumn(#"Renamed Columns1", "Combined identifiers list", each if ([#"Source Name.Identifier 1"] <> null and [#"Source Name.Identifier 2"] <> null) then [#"Source Name.Identifier 1"] & ", " & [#"Source Name.Identifier 2"] else if [#"Source Name.Identifier 1"] <> null then [#"Source Name.Identifier 1"] else if [Source Name.Identifier 2] <> null then [Source Name.Identifier 2] else null)

2 ACCEPTED SOLUTIONS

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.

vsaisraomsft_0-1755170375282.png


Hope this helps
Thank you.

View solution in original post

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
)

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Combine(List.Transform(List.RemoveNulls(Record.ToList(_)), each Text.From(_)),", "))
in
    #"Added Custom"

Hope this helps.

Ashish_Mathur_0-1755215836866.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
pankajnamekar25
Super User
Super User

Hello @AC246 

 

Try this M code

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

Thank you for this code; however unfortunately I'm still encountering issues in the results. I'm still getting just ", " in the output column in some rows, along with some ", 1234" and "5678, " type values. I'm not sure if I'm failing to properly translate your logic into my own statement, or if there's still something else going on.

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.

vsaisraomsft_0-1755170375282.png


Hope this helps
Thank you.

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
)

Hi @AC246,

Thank you for sharing the method you tried. By converting and trimming the values first, it consistently manages nulls and blanks, making the logic more dependable.

 

Thank you

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.