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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
HB13
Helper I
Helper I

Subtract fields from two tables with a many to many relationship

Hi PBI Gang

I have two tables Table1 and Table2 that have a many to many relationship (they are joined on their ID, ID_ columns)... each of these tables have a field containing a Year value.
I need to calculate the difference between these two fields.

For example;
Table1
ID          Year

1            2019

1            2019

1            2020

1            2020

 

Table
ID_    Item      Year_

1         A         2017

1         B         2018


How do I create a measure that calculates the difference between the fields? It would need to be as
YearDiff = Table1[Year] - Table2[Year_] with the output being;
ID    Item   YearDiff
1        A            2
1        B            1
1        A            3

1        B            2
where each Year in Table2 is subtracted from each Year in Table1 where they match on their ID. I'm essentially trying to calculate the age of each item for every year its ID has been recorded. I hope that makes sense.

These fields are not picking up in a DAX measure. I cannot do a lookup from one Table2 into Table1 or vice versa because of the many to many... I am not sure how to to go about this

All assistance will be greatly appreciated. 

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

Hi @HB13 ,

 

Please try this measure:

Measure = SUMX(VALUES(Table1[YEAR]),[YEAR])-SUM('Table'[Year])

 

Result:

vchenwuzmsft_2-1659603604940.png

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

2 REPLIES 2
v-chenwuz-msft
Community Support
Community Support

Hi @HB13 ,

 

Please try this measure:

Measure = SUMX(VALUES(Table1[YEAR]),[YEAR])-SUM('Table'[Year])

 

Result:

vchenwuzmsft_2-1659603604940.png

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

 

 

claymcooper
Resolver II
Resolver II

Can you merge the tables on the ID column? Then set up a custom column to calculate the difference between the years in the query editor?

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors