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,

Value
Col1
Col2
Col3
Col4
A
aaa
123
456
789
B
bbb
123
456
789
C
ccc
123
456
789

<tbody>
</tbody>

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:
Value
Col1
Col2
Col3
Col4
A
aaa
123
456
789
B
MyChangedValue
null
null
null
C
ccc
123
456
789

<tbody>
</tbody>


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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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