Hi all,
I am new to the forum and looking forward to start working my way through the learning content, however in the meantime I am stuck on a DAX query which I am trying to fix which was written by a previous colleague. There are huge amounts of sensitive data in the pbix file as it is our banking data, so I was hoping someone may be able to take a look at the query and let me know if anything jumps out immediately as being bad practice.
Measure = VAR theLastDate=MAX(Dates[Date])
RETURN CALCULATE(
SUMX(
FILTER( 'All Balances',
'All Balances'[AsAtDate] = CALCULATE(
MAX('All Balances'[AsAtDate]),
FILTER( 'All Balances',
'All Balances'[AccountNo]=EARLIER('All Balances'[AccountNo])
),
'All Balances'[AsAtDate]<=theLastDate,
FILTER(ALL(Dates),[Is Not Future]) --Exclude future values
) &&
'All Balances'[AsAtDate]<=theLastDate
),
'All Balances'[Balance_ABS]
),
'All Balances'[AsAtDate]<=theLastDate,
FILTER(ALL(Dates),[Is Not Future]) --Exclude future values
)
It seems to be very repetitive and I canāt quite figure out whether the EARLIER function is being used correctly.
The measure alone is okay, however when evaluated against the date table it is slow.
So far I have done the below (highlighted in bold) to try and improve the speed:
Measure = VAR theLastDate=MAX(Dates[Date])
RETURN CALCULATE(
SUMX(
FILTER( āAll Balancesā,
āAll Balancesā[AsAtDate] = CALCULATE(
MAX(āAll Balancesā[AsAtDate]),
FILTER( āAll Balancesā,
āAll Balancesā[AccountNo]=EARLIER(āAll Balancesā[AccountNo])
),
āAll Balancesā[AsAtDate]<=theLastDate,
FILTER(ALL(Dates**[Date]**), Dates[Date] <= Today() --Exclude future values
), USERELATIONSHIP(āAll Balancesā[AsAtDate], Dates[Date]) &&
āAll Balancesā[AsAtDate]<=theLastDate
),
āAll Balancesā[Balance_ABS]
),
āAll Balancesā[AsAtDate]<=theLastDate,
FILTER(ALL(Dates),[Is Not Future]) --Exclude future values
)
There is already a relationship between the AsAtDate column and the Dates date column.
Another thing I tried was using TREATAS(VALUES(Datesā[Date]), AllBalances[AsAtDate]) instead of the USERELATIONSHIP formula
This worked really quickly in DAX Studio, however in Power BI itself, it said my formula was too complex and I got a memory failure too.
Hopefully I have provided enough information, I always find it tricky picking up someone elseās work as itās hard to understand if there was a reason why they have written it that way. I feel like I could be missing something really obvious hence posting here
Thanks in advance!!Preformatted text