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
Solved! Go to Solution.
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.
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:
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, 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 Name | Start Location | Stop Location |
1 | Joe Bloggs | 19 Brownstone Ave, Cloverfield, SY89 4, UK | 50 One Rd, That Town, TV12 8, UK |
2 | Joe Bloggs | 50 One Rd, That Town, TV12 8, UK | Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5, UK |
3 | Joe Bloggs | Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5, UK | 70 Westview, PondTown, BV7 0, UK |
1 | Sarah Robbins | Location near BerryTree Lane, Maple, SK42 6, UK | Garage - Bradley Street, Tower Hill, WV8 4, UK |
2 | Sarah Robbins | Garage - Bradley Street, Tower Hill, WV8 4, UK | 11 Avalon Close, Cherryton, Warmly, BD76 2, UK |
3 | Sarah Robbins | 11 Avalon Close, Cherryton, Warmly, BD76 2, UK | Garage - Bradley Street, Tower Hill, WV8 4, UK |
4 | Sarah Robbins | Garage - Bradley Street, Tower Hill, WV8 4, UK | Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5, UK |
5 | Sarah Robbins | Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5, UK | Location near BerryTree Lane, Maple, SK42 6, UK |
Current | Replace |
Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5, UK | Skips - Long Rd (K3894), Sunny Side, By The River, BZ9 5GH, UK |
Garage - Bradley Street, Tower Hill, WV8 4, UK | Garage - Bradley Street, Tower Hill, WV8 4HT, UK |
Location near BerryTree Lane, Maple, SK42 6, UK | Newside near BerryTree Lane, Maple, SK42 6HT, UK |
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.
Proud to be a Super User!
New Animated Dashboard: Sales Calendar
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.
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:
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.
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 ;). | ![]() |
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. ![]() | Proud to be a Super User! | ![]() |
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!
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.
Proud to be a Super User!
New Animated Dashboard: Sales Calendar
@H_M this is possible. Can you post some sample data with desired output.
Proud to be a Super User!
New Animated Dashboard: Sales Calendar
Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.
Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!
At the monthly call, connect with other leaders and find out how community makes your experience even better.
User | Count |
---|---|
131 | |
19 | |
18 | |
14 | |
13 |
User | Count |
---|---|
155 | |
33 | |
31 | |
23 | |
18 |