Forum Discussion

konradjonsson's avatar
konradjonsson
Icon for Helper II rankHelper II
2 years ago
Solved

How to improve DAX query performance - aggregation of multiple currencies

Hi.

Originally I had a DAX measure that gave the correct result on row level, but totals where wrong. See bottom of message.

I had a previous solution where the iteration is at a granular level for each row and then aggregates these results. I knew it was going to be slower, but tried it anyway.

 

My question to the community is if there are any better (=faster) ways to aggregate correctly on multiple currencies?

 

Plan (Budget) SLOW =  

SUMX('Plan data', 

    VAR _recordDate = 'Plan data'[DateIntKey] 

    VAR _recordCurrency = 'Plan data'[CurrencyKey] 

    VAR _displayCurrency =

SELECTEDVALUE('Slicer - Targetcurrency'[Currency_BK]) 

    VAR _exchangeRateType = [Exchange Rate Type] 

    VAR _exchangeRate = CALCULATE( 

        MAX('FactExchangeRate'[Rate]), 

        'FactExchangeRate'[Date_SK] = _recordDate, 

        'FactExchangeRate'[FromCurrencyCode] = _recordCurrency, 

        'FactExchangeRate'[ExchangeRateType] = _exchangeRateType, 

        'FactExchangeRate'[ToCurrencyCode] = _displayCurrency 

    ) 

    VAR _valueInRecordCurrency = [Plan (Budget) LOCAL] 

 

    RETURN 

        _valueInRecordCurrency * _exchangeRate 

) 

 

 

The original DAX query - actually two.

Plan (Budget) INCORRECT TOTALS 

VAR LocalAmount = 

    CALCULATE( 

        SUM('Plan data'[Value]), 

        'Plan data'[ScenarioKey] = "EF", 

        'Plan data'[MeasureKey] = "M2" 

    ) 

 

RETURN 

    LocalAmount * [Exchange Rate] 

 

 

Exchange Rate 

VAR _date = 

    MAXX ( 'Plan data', 'Plan data'[DateIntKey] ) 

 

VAR _currency = 

    MAXX ( 

        'Plan data', 

        'Plan data'[CurrencyKey] 

    ) 

 

VAR _DisplayCurrency = 

    SELECTEDVALUE ( 'Slicer - Targetcurrency'[Currency_BK] ) 

      

VAR _exchangeratetype = [Exchange Rate Type] 

 

RETURN 

    CALCULATE ( 

        MAX ( 'FactExchangeRate'[Rate] ), 

        'FactExchangeRate'[Date_SK] = _date, 

        'FactExchangeRate'[FromCurrencyCode] = _currency, 

        'FactExchangeRate'[ExchangeRateType] = _exchangeratetype, 

        'FactExchangeRate'[ToCurrencyCode] = _displaycurrency 

    ) 

1 Reply