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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
amien
Helper V
Helper V

Conditional merge columns in Query Editor

I have this table:

Field1

Field2

Field3

1

 

 

 

5

 

 

 

2

 

I want to column merge these 3 into 1 field and put a prefix in the new column:

 

Field1

Field2

Field3

Field4

1

 

 

Field1_1

 

5

 

Field2_5

 

 

2

Field3_2

 

I was trying something like this : If(Field1 > 0,"Field1_" & [Field1], If(Field2 > 0,"Field2_"  & [Field2]

 

Thanks in advanced

 

1 ACCEPTED SOLUTION
LarsSchreiber
Responsive Resident
Responsive Resident

Hi @amien,

 

premising that your data come from Excel, from a table that is called "table1" use the following M code:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Field1", Int64.Type}, {"Field2", Int64.Type}, {"Field3", Int64.Type}}),
    Replace = Table.ReplaceValue(ChangedType,null,0,Replacer.ReplaceValue,{"Field1", "Field2", "Field3"}),
    AddColumn = Table.AddColumn(Replace, "Field4", each if [Field1]>0 then 
		                                                  "Field1_" & Number.ToText([Field1]) 
		                 		                        else if [Field2]>0 then 
						                                   "Field2_" & Number.ToText([Field2]) 
						                                else 
						                                   "Field3_" & Number.ToText([Field3]) 
	           )								   
in
    AddColumn

Hope that helps,

Regards,

Lars

View solution in original post

2 REPLIES 2
LarsSchreiber
Responsive Resident
Responsive Resident

Hi @amien,

 

premising that your data come from Excel, from a table that is called "table1" use the following M code:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Field1", Int64.Type}, {"Field2", Int64.Type}, {"Field3", Int64.Type}}),
    Replace = Table.ReplaceValue(ChangedType,null,0,Replacer.ReplaceValue,{"Field1", "Field2", "Field3"}),
    AddColumn = Table.AddColumn(Replace, "Field4", each if [Field1]>0 then 
		                                                  "Field1_" & Number.ToText([Field1]) 
		                 		                        else if [Field2]>0 then 
						                                   "Field2_" & Number.ToText([Field2]) 
						                                else 
						                                   "Field3_" & Number.ToText([Field3]) 
	           )								   
in
    AddColumn

Hope that helps,

Regards,

Lars

Thanks Lars

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.