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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Replace values of multiple columns using m code power bi

Hi,

 

I am trying to find and replace values for all the date columns ranging from Jan 2019 to Dec 2024.

 

I am doing some transformation using if condition, like first select the condition from another column and if it matches multiply the values in the column with multiplier column and 1000 else keep them as is and replace the values in existing column. Below is the m code that works only for single column.

 

</> #"Replaced Value3" = Table.ReplaceValue(#"Changed Type3",each [#"May-2020"], each if [Impact Type] = "One-off costs ($m)" then [Multiplier] * [#"May-2020"] * 1000 else [#"May-2020"] ,Replacer.ReplaceValue,{"May-2020"}) </>

 

How can I use this single step for all the columns like this so then I can use the transformations for all the columns in a single go:

 

</> #"Replaced Value3" = Table.ReplaceValue(#"Changed Type3", each {"Jan-2020", "Feb-2020", "Mar-2020", "Apr-2020", "May-2020"}, each if [Impact Type] = "One-off costs ($m)" then [Multiplier]  * {"Jan-2020", "Feb-2020", "Mar-2020", "Apr-2020", "May-2020"} * 1000 else {"Jan-2020", "Feb-2020", "Mar-2020", "Apr-2020", "May-2020"} ,Replacer.ReplaceValue,{"Jan-2020", "Feb-2020", "Mar-2020", "Apr-2020", "May-2020"}) </>

 

The Multiplier and Impact type looks like this,

adityaaranya6_0-1633942625605.png

 

The transformation can be seen below, I want the same for all the columns that is for May-2020, just use multiplier and condition followed by multiply by 1000. 

 

adityaaranya6_1-1633943261422.png

 

 

Can someone please help as I am stuck on this one for a long time now. Many Thanks!

 

Please let me know where I can clarify further.

@amitchandak @Greg_Deckler @AlexisOlson 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Multi-select (Ctrl+click) all colums that are not your date columns.

Go to Transform tab > Unpivot Columns > Unpivot Other Columns.

Perform your replace function on the new [Value] field.

Re-pivot [Attribute] column using [Value] as population field if you want *.

 

As a side note, the data structure after you have done the first Unpivot operation is the generally-accepted correct data structure. Having separate columns for each date is unwieldy and will not scale well. Additionally, the structure you currently have will make it very difficult to create relationships and write measures.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

1 REPLY 1
BA_Pete
Super User
Super User

Hi @Anonymous ,

 

Multi-select (Ctrl+click) all colums that are not your date columns.

Go to Transform tab > Unpivot Columns > Unpivot Other Columns.

Perform your replace function on the new [Value] field.

Re-pivot [Attribute] column using [Value] as population field if you want *.

 

As a side note, the data structure after you have done the first Unpivot operation is the generally-accepted correct data structure. Having separate columns for each date is unwieldy and will not scale well. Additionally, the structure you currently have will make it very difficult to create relationships and write measures.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.