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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Velafyko
Frequent Visitor

USERELATIONSHIP and many to many

Hi all
In my data model I have an "Order" table and an "Invoice" table. They are linked by the column "Order number". Since there are allready a lot of relationships in the model, I just have an inactive relationship between "Order" and "Invoice". This is a many-to-many relationship.
 
Order
Order number
Amount
 
Invoice
Invoice number
Amount
Order number
 
Now I want to compare an invoice with how much is ordered. I create a visulization table with invoice number and amount to get a list of all invoices. Then I try with a DAX statement to get the related order sums:
RelatedOrderSum= CALCULATE(SUM(Order[Amount]), USERELATIONSHIP(Invoice[Order number], Order[Order number]))
 
This works when there is only one order with the corresponding order number, but it fails if there are two orders. Then it only returns BLANK, and not the sum of them. How can I fix this to make it work with any number of orders?
 
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Velafyko , create a common dimension of Order number, refer the code in the file

 

https://www.dropbox.com/s/op9lb78w9utdonz/Distinct%20from%20two%20Tables.pbix?dl=0

 

refer

https://www.seerinteractive.com/blog/join-many-many-power-bi/

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

6 REPLIES 6
mahoneypat
Microsoft Employee
Microsoft Employee

I agree you should improve your model and get rid of the M2M, but FYI there is another way to path the Order Number filter between the tables.

 

RelatedOrderSum= CALCULATE(SUM(Order[Amount]), TREATAS(VALUES(Invoice[Order number]), Order[Order number]))

 

You can swap the two order number columns in the TREATAS; not sure which invoice # column you are using in the visual.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Unfortunately I don't get your formula to work. It just gives me the same result as my original formula with the USERELATIONSHIP function. It works when there is only one order, but not when there are multiple orders for an invoice.

Anonymous
Not applicable

Hm... Interesting. You're saying that one order can be found on different invoices and that one invoice can have different order numbers? As much as I understand the latter, I'm a bit surprised at the former.

Many2Many is useful only for scenarios where there's a granularity problem. Nothing else. You don't have a granularity issue here, so I'd change the design to remove Many2Many, since it's not only slower but can also be very hard to work with if you don't know all the nuances of such a design.

Yes, one order can lead to multiple invoices. For instance when not everything ordered is in stock, then what is in stock is shipped together with an invocice. When the remaining items are back in stock, they are shipped with a new invoice from the same order. Ideally we should have exactly one invoice for each order, but it's not always the case.

 

I hoped I could avoid changing the data model, but it seems like I have no choice then. Thank you 🙂

amitchandak
Super User
Super User

@Velafyko , create a common dimension of Order number, refer the code in the file

 

https://www.dropbox.com/s/op9lb78w9utdonz/Distinct%20from%20two%20Tables.pbix?dl=0

 

refer

https://www.seerinteractive.com/blog/join-many-many-power-bi/

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Super User
Super User

@Velafyko - I would avoid many-to-many relationships like the plague and use a bridge table of unique order numbers.

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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