Forum Discussion

MiraAbel's avatar
MiraAbel
Frequent Visitor
2 years ago
Solved

Initial Balance converted at specific rate - Running Total multiple currency conversion

Hello DAX wizzards!  Hello OwenAuger ! I am trying to solve a combined issue presented by Alberto Ferrari in YouTube SQLBI topic - Show the initial balance for any date selection in Power BI http...
  • calerof's avatar
    2 years ago

    Hi MiraAbel,

    I have created a measure to calculate the exchange rate for the opening balance with this DAX code:

     

    Exchange Rate = 
    VAR CurrentMonth = SELECTEDVALUE( BalanceDate[Month Name] )
    VAR CurrentCurrency = SELECTEDVALUE( dimCurrency[CurrencyId] )
    VAR BeginningDate =
        CALCULATE( MIN( dimDates[Date Key] ), ALLSELECTED() )
    VAR PreviousDate = ENDOFMONTH( PREVIOUSMONTH( DATESINPERIOD( BalanceDate[Date Key], BeginningDate, -1, MONTH ) ) )
    VAR DenominatorDate = IF(CurrentMonth = "Open balance", PreviousDate, LASTDATE( dimDates[Date Key] ) )
    VAR ExchangeRate = LOOKUPVALUE( dimCurrExchange[Exchange Rate], dimCurrency[CurrencyId], CurrentCurrency, dimCurrExchange[DateKey], DenominatorDate )
    RETURN
        ExchangeRate

     

    My first thought was to know what date is the opening balance, so I calculated the VAR PreviousDate above.
    Then, to look for the exchange rate of that date I'm using the VAR ExchangeRate above with the function LOOKUPVALUE with date and currency id. Then when we don't have a date but a label "Open balance" I used the VAR DenominatorDate to use the PreviousDate instead. This returns the correct exchange rate for every row.

    Finally, I modified your measure "Balance_EUR (work in progress) like this:

     

    Balance_EUR (work in progress) = 
    VAR CurrentMonth = SELECTEDVALUE ( BalanceDate[Month Name] )
    VAR Running_EUR = 
    --solved!
    SUMX (
        dimCurrency,
        DIVIDE(
            [Running Total_LCurr],
            CALCULATE (
                VALUES ( dimCurrExchange[Exchange Rate] ),
                LASTDATE ( dimDates[Date Key])
            )
        )
    )
    
    VAR DateStart = CALCULATE ( MIN ( dimDates[Date Key] ), ALLSELECTED () )
    VAR OpenBalanceDate = CALCULATE(EOMONTH( MIN ( 'dimDates'[Date Key] ),-1), ALLSELECTED())
    
    VAR OpenBalance = CALCULATE ( [Sum_Transaction_LCurr], dimDates[Date Key] < DateStart )
    --OpenBalance is still work in progress
    
    VAR IsGrandTotal = 
        COUNTROWS ( dimDates ) = CALCULATE ( COUNTROWS ( dimDates), ALLSELECTED( ) )
    VAR OpenBalanceConversion = 
        DIVIDE(
            OpenBalance,
            [Exchange Rate]
        )
    VAR Result = 
        IF ( 
            CurrentMonth = "Open balance",
            OpenBalanceConversion,
            -- 100 is placefolder for OpenBalance
            IF ( 
                IsGrandTotal, Running_EUR + OpenBalanceConversion, Running_EUR
            )
        )
    RETURN
        Result

     

    Where I added the VAR OpenBalanceConversion to make the conversion of the open balance in local currency, which I use in the final IF statement when the CurrentMonth is "Open balance".
    I hope it works for you and I'm glad I could help 😜

    Cheers,

    Fernando
    P.S. Here's the pbix file.