March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi All,
I have a specific calculation that works OK when seperated to different columns, but hangs when trying to combine it all into one column.
The specifics:
I have a table that gets updated weekly that have the following columns: fileDate, id, total.
In order to get a monthly total for each id, first, I'm calclating the fileDate 4 weeks ago (previousDate).
To avoid 'missing' new id's, or old id's with no new activity - if the id in previousDate does not exist, I'm using the second-to-max date in which the id is found (alternateDate).
Finally I use this date to LOOKUPVALUE the old total to subtract from the total for each fileDate.
When this is seperated to two different columns as follows it works with no issues :
First column: determins which date to use for the id
.dateChoice =
VAR previousDate = MAXX(FILTER(ALL('table1'), 'table1'[fileDate] <= ('table1'[fileDate])-28), 'table1'[fileDate])
VAR alternateDate = CALCULATE(MAX('table1'[fileDate]), ALLEXCEPT('table1','table1'[id]),'table1'[isMaxDate] = FALSE())
RETURN
IF(previousDate <> 0 , previousDate,
IF(aslternateDate <> 0 , aslternateDate, 'table1'[fileDate]))
Second colum: LOOKUPVALUE for total using the date in the first column and subtract:
totalDiff =
VAR previousTotal = LOOKUPVALUE('table1'[total], 'table1'[fileDate], 'table1'[dateChoice], 'table1'[id], 'table1'[id])
RETURN
'table1'[total] - previousTotal
When I try to combine them into a single colum I get 'Working on it' stuck forever (I tried waiting 45 minuts before giving up)
VAR previousDate = MAXX(FILTER(ALL('table1'), 'table1'[fileDate] <= ('table1'[fileDate])-28), 'table1'[fileDate])
VAR alternateDate = CALCULATE(MAX('table1'[fileDate]), ALLEXCEPT('table1','table1'[id]),'table1'[isMaxDate] = FALSE())
VAR dateChoice = IF(previousDate <> 0 , previousDate,
IF(alternateDate <> 0 , alternateDate, 'table1'[fileDate]))
VAR previousTotal = LOOKUPVALUE('table1'[total],'table1'[fileDate], dateChoice, 'table1'[id], 'table1'[id])
RETURN 'table1'[total] - previousTotal
I guess I have two questions here
1 - What am I missing here? Since my table already has ~80 columns I really prefer to keep this number as low as possible.
2 - Is there a way to 'cancel' when 'Working on it' hangs without killing the task? This was the first time this happened to me and it was quite painful 🙂
Thank you!
Rather than using MAX and LOOKUPVALUE, just use TOPN
Total diff =
var currentTotal = 'table1'[total]
var currentDate = 'table1'[fileDate]
var prevTotal = SELECTCOLUMNS( TOPN(1, FILTER( ALLEXCEPT( 'table1', 'table1'[id]),
'table1'[fileDate] <= currentDate - 28 ), 'table1[fileDate], DESC), "@val", 'table1[total])
return currentTotal - prevTotal
Thank you for the reply! I really have a lot to learn in Power BI, most of the time I'm just using the tools I already know that do the job.
When trying the solution you offered I get the error "A circular dependency was detected" on Total diff column, do you have any thoughts on why this is happening?
Thanks again!
its possible that the column itself is being included in the TOPN calculations, try replacing that line with
var prevTotal = SELECTCOLUMNS( TOPN(1, SELECTCOLUMNS(
FILTER( ALLEXCEPT( 'table1', 'table1'[id]),
'table1'[fileDate] <= currentDate - 28 ),
"@fileDate", 'table1[fileDate], "@value", 'table1[total])
@fileDate, DESC), "@val", [@value])
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
23 | |
15 | |
12 | |
9 | |
8 |
User | Count |
---|---|
41 | |
32 | |
29 | |
12 | |
12 |