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.
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.
Id | Green | red | yellow |
| 0 | 1 | 0 |
| 1 | 1 | 0 |
| 0 | 0 | 1 |
To become
Id | Green | red | yellow |
|
| red |
|
| Green | red |
|
|
|
| yellow |
Solved! Go to 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
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.
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
42 | |
23 | |
21 | |
15 | |
15 |
User | Count |
---|---|
45 | |
31 | |
30 | |
18 | |
17 |