Power Apps Many to Many relationships in Canvas Apps

Many to Many relationships can be painful in Canvas Apps . In this post I’m looking at how we can work with many to many relationships in Power Apps. I will cover the creation of the relationships, updating the relationships and querying the relationships.

Power Apps Many to Many relationships

I started by creating two tables in my solution. The first table is called Author and has a field called Full Name and the second table is called Books and has a column called Title.

Two tables created in Dataverse
Two tables created in Dataverse

Now from my books table I’m creating a relationship called Written by

Create a many to many relationship in Dataverse
Create a many to many relationship in Dataverse

Now I’m renaming the Relationship name to Written by

Rename the relationship in Dataverse
Rename the relationship in Dataverse

Once this relationship has been created I can see the relationship between my authors and books as expected.

My relationship has appeared
My relationship has appeared

Adding records to the tables

To add tables to the tables I’m creating a simple app. So that I can add some Authors and books to my tables,.

Create records in a table
Create records in a table

Relating records with Many to Many relationships

Now that I’ve created a few book and author records I want to relate the records. By selecting the records in both tables, pressing the written by button will need to set the relationship.

Relate records in Canvas App
Relate records in Canvas App

Implementing the Written by button is slightly harder than just relating the two records. WE will need to use the relate function twice. So that the relationship will be created in both directions.

Relate(
    LookUp(
        Books,
        Title = galBooks.Selected.Title
    ).Authors,
    LookUp(
        Authors,
        'Full Name' = galAuthors.Selected.'Full Name'
    )
);
Relate(
    LookUp(
        Authors,
        'Full Name' = galAuthors.Selected.'Full Name'
    ).Books,
    LookUp(
        Books,
        Title = galBooks.Selected.Title
    )
)
Two relate functions to relate the records
Two relate functions to relate the records

To display the related records, we can now add a gallery with a child gallery in it. My outer gallery is called galAuthorAndBooks while the inner gallery is called Books By Author.

Display the relationships in a gallery
Display the relationships in a gallery

For the galAuthorsAndBooks, I’m setting the Items property to Authors, so that the Authors are all displayed.

For the inner gallery I can now reference my Books using the following code:

ThisItem.Books

ThisItem refers to the Itemof the parent gallery.

The Nested Gallery Approach
The Nested Gallery Approach

Querying data across Many to Many relationships

So far we managed to get related records displayed. Noticed that there wasn’t an option to select the actual relationship. Therefore if there were multiple many to many relationships between those two table then things would still get messed up.

The next step in the post is that I want to display the books written by the author that is selected. Easy??

I can see two approaches here. First we could use the gallery that we used earlier.

All we have to do is set the Items of the parent gallery to not load data from the Authors table. Adding the following Filter will now only display the records needed.

Filter(Authors, 'Full Name' = galAuthors.Selected.'Full Name')

The other approach could be something like thisL:

Filter(Books As _Book, 
            CountRows(Filter(_Book.Authors As _Author, _Author.'Full Name' = varSelectedAuthor)) > 0
)

However the _Book.Authors property is not available. In various other constructions like creating collections for both tables, which just over complicates the app. The earlier mentioned nested gallery approach is then simpler.

A 3rd approach is to avoid Many to many relationships all together and create a table which links the tables. However that avoids the out of the box relationship data model a bit.

For more posts on Power Apps, please have a look at my Power Apps User Guide.

FAQs

How do I display records from a many to many related table?

Create nested galleries where the outer gallery displays one table and the inner gallery displays the other table.

The Nested Gallery Approach

Use the Relate and Unrelate functions in Power Apps to relate records. For Many to many you may have to call the relate function twice.

Should I use Many to Many relationships in Canvas Apps?

For some things you can use Power Apps Many to Many relationships, however quite often an additional table holding the GUIDs for each of the records is easier. Especially when you need to filter the records.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

2 thoughts on “Power Apps Many to Many relationships in Canvas Apps

  1. After much pain*) we opted for the 3rd approach (to avoid Many to many relationships all together and create a table which links the tables) and whished we’d never had this relation type. In what scenario’s is many-to-many the better option over a link table in your experience?

    *) integration scenario’s and when later requiring additional attributes on the relation

    1. I always try to use what is there out of the box. But then I noticed in the latest table building UI, the Many to Many option has been removed. I wonder if Microsoft is moving away from Many to Many relationships. I just wish they fixed this properly.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.