Passing Variable in URL (Power Query)- Loop vs Parameter

goob90

New Member
Joined
Nov 12, 2021
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hello everyone, and happy almost Thanksgiving!

I am working on a project in Power Query and need advice as to how I should pass a variable in my URL. The URL is for free access to an API that allows 100 combined request/day.

The original URL has a spot where a UPC code can be changed to lookup an item:

5010993689071

Currently, I have a list using Textjoin and I have gotten it to work somewhat. However, the URL I am using will only return two values at a time. So anything past the first two UPC codes does not get returned.

Power Query:
let
    List=Excel.CurrentWorkbook(){[Name="UPCList"]}[Content]{0}[Column1],
    Source = Json.Document(Web.Contents("https://api.upcitemdb.com/prod/trial/lookup?upc="&List&" ")),
    items = Source[items],
    #"Converted to Table" = Table.FromList(items, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"ean", "title", "description", "brand", "model", "color", "size", "dimension", "weight", "category", "currency", "lowest_recorded_price", "highest_recorded_price", "images", "offers", "elid"}, {"Column1.ean", "Column1.title", "Column1.description", "Column1.brand", "Column1.model", "Column1.color", "Column1.size", "Column1.dimension", "Column1.weight", "Column1.category", "Column1.currency", "Column1.lowest_recorded_price", "Column1.highest_recorded_price", "Column1.images", "Column1.offers", "Column1.elid"})
in
    #"Expanded Column1"

Would I be better off running some sort of loop that goes through the list one by one? If so, how would I do that? I am open to other solutions as well.

Thank you all so much!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If you create a table of your UPC codes and load it into PQ, you can then add a column to that table with a function to get the info.

Custom function

Power Query:
(upc as text) as table => 
let
    Source = Json.Document(Web.Contents("https://api.upcitemdb.com/prod/trial/lookup?upc=" & upc)),
    items = Source[items],
    items1 = items{0},
    toTable = Record.ToTable(items1)
in
    toTable

UPC List

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"APIs", type text}}),
    UPC = Table.AddColumn(Type, "Custom", each FN([APIs])),
    Expand = Table.ExpandTableColumn(UPC, "Custom", {"Name", "Value"}, {"Name", "Value"})
in
    Expand

New__Document (8).xlsx
ABC
25010993689071ean5010993689071
35010993689071titleHasbro - Ghostbusters Plasma Series Winston Zeddemore Action Figure
45010993689071descriptionIncludes figure and 3 accessories
55010993689071brandHasbro
65010993689071model78325700
75010993689071colorsilver,grey
85010993689071size
95010993689071dimension
105010993689071weight1.25 lbs
115010993689071categoryToys & Games > Toys > Dolls, Playsets & Toy Figures > Action & Toy Figures
125010993689071currency
135010993689071lowest_recorded_price15.97
145010993689071highest_recorded_price155
155010993689071images
165010993689071offers
175010993689071elid265386804334
18653569732259ean0653569732259
19653569732259titleTwister
20653569732259descriptionThis Twister game is the classic game with 2 more moves.
21653569732259upc653569732259
22653569732259brandHasbro
23653569732259model4645
24653569732259colorBlack
25653569732259size
26653569732259dimension10 X 10 inches
27653569732259weight0.00 lb
28653569732259categoryToys & Games > Games > Board Games
29653569732259currencyCAD
30653569732259lowest_recorded_price0
31653569732259highest_recorded_price60.55
32653569732259images
33653569732259offers
34653569732259asinB008J87PVC
35653569732259elid194456117411
Sheet4
 
Upvote 0
Thank you! That worked pretty well.


1637788380355.png

Is there a way to transpose the multiple tables at once? I'd like to have fields as headers with the product data below them.
 
Upvote 0
Like this?

Book1 (version 1).xlsb
ABCDEFGHIJKLMNOPQR
1UPCtitledescriptionbrandmodelcolorsizedimensionweightcategorycurrencylowest_recorded_pricehighest_recorded_priceimagesofferselidupc.1asin
25010993689071Hasbro - Ghostbusters Plasma Series Winston Zeddemore Action FigureIncludes figure and 3 accessoriesHasbro78325700silver,grey1.25 lbsToys & Games > Toys > Dolls, Playsets & Toy Figures > Action & Toy Figures15.97155265386804334
3653569732259TwisterThis Twister game is the classic game with 2 more moves. Give the spinner a whirl and see what's next as you try to keep your hands and feet on the mat! Right foot red! Can you do it? Left foot green, you got it! If your knee or elbow touches the mat, or you fall over, you're out.Hasbro4645Black10 X 10 inches0.00 lbToys & Games > Games > Board GamesCAD060.55194456117411653569732259B008J87PVC
Table1


Table1

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"UPC", type text}}),
    UPC = Table.AddColumn(Type, "Custom", each FN([UPC])),
    Expand = Table.ExpandRecordColumn(UPC, "Custom", {"title", "description", "brand", "model", "color", "size", "dimension", "weight", "category", "currency", "lowest_recorded_price", "highest_recorded_price", "images", "offers", "elid", "upc", "asin"}, {"title", "description", "brand", "model", "color", "size", "dimension", "weight", "category", "currency", "lowest_recorded_price", "highest_recorded_price", "images", "offers", "elid", "upc.1", "asin"})
in
    Expand

Function

Power Query:
(upc as text) as record => 
let
    Source = Json.Document(Web.Contents("https://api.upcitemdb.com/prod/trial/lookup?upc=" & upc)),
    items = Source[items],
    items1 = items{0}
in
    items1
 
Upvote 0
Like this?

Book1 (version 1).xlsb
ABCDEFGHIJKLMNOPQR
1UPCtitledescriptionbrandmodelcolorsizedimensionweightcategorycurrencylowest_recorded_pricehighest_recorded_priceimagesofferselidupc.1asin
25010993689071Hasbro - Ghostbusters Plasma Series Winston Zeddemore Action FigureIncludes figure and 3 accessoriesHasbro78325700silver,grey1.25 lbsToys & Games > Toys > Dolls, Playsets & Toy Figures > Action & Toy Figures15.97155265386804334
3653569732259TwisterThis Twister game is the classic game with 2 more moves. Give the spinner a whirl and see what's next as you try to keep your hands and feet on the mat! Right foot red! Can you do it? Left foot green, you got it! If your knee or elbow touches the mat, or you fall over, you're out.Hasbro4645Black10 X 10 inches0.00 lbToys & Games > Games > Board GamesCAD060.55194456117411653569732259B008J87PVC
Table1


Table1

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"UPC", type text}}),
    UPC = Table.AddColumn(Type, "Custom", each FN([UPC])),
    Expand = Table.ExpandRecordColumn(UPC, "Custom", {"title", "description", "brand", "model", "color", "size", "dimension", "weight", "category", "currency", "lowest_recorded_price", "highest_recorded_price", "images", "offers", "elid", "upc", "asin"}, {"title", "description", "brand", "model", "color", "size", "dimension", "weight", "category", "currency", "lowest_recorded_price", "highest_recorded_price", "images", "offers", "elid", "upc.1", "asin"})
in
    Expand

Function

Power Query:
(upc as text) as record =>
let
    Source = Json.Document(Web.Contents("https://api.upcitemdb.com/prod/trial/lookup?upc=" & upc)),
    items = Source[items],
    items1 = items{0}
in
    items1
Perfect! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top