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
nnouchi
Helper I
Helper I

Power Query - ReplaceValues with two conditions

Greetings PBI Community,

 

I'm trying to reassign a value in a Customer Orders table that is based off of two conditions from columns within that Customer Orders table. I've found that I need to use the Power Query table function --> Table.ReplaceValues()

 

I haven't found a similar example where multiple conditions were checked. These Customer_IDs aren't the only ones in my table and thus expect only a handful of values changed.

 

Here is an example of my code below(code insertion is in bold):

 

let
Source = Sql.Database("Server", "DB"),
Customer_Orders= Source{[Schema="dbo",Item="Customer_Orders"]}[Data],
#"Replaced Value" = Table.ReplaceValue(#"Replaced Value", [SM_Region], each if Record.Field({[Customer_ID] = 61020 and [SM_Region] = 54 then 21 else , [Customer_ID] = 61040 and [SM_Region] = 65, [Customer_ID] = 90001 and [SM_Region] = 54 then 56,[Customer_ID] = 60180 and [SM_Region] = 54 then 13,[Customer_ID]=60280 and [SM_Region] = 54 then 65,[Customer_ID] = 61000 and [SM_Region] = 54 then 65,[Customer_ID] = 90002 and [SM_Region] = 54 then 56, [Customer_ID] = 90003 and [SM_Region] = 54 then 53},Replacer.ReplaceValue,{"SM_Region"}))
in
Customer_Orders

 

 

Any help would be greatly appreciated.

 

Nic

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

As an alternative, you could add a new column with the correct values and delete the old column.

 

That would enable you to work with a "replacements table" that you join on [Customer_ID] and [SM_Region] to your Customer Orders table.

 

Expand the new value and add a new column that checks if the new value is not null -> take the new value  - otherwise - take the value from the original column.

 

Less typing and probably easier to maintain.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Super User
Super User

As an alternative, you could add a new column with the correct values and delete the old column.

 

That would enable you to work with a "replacements table" that you join on [Customer_ID] and [SM_Region] to your Customer Orders table.

 

Expand the new value and add a new column that checks if the new value is not null -> take the new value  - otherwise - take the value from the original column.

 

Less typing and probably easier to maintain.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.