Replacing a value based on 2 other columns

karmaimages

Board Regular
Joined
Oct 1, 2009
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a column where I need to change 1 value within the column based on 2 other columns,

RuleRegulator
CommunicationICO

There are lots of other values in the rule column, but I want to change the value where

[Rule] = "Communication" & [Regulator] = "ICO" then new value = "Data Protection"

I can't seem to work out how to perform a multi value replace value if anyone can help?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If you are adding a new column based on two column values:

Power Query:
NewColumn = Table.AddColumn(Source, "NewColumn", each if [Rule] = "Communication" and [Regulator] = "ICO" then "Data Protection" else "")

If you are trying to "edit" an existing column value then it is a bit different. You can either add the new column as shown above and set the "else" value as the existing column row value, then remove the existing column and use the new column as the removed column.

Or, you can edit the existing column's row values by using some M code. Sample code below:

Power Query:
let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PzS3Ny0xOLMnMz1PSUfJ09geSYYk5pamGSrE60UrB+bmpJRmZeekKqTnFqUA5Z39PmAojsAqCRqArcPZEUxCaV5Sak1iSmoKqG7vx/s5w+42VYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rule = _t, Regulator = _t, ExistingColumn = _t]),
  
  ReplaceExisting = Table.FromRecords(Table.TransformRows(Source, 
                        (r) => Record.TransformFields(r, {"ExistingColumn", 
                                        each if r[Rule] = "Communication" and r[Regulator] = "ICO" then "Data Protection" else _ })))
in
  ReplaceExisting

The following code shows each mentioned method separately. Click on the steps to see individual results that transforms the Source by using each method:
Power Query:
let
  Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PzS3Ny0xOLMnMz1PSUfJ09geSYYk5pamGSrE60UrB+bmpJRmZeekKqTnFqUA5Z39PmAojsAqCRqArcPZEUxCaV5Sak1iSmoKqG7vx/s5w+42VYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rule = _t, Regulator = _t, ExistingColumn = _t]),
  
  NewColumn = Table.AddColumn(Source, "NewColumn", each if [Rule] = "Communication" and [Regulator] = "ICO" then "Data Protection" else ""),
  
  ReplacementColumn = Table.RemoveColumns(Table.AddColumn(Source, "ReplacementColumn", each if [Rule] = "Communication" and [Regulator] = "ICO" then "Data Protection" else [ExistingColumn]), {"ExistingColumn"}),
  
  ReplaceExisting = Table.FromRecords(Table.TransformRows(Source, 
                        (r) => Record.TransformFields(r, {"ExistingColumn", 
                                        each if r[Rule] = "Communication" and r[Regulator] = "ICO" then "Data Protection" else _ })))
in
  ReplaceExisting
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,412
Members
452,399
Latest member
oranges

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