Forum Discussion

IvanS's avatar
IvanS
Icon for Helper V rankHelper V
4 years ago
Solved

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

  • 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

     

     

  • 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

3 Replies

  • 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

     

     

    • IvanS's avatar
      IvanS
      Icon for Helper V rankHelper V

      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

       

      • DataInsights's avatar
        DataInsights
        Icon for Super User rankSuper User

        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