cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
H_M
New Member

Replacing values in one column with values from another table

Hi,

I want to replace values in one table using another table as a reference/lookup.

I have tried to add the following as a step in my orginal tables 

 

= Table.ReplaceValue ( #"Changed Type",each [Stop Location], each List.Accumulate (List.Buffer(Table.ToRecords(Amendments)),[Stop Location], (valueToReplace, replaceOldNewRecord)=>Text.Replace (valueToReplace, replaceOldNewRecord[Current], replaceOldNewRecord[Replace])),Replacer.ReplaceText,{"Stop Location"})

 

Where Stop Location is the column containing the data I want to amend. 

Amendments is the table containing the two values that I want to use as a look up and replcement (current = lookup, replace = replacement value to switch within the organial table in the Stop Location collumn. 

 

However, this results in the following

 

Expression.Error: The column 'Stop Location' of the table wasn't found.
Details:
Stop Location

 

Any ideas what I am doing wrong? I am very new to Power BI / Power Query. 

 

Many thanks in advance

HM

1 ACCEPTED SOLUTION
v-angzheng-msft
Community Support
Community Support

Hi, @H_M 

 

Check whether the above search column text contains invisible characters. The simple way to avoid this error is to copy and paste the column name.

vangzhengmsft_1-1638434705304.png

 

refer:

Table.ReplaceValue

Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table

 

Here is a solution I thought of, which may help you.

I encapsulated a function that replaces the value, which can be called on the column. This works for all columns.

Create a blank query, open Advanced Editor and replace the text there with the code below.  In your original query, you can then go to the Add Column tab, invoke custom function and choose this function and choose your "Old" column as the input.

 

(inputtext as text) =>
let 
Result = List.ReplaceMatchingItems(Table.ToList(Table.FromValue(Text.From(inputtext))), List.Zip({Amendments[Current], Amendments[Replace]}))
in 
Result{0}

Result:

 

vangzhengmsft_2-1638435609704.png

 

Please refer to the attachment below for details. Hope this helps.

 

 

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

10 REPLIES 10
H_M
New Member

Hi, Sorry for the long delays in replying. The first table shows and example of the main data, the 2nd the replacement data. As mentioned I have been able to get the results I wanted using: 

= Table.ReplaceValue ( #"Changed Type",each [Stop Location], each List.Accumulate (List.Buffer(Table.ToRecords(Amendments)),[Stop Location], (valueToReplace, replaceOldNewRecord)=>Text.Replace (valueToReplace, replaceOldNewRecord[Current], replaceOldNewRecord[Replace])),Replacer.ReplaceText,{"Stop Location"})

as a step in  power query, but it took a few reloads for it to really stick. I had to duplicate the step to replace values in both start and stop location.

 

 

Driver NameStart LocationStop Location
1Joe Bloggs19 Brownstone Ave, Cloverfield, SY89 4, UK50 One Rd, That Town, TV12 8, UK
2Joe Bloggs50 One Rd, That Town, TV12 8, UKSkips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5, UK
3Joe BloggsSkips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5, UK70 Westview, PondTown, BV7 0, UK
1Sarah RobbinsLocation near BerryTree Lane, Maple, SK42 6, UKGarage - Bradley Street, Tower Hill, WV8 4, UK
2Sarah RobbinsGarage - Bradley Street, Tower Hill, WV8 4, UK11 Avalon Close, Cherryton, Warmly, BD76 2, UK
3Sarah Robbins11 Avalon Close, Cherryton, Warmly, BD76 2, UKGarage - Bradley Street, Tower Hill, WV8 4, UK
4Sarah RobbinsGarage - Bradley Street, Tower Hill, WV8 4, UKSkips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5, UK
5Sarah RobbinsSkips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5, UKLocation near BerryTree Lane, Maple, SK42 6, UK

 

CurrentReplace
Skips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5, UKSkips - Long Rd (K3894), Sunny Side, By The River,  BZ9 5GH, UK
Garage - Bradley Street, Tower Hill, WV8 4, UKGarage - Bradley Street, Tower Hill, WV8 4HT, UK
Location near BerryTree Lane, Maple, SK42 6, UKNewside near BerryTree Lane, Maple, SK42 6HT, UK
H_M
New Member

Hi @smpa01 , I can't figure out how to attached a pbix here. What I have found is that 

 

= Table.ReplaceValue ( #"Changed Type",each [Stop Location], each List.Accumulate (List.Buffer(Table.ToRecords(Amendments)),[Stop Location], (valueToReplace, replaceOldNewRecord)=>Text.Replace (valueToReplace, replaceOldNewRecord[Current], replaceOldNewRecord[Replace])),Replacer.ReplaceText,{"Stop Location"})

 

Does actually work, the issue was where I had the step. I moved it so that it was directly after promoting first row to headers and prior to a data change and it worked. I added a second step, the same as above to amend the start locations also.

For some reason I had to refresh about 3 times for everything to 'stick' I have no idea why this might be.

@H_M  you can easily copy paste sample data and desired output here. If you fail, you can always uplaod a sample pbix in g/1 drive and share the linke here. Without sample data I can't advise.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






New Animated Dashboard: Sales Calendar


v-angzheng-msft
Community Support
Community Support

Hi, @H_M 

 

Check whether the above search column text contains invisible characters. The simple way to avoid this error is to copy and paste the column name.

vangzhengmsft_1-1638434705304.png

 

refer:

Table.ReplaceValue

Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table

 

Here is a solution I thought of, which may help you.

I encapsulated a function that replaces the value, which can be called on the column. This works for all columns.

Create a blank query, open Advanced Editor and replace the text there with the code below.  In your original query, you can then go to the Add Column tab, invoke custom function and choose this function and choose your "Old" column as the input.

 

(inputtext as text) =>
let 
Result = List.ReplaceMatchingItems(Table.ToList(Table.FromValue(Text.From(inputtext))), List.Zip({Amendments[Current], Amendments[Replace]}))
in 
Result{0}

Result:

 

vangzhengmsft_2-1638435609704.png

 

Please refer to the attachment below for details. Hope this helps.

 

 

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-angzheng-msft , thank you so much for replying. I am very new to PBI so I've not created functions outside of the steps as yet. I will try to use your example to further my knowlege of how to do this. 

KNP
Super User
Super User

Sometimes it's easier to break this down into multiple steps rather than trying to do it all at once.

See attached PBIX file for a very basic example of what I mean.

It essentially merges the "lookup" table (called mapping in my case) with a left join and then uses the new value if not null otherwise uses the original.

You can then just remove the columns you don't need. 

Don't forget, Power Query is case sensitive, so you will need to account for that if this is an issue.

 

I hope this helps.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
chrome-9xf-Zagzel-B

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
KIfp67uy-Sr
Proud to be a Super User!PBI-Super-User-Rank-30x30-1x
H_M
New Member

Thanks you @KNP , this is a good solution. I really wanted to try to find a way to replace without adding anotnher column, which I have done using the orginal code, the missue seems to be around the placement of the step. Thank you for responding though, this is no doubt be a useful addition to my tool kit!

H_M
New Member

Hi thanks for the reply. I can't post the data as it is location sensitive, but I am looking to replace one string of text in its entirety with another string of text. There are likely to be multiple instances as I move forwards where I will need to replace items and so want to be able to add to a table of replacements to feed into the main data set rather than creating separate replace values for each entry.

@H_M  can you create some dummy data that is representative of the issue and post here.

I am not asking you to post the real data.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






New Animated Dashboard: Sales Calendar


smpa01
Super User
Super User

@H_M this is possible. Can you post some sample data with desired output.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!






New Animated Dashboard: Sales Calendar


Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

Power BI Dev Camp Session 22 with aka link 768x460.jpg

Check it out!

Mark your calendars and join us on Thursday, May 26 at 11a PDT for a great session with Ted Pattison!

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!