itsnotaboutthecell

Power Query M - Web Scraping USDA Results

Apr 5th, 2018
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. /*
  2. Created By: Alex Powers
  3. YouTube How To Video: https://www.youtube.com/watch?v=LwIod7Dm46s
  4. Website: http://www.itsnotaboutthecell.com
  5. LinkedIn: https://www.linkedin.com/in/alexmpowers/
  6. Instructions: Create Two Seperate Functions in Power Query M
  7. */
  8.  
  9. /* Return Total Number of Pages - Base 0 */
  10.  
  11. (produceType as text) as number =>
  12.  
  13. let
  14. Source = Web.Page(Web.Contents("https://ndb.nal.usda.gov/ndb/search/list?qlookup=" & produceType)),
  15. Data1 = Source{1}[Data],
  16. Children = Data1{0}[Children],
  17. Children1 = Children{1}[Children],
  18. Children2 = Children1{1}[Children],
  19. Children3 = Children2{1}[Children],
  20. Children4 = Children3{0}[Children],
  21. Children5 = Children4{10}[Children],
  22. #"Removed Other Columns" = Table.SelectColumns(Children5,{"Children"}),
  23. #"Expanded Children" = Table.ExpandTableColumn(#"Removed Other Columns", "Children", {"Text"}, {"Text"}),
  24. #"Changed Type" = Table.TransformColumnTypes(#"Expanded Children",{{"Text", Int8.Type}}),
  25. #"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Text"}),
  26. MaxCount = List.Max(Table.Column(#"Removed Errors", "Text")) - 1
  27. in
  28. MaxCount
  29.  
  30. /* Return Table */
  31.  
  32. (produceName as text, pageOffset as number) as table =>
  33. let
  34. Source = Web.Page(Web.Contents("https://ndb.nal.usda.gov/ndb/search/list?qlookup=" & produceName & "&offset=" & Text.From(pageOffset) & "&order=asc")),
  35. Data0 = Source{0}[Data],
  36. #"Removed Columns" = Table.RemoveColumns(Data0,{""})
  37. in
  38. #"Removed Columns"
Advertisement
Add Comment
Please, Sign In to add comment