Forum Discussion
Historical exchange rate API
- Anonymous7 years ago
Anonymous I would suggest first creating a table with sequential dates on each row. Then by invoking the function, it will send the dates to the function one by one thus returning the exchange rate info for each day. You can use this example which first generates a list of dates of the past 365 days (this is updated with every refresh) and invokes the function:
let Source = {Number.From(Date.AddDays(DateTime.Date(DateTime.LocalNow()),-365))..Number.From(DateTime.Date(DateTime.LocalNow()))}, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}}), #"Invoked Custom Function" = Table.AddColumn(#"Renamed Columns", "ExchangeRates", each Fn_GetHistoricExchangeRates(Date.ToText([Date],"yyyy-MM-dd"))), #"Expanded ExchangeRates" = Table.ExpandTableColumn(#"Invoked Custom Function", "ExchangeRates", {"Currency", "Rate"}, {"Currency", "Rate"}), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded ExchangeRates",{{"Rate", type number}, {"Currency", type text}}) in #"Changed Type1"In order for it to work you should create a function named Fn_GetHistoricExchangeRates, by using the code in my previous post.
Hope that helps!
Jan
Anonymous I have recently created a simple function that extracts exchange rate information from the site https://exchangeratesapi.io/ which gets its data from the ECB site:
let
Source = (Date as text) => let
Source = Json.Document(Web.Contents("https://api.exchangeratesapi.io/" & Date & "?base=USD")),
rates = Source[rates],
#"Converted to Table" = Record.ToTable(rates),
#"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Name", "Currency"}, {"Value", "Rate"}})
in
#"Renamed Columns"
in
SourceUsing a datetable or generated range of dates, you can then invoke the custom function like this:
= Table.AddColumn(#"Removed Other Columns", "Rates", each Fn_GetHistoricCurrencyRates(DateTime.ToText([Date],"yyyy-MM-dd")))
Hope that helps!
Jan
Anonymous this looks promising, I stumbled upon that site as well.
I am totally inexperienced with this; "invoke function" lets me enter parameter as date - should I fill in a range of dates and then use the second function you mention?
- Anonymous7 years agoNot applicable
Anonymous I would suggest first creating a table with sequential dates on each row. Then by invoking the function, it will send the dates to the function one by one thus returning the exchange rate info for each day. You can use this example which first generates a list of dates of the past 365 days (this is updated with every refresh) and invokes the function:
let Source = {Number.From(Date.AddDays(DateTime.Date(DateTime.LocalNow()),-365))..Number.From(DateTime.Date(DateTime.LocalNow()))}, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Column1", "Date"}}), #"Invoked Custom Function" = Table.AddColumn(#"Renamed Columns", "ExchangeRates", each Fn_GetHistoricExchangeRates(Date.ToText([Date],"yyyy-MM-dd"))), #"Expanded ExchangeRates" = Table.ExpandTableColumn(#"Invoked Custom Function", "ExchangeRates", {"Currency", "Rate"}, {"Currency", "Rate"}), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded ExchangeRates",{{"Rate", type number}, {"Currency", type text}}) in #"Changed Type1"In order for it to work you should create a function named Fn_GetHistoricExchangeRates, by using the code in my previous post.
Hope that helps!
Jan- mariusmc25 years agoRegular Visitor
Just note that this cannot be used in Power BI Service, it falls under a dynamic dataset and cannot be refreshed:
https://docs.microsoft.com/en-us/power-bi/connect-data/refresh-data#refresh-and-dynamic-data-sources
- KristenC4 years agoFrequent Visitor
You can rephrase the query like this using RelativePath (see Chris Webb's excellent blog about this workaround for dynamic datasources):
let Source = (Date as text) => let Source = Json.Document( Web.Contents("https://api.exchangeratesapi.io/", [RelativePath = "Date", Query = [base="USD"]])), rates = Source[rates], #"Converted to Table" = Record.ToTable(rates), #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Name", "Currency"}, {"Value", "Rate"}}) in #"Renamed Columns" in Source