Multiple table.replacevalue

tobermory

New Member
Joined
Jun 4, 2012
Messages
46
Hi,

So embarrassed to ask this, new to PQ. Soon as I see the answer it'll click.

I have three Table.ReplaceValue, would like to merge into one step.

= Table.ReplaceValue(#"Changed Type","Ad","Adult",Replacer.ReplaceText,{"Column1"})
= Table.ReplaceValue(#"Replaced Value","Ch","Child",Replacer.ReplaceText,{"Column1"})
= Table.ReplaceValue(#"Replaced Value1","Sr","Senior",Replacer.ReplaceText,{"Column1"})

I know it involves { and } but cant seem to get the right combo.

Thanks very much.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Probably some better ways exist. But here are two options:

1) You can nest the three Table.ReplaceValue functions succinctly using let..in

Power Query:
= let f = Table.ReplaceValue, r = Replacer.ReplaceText, c = {"Column1"} in f(f(f(#"Changed Type","Ad","Adult",r,c),"Ch","Child",r,c),"Sr","Senior",r,c)

as in:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    Out = let f = Table.ReplaceValue, r = Replacer.ReplaceText, c = {"Column1"} in f(f(f(#"Changed Type","Ad","Adult",r,c),"Ch","Child",r,c),"Sr","Senior",r,c)
in
    Out

2) or you could use Table.TransformColumns with a custom transform operation, but as written this only works if the Column just contains the "Ad" and not something like "Price for Ad"

Power Query:
= Table.TransformColumns(#"Changed Type",{"Column1", (x) => if x = "Ch" then "Child" else if x = "Ad" then "Adult" else if x = "Sr" then "Senior" else x})

or

Power Query:
= Table.TransformColumns(#"Changed Type",{"Column1", (x) => let lpo = List.PositionOf({"Ch","Ad","Sr"},x) in if lpo>=0 then {"Child","Adult","Senior"}{lpo} else x})

as in:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    Out = Table.TransformColumns(#"Changed Type",{"Column1", (x) => if x = "Ch" then "Child" else if x = "Ad" then "Adult" else if x = "Sr" then "Senior" else x})
in
    Out
 
Upvote 0
Another way using a custom m function called Table_ReplaceColumn:

Power Query:
let
    Table_ReplaceColumn = (tblInput as table, txtColName as text, lstColRepl as list) as table =>
        let
            lstTable = Table.ToColumns(tblInput),
            tcn = Table.ColumnNames(tblInput),
            col = List.PositionOf(tcn, txtColName),
            lstColumns = List.FirstN(lstTable,col) & {lstColRepl} & List.Skip(lstTable,col +1),
            tblOut = Table.FromColumns(lstColumns,tcn)
        in
            tblOut,
    
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    lstChanges = {{"Ch", "Child"},{"Ad","Adult"},{"Sr","Senior"}},
    tblResult = Table_ReplaceColumn(Source,"Column1",List.ReplaceMatchingItems(#"Changed Type"[Column1], lstChanges))
in
    tblResult
 
Upvote 0
Here are two other alternatives, one using if / else if and another using a Record as a type of Switch function:

if/else if:
Power Query:
= Table.ReplaceValue(#"Changed Type",each [Column1], each if [Column1] = "Ad" then "Adult" else if [Column1] = "Ch" then "Child" else if [Column1] = "Sr" then "Senior" else [Column1],Replacer.ReplaceText,{"Column1"})

Record as Switch Function:
Power Query:
= Table.ReplaceValue(#"Changed Type",each [Column1], each Record.FieldOrDefault([Ad = "Adult", Ch = "Child", Sr = "Senior"], [Column1], [Column1]),Replacer.ReplaceText,{"Column1"})
In the Record.FieldOrDefault:
1st Parameter: A record with the "Conditions",
2nd Parameter: Pass the criteria (ie, the value in Column1)
3rd Parameter: Default value (ie, pass Column1 again and return that value if there is no match)

Personally I like the Record/Switch procedure as it's easier to add additional conditions
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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