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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
mandronic
Frequent Visitor

Calculate the difference between a value of a subcategory

Hi everyone,

 

I have the below dataset (sample), where Diff = Actual - Budget:

tableA.JPG

 

Currently, I used the below code to calculate the Diff column:

Diff =

var SubCatA = CALCULATE(

SUM('Table'[Value]),

FILTER('Table',

'Table'[Sub Categoty]="Actual" &&

'Table'[Category]="A")

)

 

var SubCatB= CALCULATE(

SUM('Table'[Value]),

FILTER('Table',

'Table'[Sub Categoty]="Budget" &&

'Table'[Category]="A")

)

 

return SubCatA-SubCatB

 

However, with this code I get the below result: 

TableB.JPG

 

Can anyone help out with this?

How can I get the Diff result as shown on the first table, for only Category A in the Actual line?

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Your solution will be simpler if you transform this data into a 3 column one - Category, Actual and Budget. using Power Query.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Pady
Helper II
Helper II

Hope this helps:

diff =
var bud = calculate(SUM(Data[Value]),Data[Sub Category]="Budget")
var diff = IF (SELECTEDVALUE(Data[Sub Category])="Actual",SELECTEDVALUE(Data[Value])-bud,"")
RETURN
diff
Pady_0-1693513834667.png

 

 
Anonymous
Not applicable

Hi  @mandronic ,

 

Here are the steps you can follow:

1. Create calculated column.

If each Category group implements the above logic, you can use the following dax:

Diff 1 =
var Actual=
SUMX(
    FILTER(ALL('Table'),
    'Table'[Category]=EARLIER('Table'[Category])&&'Table'[Sub Category]="Actual"),[Value])
var Budget=
SUMX(
    FILTER(ALL('Table'),
    'Table'[Category]=EARLIER('Table'[Category])&&'Table'[Sub Category]="Budget"),[Value])
return
Actual - Budget

If only Category="A" &&Sub Category="Actual" gives the result, you can use the following dax:

Diff 2 =
var Actual=
SUMX(
    FILTER(ALL('Table'),
    'Table'[Category]=EARLIER('Table'[Category])&&'Table'[Sub Category]="Actual"),[Value])
var Budget=
SUMX(
    FILTER(ALL('Table'),
    'Table'[Category]=EARLIER('Table'[Category])&&'Table'[Sub Category]="Budget"),[Value])
return
IF(
    'Table'[Category]="A"&&'Table'[Sub Category]="Actual",
Actual - Budget,BLANK())

2. Result:

vyangliumsft_0-1693489843672.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Ashish_Mathur
Super User
Super User

Hi,

Your solution will be simpler if you transform this data into a 3 column one - Category, Actual and Budget. using Power Query.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@mandronic , Try a measure like

 

Sumx( Summarize(Table, Table[Category],Table[Sub Category] ) ,
if(Max(Table[Sub Category]) = "Actual",
Calculate(Sum(Table[Value]), filter(allselected(Table), Table[Category] = Max(Table[Categoty]) && Table[Sub Category] = "Budget"))
-Sum(Table[Value]), blank()))

 

 

If needed you can use a filter for category A

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you for your reply.

However, using the code you provided, I now get the below (changing the example numbers for better clarity):

TableC.JPG

However, I would like the result to show (114-153 = -39).

Do you have any ideas how to improve the formula to get this?

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors