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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
Giada90
Helper IV
Helper IV

problem with a calculation

Hi, 

I have a table with two fields like this:

typevalue
A10
B3

 

I need to make a measure that subtracts value A from value B

Which dax formula can I use?

Thanks

2 ACCEPTED SOLUTIONS

@Giada90 try this

 

Measure 2 = var _A = CALCULATE(SUM('Yourtable'[value]),'Yourtable'[type] = "A")

var _B = CALCULATE(SUM('Yourtable'[value]),'Yourtable'[type] = "B")

return

(_A - _B)

View solution in original post

11 REPLIES 11
Pragati11
Super User
Super User

Hi @Giada90 ,

 

You can create a DAX expression to get this done.

But before that you will need to add an Index column to your data.

I am not sure on how big is your data?

You haven't mentioned if you need to do this subtraction at you type column level or something else? This information is needed to understand whether you have multiple rows for same type.

Please provide atleast 5-10 rows in your sample data, to cover all the scenarios.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

@Pragati11 , Hi, the real model is about 1700 rows and I need to do this substraction at my type column level, thanks

Hi @Giada90 ,

 

So can you please provide more number of rows in your sample data please rather than just 2 rows?

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

 

@Pragati11, I need to have the sum of all the values that match with A and substract all the values that match with B, I attached more simple data, thanks 

typevalue
A10
B3
A5
B4
A7
B3
A9
B2

HI @Giada90 ,

 

The solution that you have accepted, will only work when you have only 2 categories in your data - A and B.

If you are interested in a dynamic solution for your problem, say where you have more number of categories than just A and B, then try the follwing solution I have created.

The sample data I am considering is:

Pragati11_0-1659430778904.png

First thing I did was created a Rank column at the type column level  in the data using the following DAX expression:

Rank = 
RANKX(
    TypeTable, 
    TypeTable[total value], , 
    DESC, Dense
    )

When you move this to table visual you will see basically a Rank assigned for every type value in the data.

Pragati11_1-1659430884652.png

Now the next thing is we need to get the sum of value column at a Type level.

This we can get using following DAX:

total value at Type level = 
CALCULATE(
    SUM(TypeTable[value]),
    ALLEXCEPT(TypeTable,TypeTable[type])
)

Pragati11_3-1659431385594.png

 

Now we need a difference between these values which should be 31 - 12 = 19 in this case as we just have 2 categories in TYPE column. The following DAX will give the required answer:

Difference at Type level = 
var minRank = CALCULATE(MIN(TypeTable[Rank]), ALL(TypeTable))
var totalValue = CALCULATE(
    SUM(TypeTable[value]),
    ALL(TypeTable),
    TypeTable[Rank] = minRank
)
var nextTotal = 
CALCULATE(
    SUM(TypeTable[value]),
    //ALLEXCEPT(TypeTable, TypeTable[type]),
    ALL(TypeTable),
    TypeTable[Rank] = minRank + 1
)
var diffVal = totalValue - nextTotal
RETURN
diffVal

The result is as follows:

Pragati11_4-1659431443075.png

As part of best practice, I always try to use the dynamic solutions as data can change over time.

Hope this solution helps.

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

thanks!!

HI @Giada90 ,

 

If you are happy with this solution, please accept this solution to the thread, so that others can benefit from it on the forum.

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

DAX=
Var TypeA = calculate(sum(value),filter(tablename, type = A)

Var TypeB = calculate(sum(value),filter(tablename, type = B)
RETURN TypeA-TypeB

@Giada90 try this

 

Measure 2 = var _A = CALCULATE(SUM('Yourtable'[value]),'Yourtable'[type] = "A")

var _B = CALCULATE(SUM('Yourtable'[value]),'Yourtable'[type] = "B")

return

(_A - _B)

thanks!

@Giada90 Welcome,

 

Please accept as solution if its worked.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.