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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Jensk
Frequent Visitor

FX Rate calculation (works), but performance is poor. Help me DAX (link to sample file included)

I am building a balance sheet statement that I need to convert to USD. I have manipulated my data in a way that it always calculates the YTD amount depending on the column in the matrix. My file produces the desired result, but the calculation time is in the 30.000-45.000 ms range for the most heavy ones. I need help to alter my DAX in a way that reduces these.

 

In my real file I have a fact table of 227,515 rows and 22 columns that I crossjoin into a table of 3 million rows and 22 columns. I suspect this is the reason for the poor performance, but it is the only way I can make it work as desired. I am looking for any suggestions to improve the DAX and thus the performance of my report!

 

Here is a link to sample file with the desired result: https://drive.google.com/file/d/1t5JklmELBKq5urP0hRrlfY50AG6eiB2x/view?usp=sharing 

 

My Data:

DATEACCOUNTCURAMOUNTPERIODIDIndex
01/01/2023CashSEKSEK100.00001
01/01/2023CashUSDUSD10002
01/01/2023CashGBPGBP1.00003
01/01/2023CashSEKSEK50.00014
01/01/2023CashUSDUSD5015
01/01/2023CashGBPGBP50016
01/01/2023ReceivablesDKK1.00017
01/01/2023ReceivablesEUR1018
01/01/2023ReceivablesUSD5019
01/02/2023ReceivablesSEK10.000210
01/02/2023ReceivablesEUR20211
01/02/2023ReceivablesUSD160212
01/03/2023ReceivablesGBP25.000313
01/03/2023ReceivablesEUR30314
01/03/2023ReceivablesUSD250315

 

FX Rates:

Jensk_0-1715087653519.png

 

Jensk_1-1715087712914.png

 

My Desired result:

Jensk_2-1715087765835.png

 

Example calculation for Receivables in March (Period ID 3):

Jensk_3-1715087803742.png

 

Things worth noting:

  • I need to present it with 13 columns: periods 0 through 12 (I only included 6 months in the example for limitation reasons)
  • Period 0 represents the opening balance which is last years end balance. Periods 1 through 12 represent the months of the year Jan through Dec
  • Transactions in period 0 is always 01-01-2023, but period 1 might also contain the same date, so date cannot be used as columns
  • The fx rate date for period 0 is 31-12-2022 (because it is last years end balance carried forward)

 

The steps I made to get the result (see my file):

  • I create a calculated table 'PERIOD' with values 0 through 12
  • Then in order to calculate a value in fields that are blank I create: CrossJoin_Table = CROSSJOIN(FACTTABLE,PERIOD)
  • In my CrossJoin_Table I add three calculated columns: CrossjoinBalance, CrossjoinBalAcc and USD_rate
  • Finally I create my measure 'own_solution' that provides the desired result
CrossjoinBalance = 
CALCULATE(
    SUM(FACTTABLE[AMOUNT]),
    FILTER(
        FACTTABLE,
        FACTTABLE[PERIODID] = CrossJoin_Table[PERIOD_ID_ALL]
            && FACTTABLE[Index] = CrossJoin_Table[Index]
    )
)
CrossjoinBalAcc = 
VAR CurrentPeriod = CrossJoin_Table[PERIOD_ID_ALL]
VAR Index = CrossJoin_Table[Index]
VAR FilteredTable =
    FILTER(
        CrossJoin_Table,
        CrossJoin_Table[PERIOD_ID_ALL] <= CurrentPeriod
            &&  Index = CrossJoin_Table[Index]
    )
RETURN
    CALCULATE( SUM(CrossJoin_Table[CrossjoinBalance]),
    FilteredTable
    )
USD_rate = 
VAR FX_Date = 
SWITCH(
    TRUE(),
    CrossJoin_Table[PERIOD_ID_ALL] = 0, DATE(2022, 12, 31),
    CrossJoin_Table[PERIOD_ID_ALL] = 1, DATE(2023, 1, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 2, DATE(2023, 2, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 3, DATE(2023, 3, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 4, DATE(2023, 4, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 5, DATE(2023, 5, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 6, DATE(2023, 6, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 7, DATE(2023, 7, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 8, DATE(2023, 8, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 9, DATE(2023, 9, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 10, DATE(2023, 10, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 11, DATE(2023, 11, 1),
    CrossJoin_Table[PERIOD_ID_ALL] = 12, DATE(2023, 12, 1),
    BLANK()
)
RETURN
    LOOKUPVALUE(
        FXRATE[Rate],
        FXRATE[Currency], CrossJoin_Table[CUR],
        FXRATE[Date], FX_Date
    )
own_solution = 
SUMX(
    CrossJoin_Table,
    CrossJoin_Table[CrossjoinBalAcc] * CrossJoin_Table[USD_rate]
) 

 

I hope someone can come up with a smarter solution that yields the same results!

1 REPLY 1
Greg_Deckler
Super User
Super User

@Jensk Have you tested these measures individually to see where the performance issue might lie? The calculations you are doing strike me as potentially similar to a recent CALCULATE challenge: Solved: Re: CALCULATE Challenge - Round 1 - Microsoft Fabric Community


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.