Guest User

Untitled

a guest
Oct 22nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. let
  2. Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY49D4MgEIb/C7MLYL9m07GNadwMw9UwmKLXACb67z1sbaAMR9477rk8bcuafmAF45zTW+ux6w0FSVWB6SYDHm34p2r07J+IL4pHpooUfUxG74sVDm9wjpIInYUFRwrnDLqj19+LYfNqwW1Hyo/LNg7MDewSGiFiyfJfUqSSp5zdLUViKWPLS05FmjLWPPw0JVNqBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, Record = _t, Item.1 = _t, Value.1 = _t, Item.2 = _t, Value.2 = _t, Item.3 = _t, Value.3 = _t]),
  3. #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Record", Int64.Type}, {"Item.1", type text}, {"Value.1", Int64.Type}, {"Item.2", type text}, {"Value.2", Int64.Type}, {"Item.3", type text}, {"Value.3", Int64.Type}}),
  4. #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Record", "Value.1", "Value.2", "Value.3"}, "Attribute", "Value"),
  5. #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "ItemKey"}, {"Value", "Item"}}),
  6. #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Name", "Record", "ItemKey", "Item"}, "Attribute", "Value"),
  7. #"Renamed Columns1" = Table.RenameColumns(#"Unpivoted Columns1",{{"Attribute", "ValueKey"}}),
  8. #"Filtered Rows" = Table.SelectRows(#"Renamed Columns1", each Text.EndsWith([ItemKey], Text.End([ValueKey], 1))),
  9. #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"ItemKey", "ValueKey"}),
  10. #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Item]), "Item", "Value", List.Sum)
  11. in
  12. #"Pivoted Column"
  13.  
  14. let
  15. Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
  16.  
  17. //Add index for each row of source column. Use Add Index Column
  18. #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1),
  19. //Unpivot table
  20. #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index", "Name", "Record"}, "Attribute", "Value"),
  21.  
  22. //At this step I corrected default column names in formula editor.
  23. // Also, it is important to have only 1 dot in column names.
  24. #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns","Attribute",Splitter.SplitTextByEachDelimiter({"."}, QuoteStyle.Csv, false),{"ColumnName", "ColumnIndex"}),
  25. //As a result we get old column names clean of numbers in one column, and index of each column in another
  26.  
  27. //Next we combine row index and column index in order to generate identifier for new row. Add custom column, write Text.From([Index]) & Text.From([ColumnIndex]) in formula window
  28. #"Added Custom" = Table.AddColumn(#"Split Column by Delimiter", "Idx", each Text.From([Index]) & Text.From([ColumnIndex])),
  29.  
  30. //some cleanup
  31. #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Idx", "Name", "Record", "ColumnName", "Value"}),
  32.  
  33. // Pivot columns back. Without row identifier this won't work!
  34. #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", {"Item", "Value"}, "ColumnName", "Value"), //Step on ColumnNames, select Pivot, Value as values column, Don't Aggregate as function.
  35. #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Idx"}),
  36. //Finally, Pivot to the desired look.
  37. #"Pivoted Column1" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Item]), "Item", "Value", List.Sum) //Step on Item, select Pivot, Value as values column, Sum or Don't Aggregate as function.
  38. in
  39. #"Pivoted Column1"
Add Comment
Please, Sign In to add comment