Forum Discussion
Replacing values in one column with values from another table
- 4 years ago
Hi, Anonymous
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 guys,
As I had the same issue I stumbled across this topic. It took me another 3 hours with ChatGPT to come up with the following code:
let
// Load TableA
SourceA = ..., // Enter the source for TableA
// Load TableB
TableB = ..., // Enter the source for TableB
// Define a function to replace Total Amount
ReplaceTotalAmount = (invoiceNum, amountA) =>
let
matchingRow = Table.SelectRows(TableB, each [Invoicenumber] = invoiceNum),
correctedAmount = if Table.RowCount(matchingRow) > 0 then matchingRow{0}[Total Amount] else amountA
in
correctedAmount,
// Add a new column with corrected Total Amount values
UpdatedTableA = Table.AddColumn(
SourceA,
"Corrected Total Amount",
each ReplaceTotalAmount([Invoicenumber], [Total Amount]),
type number
),
// Remove the original "Total Amount" column
RemovedOriginalTotalAmount = Table.RemoveColumns(UpdatedTableA, {"Total Amount"}),
// Rename the new "Corrected Total Amount" column to "Total Amount"
RenamedColumn = Table.RenameColumns(RemovedOriginalTotalAmount, {{"Corrected Total Amount", "Total Amount"}}),
// Reorder the columns back to original order
ReorderedColumns = Table.ReorderColumns(RenamedColumn, {"Invoicenumber", ..., "Total Amount"}) // Replace ... with the other column names
in
ReorderedColumns
Table A is your source file, TableB has some corrections.
TableB has 2 fields (in my case, but you can easily change this to your situation): invoicenumber and total amount.
You must have understood by now, that TableA has many fields with invoice data and TableB can make a correction to the invoice amount based on a matching invoice number.
I didn't want the outcome to produce a new table, so merging was out of the option. ChatGPT tried many times to update the field with the corrected amount but that took me an hour to get around.
So the solution is:
- add a column that has either the original amount unless based on a matching invoicenumber the corrected amount of table b is placed there.
- then remove/delete the original column (that as a wrong entry)
- rename the added column to the column name you just removed/deleted
- to tidy things up, like nothing happened, I reordered the columns to the original order as the newly added column was added as last
Hope this helps someone, it took me 6 hours to figure it out, it may save you some time.
Cheers R