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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
IvanS
Helper V
Helper V

Exchange rate for specific date range based on posting date

Dear all,

 

could you please help me with writing following DAX formula as calculated column "Exchange rate" in my FACT_Invoices table.

 

I have table DIM_ExchangeRates that contains rates that are updated with irregular frequency - only in case of significant rate change. As the table consists of duplicates, there is no relationship to to the FACT_Invoices table.

 

CurrencyExchange RateValid From
PLN4.31.1.2020
USD1.21.3.2020
PLN4.51.6.2021
USD1.11.1.2022
PLN4.61.1.2022

 

Sample of FACT_Invoices tables is following. I am trying to get the exchange rate valid for the specific currency and valid within the given date range from DIM_ExchangeRates table. If the currency is EUR, then set the exchange rate to 1, as the next step will be to create simple DIVIDE function Amount in EUR = Amount/Exchange rate.

 

Invoice No.AmountPosting DateCurrencyExchange rateAmount in EUR
1100015.3.2021EUR1 
25000031.5.2021PLN4.3 
350024.5.2020EUR1 
430001.11.2021USD1.2 
59000030.12.2021PLN4.5 

 

Thank you for any help!

Ivan

2 ACCEPTED SOLUTIONS
DataInsights
Super User
Super User

@IvanS,

 

Try this calculated column in FACT_Invoices:

 

Exchange rate = 
VAR vPostingDate = FACT_Invoices[Posting Date]
VAR vCurrency = FACT_Invoices[Currency]
VAR vMaxValidFrom =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Valid From] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] < vPostingDate
    )
VAR vRate =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Exchange Rate] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] = vMaxValidFrom
    )
VAR vResult =
    IF ( vCurrency = "EUR", 1, vRate )
RETURN
    vResult

 

DataInsights_0-1651672137206.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

@IvanS,

 

Glad to hear that worked. Thanks for letting me know--I've updated the DAX:

 

Exchange rate = 
VAR vPostingDate = FACT_Invoices[Posting Date]
VAR vCurrency = FACT_Invoices[Currency]
VAR vMaxValidFrom =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Valid From] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] <= vPostingDate
    )
VAR vRate =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Exchange Rate] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] = vMaxValidFrom
    )
VAR vResult =
    IF ( vCurrency = "EUR", 1, vRate )
RETURN
    vResult




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@IvanS,

 

Try this calculated column in FACT_Invoices:

 

Exchange rate = 
VAR vPostingDate = FACT_Invoices[Posting Date]
VAR vCurrency = FACT_Invoices[Currency]
VAR vMaxValidFrom =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Valid From] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] < vPostingDate
    )
VAR vRate =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Exchange Rate] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] = vMaxValidFrom
    )
VAR vResult =
    IF ( vCurrency = "EUR", 1, vRate )
RETURN
    vResult

 

DataInsights_0-1651672137206.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you very much! Very helpful solution! Just one small adjustment in the code - can you please change that in your post for other users?

 

DIM_ExchangeRates[Valid From] <= vPostingDate

 

@IvanS,

 

Glad to hear that worked. Thanks for letting me know--I've updated the DAX:

 

Exchange rate = 
VAR vPostingDate = FACT_Invoices[Posting Date]
VAR vCurrency = FACT_Invoices[Currency]
VAR vMaxValidFrom =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Valid From] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] <= vPostingDate
    )
VAR vRate =
    CALCULATE (
        MAX ( DIM_ExchangeRates[Exchange Rate] ),
        DIM_ExchangeRates[Currency] = vCurrency,
        DIM_ExchangeRates[Valid From] = vMaxValidFrom
    )
VAR vResult =
    IF ( vCurrency = "EUR", 1, vRate )
RETURN
    vResult




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.