Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ebeauchamp
Helper I
Helper I

Find and replace one specific value in a row

Hi everyone,

In Power BI, I am currently using a table with about 8M transactions from an SQL query. For one very specific transaction, I want to change the SKU name and replace it with a different SKU name. With Power Query Editor, I can achieve it by filtering the transactions to identify the row in question (filter by customer name, and the specific amount), and then use the "Replace Values" option, with the SKU column selected. But then I can't go back and unfilter the table, so this method does not work.

What other methods are available to find a specific row, and then replace the original value in that row only? With the constraint that this same original value is present on thousands of other rows where it should stay as is and not be impacted by the replacement. 

Thanks for your help,
Erik

1 ACCEPTED SOLUTION
Anonymous
Not applicable

If you are sure there is only one record matching criteria, you can use this line of code:

 

= Table.ReplaceMatchingRows(yourTab, {Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610){0},[name="Client3",amount=610,SKU=newValue]})

 

Otherwise, in case of multiple rows matching the criteria, try this:

 

 

Table.ReplaceMatchingRows(yourTab, List.Zip({Table.ToRecords(Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610)), List.Repeat({[name="Client3",amount=610,SKU=newValue]}, List.Count(Table.ToRecords(Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610))))}))

 

PS

In case of duplicate rows, seems there is some problems. But I didn't investigate furter .

View solution in original post

5 REPLIES 5
Mariusz
Community Champion
Community Champion

Hi @ebeauchamp 

 

Can you provide a data sample so the scenario can be recreated?

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

This is very simplified example from a table with 30 columns and Ms of rows. But basically, I need to apply a transformation after the query where I can find the row with the amount "5787", and replace the SKU "ProductABC", for another SKU, eg "ProductXYZ", just for that row - without impacting the other rows. Thanks

SnapshotClientSKUAmount
2020-06-09Client1ProductABC100
2020-06-09Client2ProductABC120
2020-06-09Client3ProductABC90
2020-06-08Client1ProductABC130
2020-06-08Client2ProductABC5787
2020-06-08Client3ProductABC100
2020-06-07Client1ProductABC392
2020-06-07Client2ProductABC400
2020-06-07Client3ProductABC110
Anonymous
Not applicable

If you are sure there is only one record matching criteria, you can use this line of code:

 

= Table.ReplaceMatchingRows(yourTab, {Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610){0},[name="Client3",amount=610,SKU=newValue]})

 

Otherwise, in case of multiple rows matching the criteria, try this:

 

 

Table.ReplaceMatchingRows(yourTab, List.Zip({Table.ToRecords(Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610)), List.Repeat({[name="Client3",amount=610,SKU=newValue]}, List.Count(Table.ToRecords(Table.SelectRows(yourTab, each [name]="Client3" and [amount]=610))))}))

 

PS

In case of duplicate rows, seems there is some problems. But I didn't investigate furter .

Anonymous
Not applicable

This should work in both cases:

 

Table.FromRecords(Table.TransformRows(yourTab, each if ([name]="nome3" and [amount]=5787) then Record.TransformFields(_,{"SKU", (_)=> newValue}) else _))
Anonymous
Not applicable

and yet another way to set a field to a specific value:

Table.FromRecords(Table.TransformRows(yourTab, each if ([name]="nome3" and [amount]=5787) then Record.Combine({_,[SKU=newValue]}) else _))

 

Also this solution seems valid  both in case of single, multiple or null matching

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors