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
How did you get that date into PowerBI? I had trouble with the navigation to find the dataset within the XML file.
In my example, I needed only 3 currencies EUR, GBP and PLN, and grouped average by month.
See the M code below.
/*
let
Source = Xml.Tables(Web.Contents("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml")),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"subject", type text}}),
#"Expanded Sender" = Table.ExpandTableColumn(#"Changed Type", "Sender", {"name"}, {"Sender.name"}),
#"Expanded http://www.ecb.int/vocabulary/2002-08-01/eurofxref" = Table.ExpandTableColumn(#"Expanded Sender", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref", {"Cube"}, {"Cube"}),
#"Expanded Cube" = Table.ExpandTableColumn(#"Expanded http://www.ecb.int/vocabulary/2002-08-01/eurofxref", "Cube", {"Cube"}, {"Cube.1"}),
#"Expanded Cube.1" = Table.ExpandTableColumn(#"Expanded Cube", "Cube.1", {"Cube", "Attribute:time"}, {"Cube", "Attribute:time"}),
#"Expanded Cube1" = Table.ExpandTableColumn(#"Expanded Cube.1", "Cube", {"Attribute:currency", "Attribute:rate"}, {"Attribute:currency", "Attribute:rate"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Cube1", each ([#"Attribute:currency"] = "GBP" or [#"Attribute:currency"] = "PLN")),
#"Replaced Value" = Table.ReplaceValue(#"Filtered Rows",".",",",Replacer.ReplaceText,{"Attribute:rate"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute:rate", type number}, {"Attribute:time", type date}, {"Attribute:currency", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"subject", "Sender.name"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Attribute:time", "Date"}}),
#"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[#"Attribute:currency"]), "Attribute:currency", "Attribute:rate", List.Sum),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "EUR", each 1),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"EUR", type number}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type2", {"Date"}, "Attribute", "Value"),
#"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Date", Order.Descending}}),
#"Inserted Year" = Table.AddColumn(#"Sorted Rows", "Year", each Date.Year([Date]), Int64.Type),
#"Inserted Month" = Table.AddColumn(#"Inserted Year", "Month", each Date.Month([Date]), Int64.Type),
#"Inserted Merged Column" = Table.AddColumn(#"Inserted Month", "CURR_ID", each Text.Combine({[Attribute], Text.From([Year], "nl-BE"), Text.From([Month], "nl-BE")}, "-"), type text),
#"Removed Other Columns1" = Table.SelectColumns(#"Inserted Merged Column",{"CURR_ID", "Value"}),
#"Grouped Rows" = Table.Group(#"Removed Other Columns1", {"CURR_ID"}, {{"FX", each List.Average([Value]), type number}})
in
#"Grouped Rows"
*/
- PhilipHradil2 years agoNew Member
Hi,
Thanks a million! That was exactly what I needed but for other currencies (which I easily can adjust).
I have made the first tests and this looks brilliant.
You made my week!
// Philip