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
alya1
Helper V
Helper V

Sum 2 rows if ID and subtract from ID columns match?

Hello, 
I have table as below:

ID  Amount  Subtract from ID  
1-1003
2200 
3300 
4-502
5-103

 

Can I get to the goals table below in power bi? 
Goals:

ID  Amount  Subtract from ID  Final
1-10030
2200 150
3300 50
4-5020
5-15030
1 ACCEPTED SOLUTION
v-jianpeng-msft
Community Support
Community Support

Hi, @alya1 

Based on your description, I created a measure using the following DAX:

Result =
VAR _ID =
    SELECTEDVALUE ( 'Table'[ID  ] )
VAR _table =
    SUMMARIZE (
        ALL ( 'Table' ),
        'Table'[ID  ],
        'Table'[Subtract from ID  ],
        'Table'[Amount  ]
    )
VAR _amount =
    SELECTEDVALUE ( 'Table'[Amount  ] )
VAR _blank =
    FILTER ( _table, 'Table'[Subtract from ID  ] = _ID )
RETURN
    IF (
        _ID IN SELECTCOLUMNS ( _blank, 'Table'[Subtract from ID  ] ),
        _amount + SUMX ( _blank, 'Table'[Amount  ] ),
        0
    )

 Put the corresponding column and this measure in the table visual, and the result is as follows:

vjianpengmsft_0-1718245154720.png

I've provided the PBIX file used this time below.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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-jianpeng-msft
Community Support
Community Support

Hi, @alya1 

Based on your description, I created a measure using the following DAX:

Result =
VAR _ID =
    SELECTEDVALUE ( 'Table'[ID  ] )
VAR _table =
    SUMMARIZE (
        ALL ( 'Table' ),
        'Table'[ID  ],
        'Table'[Subtract from ID  ],
        'Table'[Amount  ]
    )
VAR _amount =
    SELECTEDVALUE ( 'Table'[Amount  ] )
VAR _blank =
    FILTER ( _table, 'Table'[Subtract from ID  ] = _ID )
RETURN
    IF (
        _ID IN SELECTCOLUMNS ( _blank, 'Table'[Subtract from ID  ] ),
        _amount + SUMX ( _blank, 'Table'[Amount  ] ),
        0
    )

 Put the corresponding column and this measure in the table visual, and the result is as follows:

vjianpengmsft_0-1718245154720.png

I've provided the PBIX file used this time below.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

Hi Jianpeng, thank you so much for the clean and effective DAX! 
May I ask you to please explain the steps please? specifically VAR _table and the RETURN line please.

Hi, @alya1 

Thank you very much for your reply. First_ID returns the ID of the current table visual row, _amount something similar:

vjianpengmsft_0-1718333729341.png

_table generates a table of variables that looks like this:

vjianpengmsft_1-1718333834061.png

_blank returns a table of variables that contains only the current _ID. Finally, use the SELECTCOLUMNS method to select the ID column of the _blank table, and then determine whether the _ID of the current row is in this column. If the current ID is in this table, then the amount corresponding to the current ID + the amount that matches it. That's what you're looking for.

 

 

Best Regards

Jianpeng Li

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

 

 

Thank you Jianpeng!! 

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