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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
paulomartins
Frequent Visitor

Measure to sum a column bases on another tables column

Hello guys, it's my first post here and I'm very glad to be part of this forum.

 

Recently I'm trying to deal with a logical sum in DAX and to be honest I not dealing with it really well...

 

My problem is:

 

I have 3 tables:

 

paulomartins_0-1657066142563.png

 

paulomartins_1-1657066191404.png

 

paulomartins_2-1657066220496.png

 

I want to sum the MB52[Quantity] based on Current and Old Keys, reaching this result:

 

paulomartins_3-1657066242560.png

 

Identifying [Current Key] and suming the quantity of the [Old. Key] with owns [Current Key] quantity.

 

I tried this code but with no sucess 😞

 

[Sum. Quantity] =
IF
(

'KEYS'[Old. Key] <> "";

CALCULATE(SUM(MB52[Quantity]) ; MAX(MB52[Key]) = MAX('KEYS'[Old. Key])) + CALCULATE(SUM(MB52[Quantity]) ; MAX(MB52[Key]) = MAX('KEYS'[Current Key]));

SUM(MB52[Quantity])

)

 

I apreciate a lot your help and thanks anyway 🙂

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @paulomartins ,

Here are the steps you can follow:

1. Create measure.

Measure =
var _old=
CALCULATE(SUM('MB52'[Quantity]),
FILTER(ALL(MB52),
'MB52'[Key]=MAX('Keys'[Old.Key])))
var _current=
CALCULATE(SUM('MB52'[Quantity]),
FILTER(ALL(MB52),
'MB52'[Key]=MAX('Keys'[Current Key])))
var _sum=
_old + _current
return
IF(
_sum = BLANK() ,0,_sum)
Measure 2 =
SUMX(
    FILTER(ALL('Keys'),
    'Keys'[Current Key] = MAX('Products'[Currrent Key])),
    [Measure])

2. Result:

vyangliumsft_0-1657245022628.png

If you need pbix, please click here.

 

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

View solution in original post

4 REPLIES 4
v-yangliu-msft
Community Support
Community Support

Hi  @paulomartins ,

Here are the steps you can follow:

1. Create measure.

Measure =
var _old=
CALCULATE(SUM('MB52'[Quantity]),
FILTER(ALL(MB52),
'MB52'[Key]=MAX('Keys'[Old.Key])))
var _current=
CALCULATE(SUM('MB52'[Quantity]),
FILTER(ALL(MB52),
'MB52'[Key]=MAX('Keys'[Current Key])))
var _sum=
_old + _current
return
IF(
_sum = BLANK() ,0,_sum)
Measure 2 =
SUMX(
    FILTER(ALL('Keys'),
    'Keys'[Current Key] = MAX('Products'[Currrent Key])),
    [Measure])

2. Result:

vyangliumsft_0-1657245022628.png

If you need pbix, please click here.

 

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

It is what I was trying to do!

Thank you so much @v-yangliu-msft, you helped me to reach the result and learn a lot about DAX with this problem!

paulomartins
Frequent Visitor

Sorry, I forgot to post the tables relationship...

 

paulomartins_0-1657128689004.png

 

Because I'm using power pivot I had to do some changes, using INTERSECT instead TREATAS and use DISTINCT on MB52[Key].

 

The final result is just a sum of Current Keys quantity and putting the Old Keys quantity in a null key 😞

 

Sum of quantity = CALCULATE(

SUM ( MB52[Quantity] );
INTERSECT(

UNION( VALUES ( KEYS[Current Key] ); VALUES( KEYS[Old. Key] ) );
DISTINCT(MB52[Key])

)

)

Result:  

 

paulomartins_1-1657129032790.png

johnt75
Super User
Super User

Assuming that you have a one-to-many relationship from Products to Keys and no relationships between MB52 and the other tables, then you could try

Sum of quantity =
CALCULATE (
    SUM ( MB52[Quantity] ),
    TREATAS (
        UNION ( VALUES ( Keys[Current Key] ), VALUES ( Keys[Old Key] ) ),
        MB52[Key]
    )
)

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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

Top Solution Authors