Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
SevanT
Frequent Visitor

Function currency conversion

Hi Community,

 

I am new here and have been using PowerQuery more and more. 

 

Currently I am struggling with finding a solution to convert amounts from one currency to another. I have created a web query pulling exchange rates for a set date and set currencies (EUR, etc.) in a table. This table is stored centrally since I am using it for repeatedly for different purposes.  Something like this:

 

CurrencyFX to EUR
EUR1
USDxxxx
GBPxxxx

 

In a second table I have transactional data, i.e. an account balance with the additional currency detail. 

 

Supplier IDSupplier NameCurrency Amount
1AEUR100
2BUSD100
3CGBP100

 

So far I always just merge the tables to add FX to EUR column, convert the Amount to EUR in a new Column and delete the FX to EUR column. I would like to create a function that I can re-purpose repeatedly for other files as I am doing these kind of conversions regularly. 

 

How would I go about this? 

Thank you for any knind

1 ACCEPTED SOLUTION

You can use Record.Field as below

(sourceCurrency as text, Amount_Currency as number, column as text )=>
let
Source = tbFXRates,
FXRate = Record.Field(Table.SelectRows(Source, each ([Currency] = sourceCurrency )){0},Column),
Conversion = Value.Divide(Amount_Currency,FXRate)
in
Conversion

View solution in original post

4 REPLIES 4
ZhangKun
Resolver V
Resolver V

If your exchange rate table is accessed in real time through the network, then due to some complex mechanism problems of power query, you may get different results each time you query the exchange rate.

If your exchange rate table is stored on the local computer, then there is no such problem.

Assuming that the exchange rate table does not need to consider the problem of real-time update and the currency type is not repeated, you can use the following syntax instead of the Table.SelectRows function:

 

 

//Table.SelectRows(Source, each ([Currency] = sourceCurrency ))[FXRate EUR] {0}
Source{[Currency = sourceCurrency ]}[FXRate EUR]

// if not existed sourceCurrency, eg: RMB, should add try...otherwise
try Source{[Currency = sourceCurrency ]}[FXRate EUR] otherwise error "Not Exist"

 

 

 

Omid_Motamedise
Resident Rockstar
Resident Rockstar

Use the following code.

 

Just replace the Source table with your currency rate table

 

(Currency )=>

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcg0NUtJRMlSK1YlWCg12AbIrgADMdXcKgHNjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Currency = _t, #"FX to EUR" = _t]),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Currency] = Currency ))[FX to EUR]{0}
in
    #"Filtered Rows"

 

@Omid_Motamedise Thank you for quick reply. 

This is your code adjusted a little to fit my needs:

(sourceCurrency as text, Amount_Currency as number )=>
let
    Source = tbFXRates,
    FXRate = Table.SelectRows(Source, each ([Currency] = sourceCurrency ))[FXRate EUR] {0},
    Conversion = Value.Divide(Amount_Currency,FXRate)
in
    Conversion

 

There is one addition I would like to add to the code, which I cant figure out. As of now FXRate EUR is hard coded to the function. I would like to be add the column as a parameter so I can adjust the source and target currency as needed. How would I add [FXRate EUR] as a parameter in the function?

You can use Record.Field as below

(sourceCurrency as text, Amount_Currency as number, column as text )=>
let
Source = tbFXRates,
FXRate = Record.Field(Table.SelectRows(Source, each ([Currency] = sourceCurrency )){0},Column),
Conversion = Value.Divide(Amount_Currency,FXRate)
in
Conversion

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors