PQ Column Transformation Based on Other Column

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
In Power Query, I would like to perform transformations on certain columns that are dependent on the value in another column. For example, in this table,

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Value
[/TD]
[TD]Col1
[/TD]
[TD]Col2
[/TD]
[TD]Col3
[/TD]
[TD]Col4
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]aaa
[/TD]
[TD]123
[/TD]
[TD]456
[/TD]
[TD]789
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]bbb
[/TD]
[TD]123
[/TD]
[TD]456
[/TD]
[TD]789
[/TD]
[/TR]
[TR]
[TD]C
[/TD]
[TD]ccc
[/TD]
[TD]123
[/TD]
[TD]456
[/TD]
[TD]789
[/TD]
[/TR]
</tbody>[/TABLE]

On any row where "Value" is "B," I want to do the following

- Change Col1 to "MyChangedValue"
- Replace contents in Col2, Col3, and Col4 with null

The output should look like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Value
[/TD]
[TD]Col1
[/TD]
[TD]Col2
[/TD]
[TD]Col3
[/TD]
[TD]Col4
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]aaa
[/TD]
[TD]123
[/TD]
[TD]456
[/TD]
[TD]789
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]MyChangedValue
[/TD]
[TD]null
[/TD]
[TD]null
[/TD]
[TD]null
[/TD]
[/TR]
[TR]
[TD]C
[/TD]
[TD]ccc
[/TD]
[TD]123
[/TD]
[TD]456
[/TD]
[TD]789
[/TD]
[/TR]
</tbody>[/TABLE]


The only way I know how to do this currently is to add a conditional column for each one, and perform the test "if [Value] = "B" then..."

Is there a better approach to this than adding 4 new columns and then removing the old ones? In some cases, I may have up to 8 columns requiring modification like this.

Thanks
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
One of possible ways
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
     Result = Table.ExpandRecordColumn(Table.AddColumn(Source, "Records", each if [Value] = "B" then Record.FromList( {"B", "MyChangedValue"},   List.FirstN(Table.ColumnNames(Source),2)) else _)[[Records]], "Records",Table.ColumnNames(Source))
in
     Result
 
Last edited:
Upvote 0
Two more possible ways. Table1 is already imported into Power Query and typed.

Code:
let
    Source = Table1,
    Custom1 = Table.Combine(Table.TransformRows(Source, each if [Value] = "B" then #table(Value.Type(Source),{{"B","MyChangedValue",null,null,null}}) else Table.FromRecords({_},Value.Type(Source))))
in
    Custom1

Code:
let
    Source = Table1,
    Result = Value.ReplaceType(Table.Combine(Table.TransformRows(Source, each if [Value] = "B" then #table(List.FirstN(Table.ColumnNames(Source),2),{{"B","MyChangedValue"}}) else Table.FromRecords({_}))),Value.Type(Source))
in
    Result
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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