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
Merleau
Helper II
Helper II

How to automate "replace values" in multiple columns by their column name using M?

Hello,

I think thisi s Power Query question.

I am new to Power Query and M language. Still looking for good tutorial on M.

This is my problem.

I have a table with more than 40 columns.

Originally those columns contain "0" and "1"

I need them to contain only their respective column names instead of "0" or "1"s.

I did the following:

1- Change all column type from numeric to text

2- Bulk replaces values "0" with nothing, empty.

3- The last step is to replace each "1' in the column by the column name and I don't know how to automate it. I can do it manually by using "replace value" in about 30 columns but that is tedious.

 

Can somebody pls help?

Thank you.

 

  1.  

Id

Green

red

yellow

 

0

1

0

 

1

1

0

 

0

0

1

 

 

To become

Id

Green

red

yellow

 

 

red

 

 

Green

red

 

 

 

 

yellow

1 ACCEPTED SOLUTION

Hi @Merleau  

it would be easiest to unpivot on the ID-column and then add a column with the replacement-command. Then pivot back.

Add the following code into the advanced editor of a new query and you can follow the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIAYggdqxOtZATlIUSMoWog6mJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, Green = _t, red = _t, yellow = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Id"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if [Value] = "1" then [Attribute] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Custom")
in
    #"Pivoted Column"

 

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

3 REPLIES 3
Greg_Deckler
Super User
Super User

@ImkeF 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Merleau  

it would be easiest to unpivot on the ID-column and then add a column with the replacement-command. Then pivot back.

Add the following code into the advanced editor of a new query and you can follow the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIAYggdqxOtZATlIUSMoWog6mJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id = _t, Green = _t, red = _t, yellow = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Id"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if [Value] = "1" then [Attribute] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Custom")
in
    #"Pivoted Column"

 

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

Thanks so much ImkeF.

This approach completely works.

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