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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
aldpbi
Frequent Visitor

Substracting subtotal from subsequent subtotals (DAX formula)

Hi everyone, 

 

 I've been struggling lately to find a way to calculate the Delta column in PBI using DAX, as shown in below output example:

 

 Calc_eg.PNG

 

Just keep in mind the data is part of a larger model, and I need to retain the full flexibility of slicing the data.

 

Table structure example there this data:

Calendar WeekFactoryCategoryStock (past)
     

 

Could maybe more experienced help me with a hint in this endeavour?

 

Thanks a lot,

Cristian

1 ACCEPTED SOLUTION
aldpbi
Frequent Visitor

Hi all,

 

In case anyone interested, I was able to crack the puzzle with this measure:

Delta_measure = 
var maxweek = calculate(max('Table'[Calendar Week]),all('Table'),ISBLANK('Table'[Stock Past])=FALSE())
var laststock = calculate(sum('Table'[Stock Past]),'Table'[Calendar Week]=maxweek)

RETURN
if(LASTNONBLANK('Table'[Stock Future],'Table'[Stock Future]),ABS(laststock - sum('Table'[Stock Future])))

delta_measure_solved.PNG

It is still not very clear why only the above syntax works, compared with any of these:

 

calculate(ABS(laststock - sum('Table'[Stock Future])),LASTNONBLANK('Table'[Stock Future],'Table'[Stock Future]))

calculate(ABS(laststock - sum('Table'[Stock Future])),'Table'[Stock Future]<>BLANK())

 

Cheers,

Cristian

View solution in original post

3 REPLIES 3
v-yuta-msft
Community Support
Community Support

Hi aldpbi,

 

 

To achieve your requirement, create a calculate column using DAX formula below:

Delta = 
IF(ISBLANK(Table1[Stock(future)]), BLANK(), ABS(Table1[Stock(future)] - MAX(Table1[Stock(past)])))

Capture.PNG 

 

Regards,

Jimmy Tao

 

Hi @v-yuta-msft,

 

Thanks so much for looking into it.

 

However, I don't think the calculated column does the job (see below screenshot). I was able to move closer to the solution by using this calculated measure:

 

Delta_measure = 
var maxweek = calculate(max('Table'[Calendar Week]),all('Table'),ISBLANK('Table'[Stock Past])=FALSE())
var laststock = calculate(sum('Table'[Stock Past]),'Table'[Calendar Week]=maxweek)

RETURN
ABS(laststock-sum('Table'[Stock Future]))

 

I still need a way to eliminate the past values' calculation when adding in a the table context:

delta_measure.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

Any Idea how I can achieve that? I would need to show blanks in above measure, where there's no future data.

 

I have added the PBX file with the calculation sample, for better context.

https://drive.google.com/file/d/1xrqMdHcmlbr3AzQ-eGi2LPUQ5o83YANF/view?usp=sharing

(hosted on g.drive - not aware of a better way to share the file on the platform directly)

 

Thanks a lot,

Cristian

 

aldpbi
Frequent Visitor

Hi all,

 

In case anyone interested, I was able to crack the puzzle with this measure:

Delta_measure = 
var maxweek = calculate(max('Table'[Calendar Week]),all('Table'),ISBLANK('Table'[Stock Past])=FALSE())
var laststock = calculate(sum('Table'[Stock Past]),'Table'[Calendar Week]=maxweek)

RETURN
if(LASTNONBLANK('Table'[Stock Future],'Table'[Stock Future]),ABS(laststock - sum('Table'[Stock Future])))

delta_measure_solved.PNG

It is still not very clear why only the above syntax works, compared with any of these:

 

calculate(ABS(laststock - sum('Table'[Stock Future])),LASTNONBLANK('Table'[Stock Future],'Table'[Stock Future]))

calculate(ABS(laststock - sum('Table'[Stock Future])),'Table'[Stock Future]<>BLANK())

 

Cheers,

Cristian

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.