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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Anonymous
Not applicable

Compare 2 tables by Matrix

Hi,

I have 2 tables : 

Stock : 

Codeteam
1108Team1
1109Team1
1113Team1
1114Team1
1115Team1
1116Team1
1117Team1
1123Team1
1124Team1
1125Team1
1127Team1
1128Team1
1129Team1
1130Team1
1131Team1
1132Team1
1133Team1
1134Team1
1136Team2
1137Team2
1138Team2
1139Team2
1140Team2
1145Team2
1149Team2
1150Team2
1151Team2
1152Team2
1153Team2
1154Team2
1155Team2
18Team2
180Team2
1853Team2
1877Team2
1879Team2

Used: 

 

CodeTeamSub_team
1108Team1Sub_team1
1109Team1Sub_team1
1113Team1Sub_team1
1114Team1Sub_team2
1115Team1Sub_team2
1116Team1Sub_team2
1117Team1Sub_team2
1123Team1Sub_team2
1124Team1Sub_team2
1125Team1Sub_team2
1127Team1Sub_team2
1128Team1Sub_team2
1129Team1Sub_team2
1130Team1Sub_team3
1131Team1Sub_team3
1132Team1Sub_team3
1133Team1Sub_team3
1134Team1Sub_team3
1136Team2Sub_team4
1137Team2Sub_team4
1138Team2Sub_team4
1139Team2Sub_team4
1140Team2Sub_team4
1145Team2Sub_team4
1149Team2Sub_team5
1150Team2Sub_team5
1151Team2Sub_team5
1152Team2Sub_team5
1153Team2Sub_team5
1154Team2Sub_team5
1155Team2Sub_team5
1156Team2Sub_team5
1157Team2Sub_team5
1158Team2Sub_team5

I would like to create a matrix to compare the code used and stocked  as below : 

The 2 tables can have many to many relationship between the codes.

The same code can be in different teams.

Team   UsedStockOverusedOverstock
 Team1      
  sub_team1 4400
   1108    
   1109    
   1113    
   1114    
    9900
  sub_team21115    
   1116    
   1117    
   1123    
   1124    
   1125    
   1127    
   1128    
   1129    
  sub_team3 5500
   1130    
   1131    
   1132    
   1133    
   1134    
 Team2      
  sub_team4 6600
   1136    
   1137    
   1138    
   1139    
   1140    
   1145    
  sub_team5 10730
   1149    
   1150    
   1151    
   1152    
   1153    
   1154    
   1155    
   1156    
   1157    
   1158    
  sub_team6 6060
   1162    
   1166    
   1167    
   1168    
   1171    
   1172    
  sub_team7 6060
   1173    
   1177    
   1178    
   1179    
   1180    
   1183    
    0505
   18    
   180    
   1853    
   1877    
   1879    

Thank you in advance for your help.

Regards,

Tg 

 

 

2 REPLIES 2
DataNinja777
Super User
Super User

Hi @Anonymous ,

 

A many-to-many relationship should be avoided where possible, especially for clarity in data modeling. Instead, the approach should be structured with a one-to-many relationship between CodeTable and both Stock and Used.

Start by loading both Stock and Used tables into Power BI. To ensure proper relationships, create a bridge table (CodeTable) containing unique codes:

CodeTable = DISTINCT( UNION( SELECTCOLUMNS(Stock, "Code", Stock[Code]), SELECTCOLUMNS(Used, "Code", Used[Code]) ) )

Then, establish one-to-many relationships:

CodeTable[Code] → Stock[Code]
CodeTable[Code] → Used[Code]
This setup ensures that filtering through CodeTable affects both Stock and Used tables correctly.

Now, create DAX measures to calculate the required values for the matrix. The Used Count measure counts how many times each code appears in the Used table:

Used Count = COUNTROWS(Used)

Similarly, the Stock Count measure calculates the occurrences of each code in the Stock table:

Overused = MAX(0, [Used Count] - [Stock Count])

For overstocked items (codes stocked more than used), reverse the calculation:

Overstock = MAX(0, [Stock Count] - [Used Count])

To ensure that the team and sub-team filtering is respected, use CALCULATE to keep the necessary context:

Used Count by Team = CALCULATE( COUNTROWS( Used ), ALLEXCEPT( Used, Used[Team], Used[Sub_team], Used[Code] ) )

Similarly, apply the same logic for the stock count:

Stock Count by Team = CALCULATE( COUNTROWS( Stock ), ALLEXCEPT( Stock, Stock[Team], Stock[Code] ) )

Use these team-based calculations to ensure that Overused and Overstock calculations remain accurate:

Overused by Team = MAX(0, [Used Count by Team] - [Stock Count by Team])
Overstock by Team = MAX(0, [Stock Count by Team] - [Used Count by Team])

Finally, in the matrix visual, set:

Rows: Team → Sub_team → Code
Values: Used Count, Stock Count, Overused, Overstock
This structure correctly compares used and stocked codes, highlights any overuse or overstock situations, and avoids unnecessary many-to-many relationships.

Anonymous
Not applicable

Hello,

 

Thank you for your detailed reply.

Here is the matrix, I can't have a good list of vlan in each team and sub-team. Could you please have any susggestion ? 

 

Thank you.

 

vnqt_1-1740474560711.png

 

 

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors