Power Query - Copy certain text from 1 column to another based on value

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have 2 columns titled Reason and Code. In the reason column there are numbers like 300, 331, 600, and 401. In the Code column there are codes like 3.3A, LTD-F, etc. If there is a 401 in the Reason column, I need to place 401 in the Code column. I've tried replace value but can't get it to work. Here is the formula I tried = Table.ReplaceValue(Source,each
Code:
,each if [Reason] = "401" then [Code] = 401 else [Code],Replacer.ReplaceValue,{"Code"}) but it's not working. It says that the column Code cannot be found but its clearly on the table so I'm not sure what I'm doing wrong. Any help would be greatly appreciated.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can add a new column code and apply an if statement. Then delete the original column. Renate the new column. You can't replace values in a column with a formula like that (too my level of understanding of M).
 
Upvote 0
The formula in my post works on another table but not on the one I need it to work on. Not sure why.
 
Upvote 0
Why not simply use
Power Query:
Text.Replace([Code], "old", "new")
inside the if then else.
 
Upvote 0
Might be just taking a shot in the dark, but should your
Power Query:
[Code] = 401
in your if statement be
Power Query:
[Code] = "401"
I'm suspecting maybe a datatype mismatch with no quotes as you have it.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,989
Members
452,541
Latest member
haasro02

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