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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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

 

 
v-yangliu-msft
Community Support
Community Support

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

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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.