itsnotaboutthecell

Power Query M - Group Rows, Transpose, Table.ToList

Feb 3rd, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. /*
  2. Original Post: https://un.reddit.com/r/excel/comments/7uzldp/looking_for_a_way_to_organise_files/
  3. Created By: Alex Powers
  4. Website: http://www.itsnotaboutthecell.com
  5. Reddit: /u/itsnotaboutthecell
  6. Edits: /u/tirlibibi17
  7. */
  8.  
  9. let
  10. Source = Csv.Document(File.Contents("C:\Users\xxxxx\Desktop\ocldb1517674051.29276.OSD.csv"),[Delimiter=",", Columns=7, Encoding=1252, QuoteStyle=QuoteStyle.None]),
  11. #"Trim All Text" = Table.TransformColumns(Source,{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}, {"Column3", Text.Trim, type text}, {"Column4", Text.Trim, type text}, {"Column5", Text.Trim, type text}, {"Column6", Text.Trim, type text}, {"Column7", Text.Trim, type text}}),
  12. #"Filter: ""#-----..."" and ""END OF VARIABLES SECTION""" = Table.SelectRows(#"Trim All Text", each [Column1] <> "#--------------------------------------------------------------------------------" and [Column1] <> "END OF VARIABLES SECTION"),
  13. #"Grouped Rows" = Table.Group(#"Filter: ""#-----..."" and ""END OF VARIABLES SECTION""", {"Column1"}, {{"Count", each _, type table}}),
  14. #"Table To List" = Table.AddColumn(#"Grouped Rows", "Values", each Table.ToList([Count])),
  15. #"Remove Table Column" = Table.RemoveColumns(#"Table To List",{"Count"}),
  16. #"Extract List Values" = Table.TransformColumns(#"Remove Table Column", {"Values", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
  17. #"Split: "",""" = Table.SplitColumn(#"Extract List Values", "Values", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv)),
  18. #"Transposed Table" = Table.Transpose(#"Split: "","""),
  19. #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
  20. #"Filter: Remove Header Labels" = Table.SelectRows(#"Promoted Headers", each ([CAST] <> "" and [CAST] <> "CAST" and [CAST] <> "WOD Unique Cast Number")),
  21. #"Replace: ""."" with (blank)" = Table.ReplaceValue(#"Filter: Remove Header Labels",".","",Replacer.ReplaceText,{"Accession Number", "Platform", "Bottom depth", "Database origin", "Institute", "High resolution CTD - Bottle"}),
  22. #"Removed Other Columns" = Table.SelectColumns(#"Replace: ""."" with (blank)",{"Year", "Month", "Day", "Latitude", "Longitude", "VARIABLES", "1", "2", "3", "4", "5", "6", "7", "8"}),
  23. #"Replace: (blank) with null" = Table.ReplaceValue(#"Removed Other Columns","",null,Replacer.ReplaceValue,{"Year", "Month", "Day", "Latitude", "Longitude", "VARIABLES", "1", "2", "3", "4", "5", "6", "7", "8"}),
  24. #"Filled Down" = Table.FillDown(#"Replace: (blank) with null",{"Year", "Month", "Day", "Latitude", "Longitude"}),
  25. #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([VARIABLES] = "pH")),
  26. #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {"Year", "Month", "Day", "Latitude", "Longitude", "VARIABLES"}, "Meters", "Value"),
  27. #"Filter: Top 1 Meter" = Table.SelectRows(#"Unpivoted Other Columns", each ([Meters] = "1")),
  28. #"Changed Types" = Table.TransformColumnTypes(#"Filter: Top 1 Meter",{{"Year", Int32.Type}, {"Month", Int8.Type}, {"Day", Int8.Type}, {"Latitude", type number}, {"Longitude", type number}, {"VARIABLES", type text}, {"Meters", Int64.Type}, {"Value", type number}}, "en-us"),
  29. #"Custom: Date(Year, Month, Day)" = Table.AddColumn(#"Changed Types", "Date", each #date([Year],[Month],[Day])),
  30. #"Removed Original Date Columns" = Table.SelectColumns(#"Custom: Date(Year, Month, Day)",{"Date", "Latitude", "Longitude", "VARIABLES", "Meters", "Value"}),
  31. #"Type: Date" = Table.TransformColumnTypes(#"Removed Original Date Columns",{{"Date", type date}}, "en-us")
  32. in
  33. #"Type: Date"
Add Comment
Please, Sign In to add comment