Forum Discussion
Change column values based on different column values
- 1 year ago
The structure/syntax of the step looks fine. Is it perhaps just a typo:
[Customers]instead of[Customer]?This test query appears to work as intended:
let Source = #table( type table [Date = date, Customer = text, Industry = text], { {#date(2025, 1, 5), "A", "Aerospace"}, {#date(2025, 1, 10), "B", "Baking"}, {#date(2025, 1, 15), "C", "Carpentry"}, {#date(2025, 1, 20), "B", "Baking"}, {#date(2025, 1, 25), "B", "NA"}, {#date(2025, 1, 30), "D", "Detailing"}, {#date(2025, 2, 4), "A", "NA"} } ), #"Replace Values" = Table.ReplaceValue( Source, each [Industry], each if [Customer] = "A" then "Aerospace" else if [Customer] = "B" then "Baking" else if [Customer] = "C" then "Carpentry" else if [Customer] = "D" then "Detailing" else [Industry], Replacer.ReplaceText, {"Industry"} ) in #"Replace Values" - 1 year ago
Hi mikesdunbar,
Thank you for reaching out to the Microsoft Fabric Community Forum.
I have reproduced your scenario in Power BI using Power Query and was able to achieve the expected outcome as per your requirement. For your reference, I’m attaching a .pbix file so you can explore the complete steps directly.
Output:
Thanks you, OwenAuger for sharing your valuable insights.If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
Hi mikesdunbar , I think instead of Table.ReplaceValue, you can use Table.TransformRows which will fit in perfectly in this siutation. I'll leave the images of the output and code below. Don't hesitate to ping back in case of queries. Thanks !
Here's the code:
let
Source = Excel.CurrentWorkbook(){[Name="Table9"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Customer", type text}, {"Industry", type text}}),
Cols = Table.SelectColumns( #"Changed Type",{ "Customer","Industry" } ),
TransformRows = Table.TransformRows( #"Changed Type", each _ & [ Industry = if [Industry] = "NA" then Record.ToList ( Record.SelectFields ( #"Changed Type" { List.PositionOf ( #"Changed Type"[Customer] , [Customer] ) } , "Industry" ) ) {0} else [Industry] ] ),
Custom1 = Table.FromRecords ( TransformRows )
in
Custom1