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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.