Forum Discussion
Multiple columns to Multiple Rows in power query
hi Team,
I have 3 million records with 8 columns coming from DB table . i need help to get the fast way in power query to transpose 1 record to 3 rows as below:
Col4,Col7 should be transposed under Col1....
Col5,Col8 should be transposed under Col2......
Col3 and Col6 should not be transposed .
pleaese let me know if there a way to achieve it.
Link to PBIX with sample data is attached.Please help
https://drive.google.com/file/d/1j-snDUUjFdeefu01XVyNsaa51HxOlEkW/view?usp=sharing
sample input data
required output:
| Col1 | Col2 | Col3 | Col6 |
| a1 | a2 | a3 | a6 |
| a4 | a5 | a3 | a6 |
| a7 | a8 | a3 | a6 |
| b1 | b2 | b3 | b6 |
| b4 | b5 | b3 | b6 |
| b7 | b8 | b3 | b6 |
| c1 | c2 | c3 | c6 |
| c4 | c5 | c3 | c6 |
| c7 | c8 | c3 | c6 |
| d1 | d2 | d3 | d6 |
| d4 | d5 | d3 | d6 |
| d7 | d8 | d3 | c6 |
In our file itself, just paste the M code. It will work fine.
8 Replies
- Ashish_MathurSuper User
Hi,
This M code works
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JcqpEcAwFEPBXj42yZ1ePAaW1H8NyRuTRdt7za1azR0OOOGCGx54a7ReYooppphiiimm1jTTTDPNNNNMM71mmGGGGWaYYYaZf44P", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, Col4 = _t, Col5 = _t, Col6 = _t, Col7 = _t, Col8 = _t]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Col3", "Col6"}, "Attribute", "Value"), #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if [Attribute]="Col1" or [Attribute]="Col4" or [Attribute]="Col7" then "Col1" else "Col2"), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Attribute"}), Partition = Table.Group(#"Removed Columns", {"Custom"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}), #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Col3", "Col6", "Value", "Index"}, {"Col3", "Col6", "Value", "Index"}), #"Pivoted Column" = Table.Pivot(#"Expanded Partition", List.Distinct(#"Expanded Partition"[Custom]), "Custom", "Value"), #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"}) in #"Removed Columns1"Hope this helps.
- ak77Post Patron
Thanks Ashish. i will check and get back to u
- ak77Post Patron
Ashish_Mathur , can u please share the pbix file
- ak77Post Patron
Thanks Ashish_Mathur . some syntax issue when i tried before .. got it fixed and running fine when i copy pasted your M code again .Thanks again