Replace values at the same time

Krokolv

New Member
Joined
Jan 3, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi M experts,

I want to add a multiple columns replace values, following this example, into one single step:

Power Query:
= Table.ReplaceValue(#"Changed Type",each [ZT04],each [ZT04]*100 ,Replacer.ReplaceValue,{"ZT04"})

So basically I want to apply this massively to 7 columns in one step. Do you know a way?

Thanks in Advance.
Marco
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Power Query:
= Table.TransformColumns(#"Changed Type", let cols = {"col2", "col4", "col6", "col7", "col9", "col10", "col11"} in List.Zip({cols, List.Repeat({each _*100},List.Count(cols))}))

change {"col2", "col4", "col6", "col7", "col9", "col10", "col11"} to the names of the columns that you want to transform
 
Upvote 0
Solution
Woow, works like a charm I will save this code forever :D I see the potential here for other operations as well. I am also searching for something similar, but in this case I would have to each row in 2 different columns:
VBA Code:
= Table.AddColumn(#"Multiplication", "Dif ZT04", each if [Sales Document] = "ZFUT" or "ZCQ" then [ZT04] - [Modeller.ZT04] else 1)
= Table.AddColumn(#"Added Conditional Column", "Dif ZT37", each  [ZT37] - [Modeller.ZT37])
I have like 10 steps like this, do you see a way of putting a lot of this custom columns into a single step? sorry for pushing on your expertise.
 
Upvote 0
Here's an example that may get you started

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    ZTs = {"ZT1","ZT2","ZT3","ZT4"},
    cols = List.Zip({ZTs, List.Transform(ZTs, each "Modeller." & _)}),
    Result = List.Accumulate(cols, Source, (s,c)=> Table.AddColumn(s, "Dif " & c{0}, each Record.Field(_, c{0}) - Record.Field(_,c{1})))
in
    Result

Book1
ABCDEFGHIJKLMNOPQRSTUV
1ZT1ZT2ZT3ZT4Modeller.ZT1Modeller.ZT2Modeller.ZT3Modeller.ZT4ZT1ZT2ZT3ZT4Modeller.ZT1Modeller.ZT2Modeller.ZT3Modeller.ZT4Dif ZT1Dif ZT2Dif ZT3Dif ZT4
245171326141832445171326141832431-1102
3104128344715234104128344715234-3726530
4531637556221238531637556221238-3-625-33
5397304414843493973044148434938-41-13-5
654271951522021654271951522021627-245
78314250553693383142505536933-47-53317
8293224011253546293224011253546187-33-6
9
Sheet4
 
Upvote 0
Hi M experts,

I want to add a multiple columns replace values, following this example, into one single step:

Power Query:
= Table.ReplaceValue(#"Changed Type",each [ZT04],each [ZT04]*100 ,Replacer.ReplaceValue,{"ZT04"})

So basically I want to apply this massively to 7 columns in one step. Do you know a way?

Thanks in Advance.
Marco
Although a bit odd in syntax, it can be done with the Table.ReplaceValue

Power Query:
= Table.ReplaceValue(#"Changed Type", each true, null , (x,y,z)=> if y then x * 100 else x, {"ZT04","ZT05"})

Also, as an alternative, you can replace the List.Zip functions with a single List.Transform:

Power Query:
= Table.TransformColumns(#"Changed Type", List.Transform({"ZT04", "ZT05"}, each {_, each _*100}))

Power Query:
= List.Transform(ZTs, each {_,"Modeller." & _})
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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