Forum Discussion
Replacing values in multiple columns based on condition in Power Query
- 6 years ago
Hi, Anonymous
Based on the simulated data source and the data processing logic you provided, write the query code like this:
= Table.ReplaceValue(Source, each [day flag]="Down Day", null, (x,y,z)=> if y then z else x, List.Range(Table.ColumnNames(Source),1,4))If my code solves your problem, mark it as a solution
- 6 years ago
Hi, Anonymous
As what is suggested by ziying35 , I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may insert a new step as below. On your side, you may add all corresponding columns in {"plan","unplan","internal","external",...}.
= Table.ReplaceValue(Source, each [day flag]="Down Day", null, (x,y,z)=> if y then z else x, List.Select(Table.ColumnNames(#"Changed Type"), each List.Contains( {"plan","unplan","internal","external"},_) ) )Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi ziying35 , Sorry for pinging you on this old topic. This method works perfectly if I just want to replace the value with a fixed value (null @ 1 @2, etc). But what if I want to multiple the old value with 1000? For example if the old value is 1000, instead of changing it to null i want to make it 1,000,000.
I tried changing the null to below, but it didn't work.
each _ * 1000
Figured it out minutes after asking this.
= Table.ReplaceValue(
Source,
each [day flag]="Down Day",
each _, // this step become redundant, but needed to fill the syntax requirement
// (x,y,z)=> if y then z else x, --- this is the original one, below is edited.
(x,y,z)=> if y then x*1000 else x, // we just ignore z, and replace it with x*1000
List.Range(Table.ColumnNames(Source),1,4)
)