Forum Discussion
Malsk1_1
Helper II
3 years agoHow to flatten data in Power BI
Hi,
How can i flatten the data in the attached excel to the desired output:
| AG Code | Name | Capacity | Appointee Code | Appointing Code |
| AG1234 | ABC | Appointing Party | AB98765 | |
| AG1234 | XYZ | Appointee | AB23546 | |
| AG996 | QWE | Appointing Party | AB67456 | |
| AG996 | ASD | Appointee | AB87430 | |
| AG997 | POI | Appointing Party | AB98765 | |
| AG997 | CAB | Appointee | AB90756 |
to the following:
| AG Code | Appointee Code | Appointee Name | Appointing Code | Appointing Name |
| AG1234 | AB23546 | XYZ | AB98765 | ABC |
| AG996 | AB87430 | ASD | AB98765 | QWE |
| AG997 | AB90756 | CAB | AB98765 | POI |
Any ideas? I have tried the options to pivot/unpivot but couldnt get to the desired output
3 Replies
- Malsk1_1
Helper II
Any ideas? I have tried the options to pivot/unpivot but couldnt get to the desired output
- v-zhangti
Community Support
Hi, Malsk1_1
You can try the following methods.
Column:New Appointee Code = CALCULATE(MAX('Table'[Appointee Code]),ALLEXCEPT('Table','Table'[AG Code]))New Appointing Code = CALCULATE(MAX('Table'[Appointing Code]),ALLEXCEPT('Table','Table'[AG Code]))Table:
New table = Var _table=SUMMARIZE('Table','Table'[AG Code],'Table'[New Appointee Code],'Table'[New Appointing Code],'Table'[Name],'Table'[Capacity]) Return FILTER(_table,[Capacity]="Appointing Party")Is this the result you expect?
Best Regards,
Community Support Team _Charlotte
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- Ashish_Mathur
Super User
Hi,
This M code works
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnQ3NDI2UdJRcnRyBpEFBfmZeSWZeekKAYlFJZVAIbCcpYW5malSrA6ShojIKISG1FSwMiNjUxMzkB6ISktLECcw3BWPyWbmJqZmKOodg10wDLYwNzE2QDbYHMgJ8Pck0skQ9c6OThgGWxqYm0JcHAsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"AG Code" = _t, Name = _t, Capacity = _t, #"Appointee Code" = _t, #"Appointing Code" = _t]), #"Merged Columns" = Table.CombineColumns(Source,{"Appointee Code", "Appointing Code"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Code"), #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"Name", "Code"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"), #"Pivoted Column" = Table.Pivot(#"Merged Columns1", List.Distinct(#"Merged Columns1"[Capacity]), "Capacity", "Merged"), #"Split Column by Delimiter" = Table.SplitColumn(#"Pivoted Column", "Appointing Party", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Appointing name", "Appointing code"}), #"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Appointee", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Appointee name", "Appointee code"}) in #"Split Column by Delimiter1"Hope this helps.