TransformColumns if null then pull data from another column

mduntley

Board Regular
Joined
May 23, 2015
Messages
134
Office Version
  1. 365
Platform
  1. Windows
I am trying to find a way so if there is a data in column 3 it will change it to null, but if the data is null, then get the value from column 2. I know I can add another column to complete this task, but I am trying to use the TransformColumns function more

Power Query:
let
    Source = #table({"Column 1", "Column 2", "Column 3"}, {{"439","772","748"},{"3241","1143","740"},{"1552",null,"531"},{"1308","2494","161"},{"3562","1527",null},{"2043","2162","513"},{"1749","1646","26"},{"3472",null,"427"},{null,"456",null}}),
    Custom1 = Table.TransformColumns(Source, {"Column 3", each if _ is null then [Column 2] else null})
in
    Custom1
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Table.TransformColumns isn't going to work for this example. It doesn't provide the row context to reference Column 2 while transforming Column 3. You can accomplish this with Table.TransformRows as below:

Power Query:
let
    Source = #table({"Column 1", "Column 2", "Column 3"}, {{"439","772","748"},{"3241","1143","740"},{"1552",null,"531"},{"1308","2494","161"},{"3562","1527",null},{"2043","2162","513"},{"1749","1646","26"},{"3472",null,"427"},{null,"456",null}}),
    Result = Table.FromRecords(Table.TransformRows(Source, each Record.TransformFields(_, {"Column 3", (x)=> if x=null then [Column 2] else null})))
in
    Result
 
Upvote 0
Table.TransformColumns isn't going to work for this example. It doesn't provide the row context to reference Column 2 while transforming Column 3. You can accomplish this with Table.TransformRows as below:

Power Query:
let
    Source = #table({"Column 1", "Column 2", "Column 3"}, {{"439","772","748"},{"3241","1143","740"},{"1552",null,"531"},{"1308","2494","161"},{"3562","1527",null},{"2043","2162","513"},{"1749","1646","26"},{"3472",null,"427"},{null,"456",null}}),
    Result = Table.FromRecords(Table.TransformRows(Source, each Record.TransformFields(_, {"Column 3", (x)=> if x=null then [Column 2] else null})))
in
    Result
What will if its be if the column 3 contain a specific text?
 
Upvote 0
Can be achieved also with a replace value, like below.
Power Query:
let
    Source = #table({"Column 1", "Column 2", "Column 3"}, {{"439","772","748"},{"3241","1143","740"},{"1552",null,"531"},{"1308","2494","161"},{"3562","1527",null},{"2043","2162","513"},{"1749","1646","26"},{"3472",null,"427"},{null,"456",null}}),
    #"Replaced Value" = Table.ReplaceValue(Source,each [Column 3],each if [Column 3] = null then [Column 2] else null,Replacer.ReplaceValue,{"Column 3"})
   
in
    #"Replaced Value"

Adjust in this part here if [Column 3] = null then [Column 2] else null, to your needs
 
Upvote 0

Forum statistics

Threads
1,215,151
Messages
6,123,321
Members
449,094
Latest member
Chestertim

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