Combining two Columns in Power Query Based on a Condition on another column

ReDDozer

New Member
Joined
Feb 11, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all,

the following code combines 2 columns ColumnB1 and ColumnB2 based on the condition if ColumnB1 = null then ColumnB2 else ColumnB1:

Table.CombineColumns ( PreviousStep,{"ColumnB1", "ColumnB2"}, each if _{0} = null then _{1} else _{0}, "ColumnB")

is there a way to edit the code in order the condition to be based on another column, not part of the merge? if ColumnA = null then ColumnB2 else ColumnB1.

Thank you
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    nCol = Table.AddColumn(Source, "Merged", each if [ColumnA] = null then [ColumnB2] else [ColumnB1]),
    dCols = Table.RemoveColumns(nCol,{"ColumnB1", "ColumnB2"})
in
    dCols

Book1
ABCDEF
1ColumnAColumnB1ColumnB2ColumnAMerged
2aehae
3fii
4gjj
5dekde
Sheet1
 
Upvote 0
Hi JET,

thank you for your response.

I was hoping there was a way to solve the problem in two steps via the Table.CombineColumns function and without the need to add an extra column.

can you confirm that it is not possible to solve it with Table.CombineColumns?

thank you
 
Upvote 0
I don't think it's possible with just using CombinColumns.
Like below you won't notice the extra step. Although I like your urge to do it with less steps/code. Motto: More with less

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    dCols = Table.RemoveColumns(Table.AddColumn(Source, "Merged", each if [ColumnA] = null then [ColumnB2] else [ColumnB1]),{"ColumnB1", "ColumnB2"})
in
    dCols
 
Upvote 0
Solution
Great, thanks for the feedback!
eheheh yes, "do more with less" it's definitely my motto!
Cheers
 
Upvote 0
Here's a way using Table.CombineColumns. I don't think it has any advantage over JEC's method though.

Power Query:
let
    t = Table.FromRows({{"a","e","h"},{null,"f","i"},{null,"g","j"},{"d","e","k"}}, {"ColumnA","ColumnB1","ColumnB2"}),
    Result = Table.FromRecords(Table.CombineColumns(t, Table.ColumnNames(t), (x)=> [ColumnA = x{0}, Merged = if x{0} = null then x{2} else x{1}], "r")[r])
in
    Result
 
Upvote 0

Forum statistics

Threads
1,223,967
Messages
6,175,674
Members
452,666
Latest member
AllexDee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top