Formula to replace text if condition from another column

meileetan

Board Regular
Joined
Aug 18, 2005
Messages
86
I would like to replace text in "Client" column with value of "Company" if the value in "Project" column = Capacity. I can't figure out what is wrong with my formula below - can someone help review? I tried 2 versions:

Version 1
= Table.ReplaceValue(#"Replaced Project TO to Cap",
each [Client],
each if Text.Contains([Project],"Capacity") then "Company" else [Client],
Replacer.ReplaceText,{"Client"})

Version 2
= Table.ReplaceValue(#"Replaced Project TO to Cap",
each [Client],
each if [Project] = "Capacity" then "Company" else [Client],
Replacer.ReplaceText,{"Client"})


The original cells were blank and remained so after the formula was applied.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Table.ReplaceValue doesn't work that way. It looks in all the cells of the table for OldValue and replaces with NewValue. So the way you have written it the function is looking in all cells of the table for a function that looks like (_)=>_[Client], and replace if found with another function. Since none of your cells have that function in them, it finds nothing to replace, and it doesn't throw an error because finding nothing is an acceptable result.

There may be a better way but I would

Add a column: Table.AddColumn(#"Replaced Project TO to Cap", "ClientR1", each if [Project] = "Capacity" then "Company" else [Client])
Delete the old Client Column: Table.RemoveColumns(...)
Rename ClientR1 back to Client: Table.RenameColumns(...)
Reorder back to the original order: Table.ReorderColumns(PreviousStepName, Table.ColumnNames(#"Replaced Project TO to Cap"))
 
Upvote 0
I don't know the logic behind the formula but I know it works because I have done it for other columns in another workbook. Anyway, version 2 mysteriously worked today. I have no idea why! ?‍?
 
Upvote 0
You're right - Table.ReplaceValue has a lot of functionality I didn't realize. Thanks for pointing this out.
 
Upvote 0

Forum statistics

Threads
1,223,923
Messages
6,175,404
Members
452,640
Latest member
steveridge

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