cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
KevinAlc0r
Frequent Visitor

Alternatives to "in" operator and CountRows to filter for all rows of one table based on the value from another table in a many-to-many relationship schema

Hi,

 

I am using SQL tables as the data source for my Power Apps. I am having trouble in finding a delegable function to filter for rows from one table based on the value from another table which has a many-to-many relationship with the current table I want to filter.

Suppose I have two tables: STUDENT and COURSE that have many-to-many relationship between them (one student can enroll to multiple courses and one course can have multiple students). I also have a table to handle the many-to-many relationship between the two tables called STUDENT_COURSE:

 

KevinAlc0r_2-1714964656639.png

 

Let's say I want to filter for all students who attend a Course with a CourseId = 37, what I would normally do is:

 

// Find all studentId of all students who enroll to the course by filtering the STUDENT_COURSE table
Set(currentStudentIdsInCourse, 
    Filter(STUDENT_COURSE, CourseId = 37).StudentId
);
// Step 2: Filter for all Students from the STUDENT table using the studentIds obtained from the previous step and get their names

Set(studentNamesInCourse,
    Filter(STUDENT, studentId in currentStudentIdsInCourse).StudentName
);

 

 

Or alternatively, I can also use CountRows:

 

// OR I can also use CountRows as the filtering condition to find students who enroll to the course

Set(studentNamesInCourse,
    Filter(STUDENT, CountRows(
         Filter(STUDENT_COURSE, courseId = 37 && STUDENT[@studentId] = STUDENT_COURSE[@studentId])
        ) > 0
    ).StudentName
);

// Alternatively, I can also use the "in" operator in a single step like shown below instead of what I did previously:
Set(studentNamesInCourse,
    Filter(STUDENT, STUDENT[@studentId] in
        Filter(STUDENT_COURSE, courseId = 37 && STUDENT[@studentId] = STUDENT_COURSE[@studentId]).studentId
    ).StudentName
);

 

 

Using either "in" or CountRows works fine. However, both of them throw out a Delegation warning and I found out that both "in" operator in this case and the CountRows function are non-delegable. Reading into this documentation from Microsoft, the "in" operator is supposed to be delegable as long as the operator is used for columns in the base data source which I believe I have done correctly (both "in" operator usage in my example uses the studentId column from the STUDENT table, studentId is a column inside the STUDENT table which acts as the base data source).

 

I am worried that in the future my database would grow to accommodate even more data which is why I am concerned with these Delegation problems.

 

Are there any other alternative functions or methods for this filtering process?

Thanks a lot!

3 ACCEPTED SOLUTIONS

Accepted Solutions
DBO_DV
Resident Rockstar
Resident Rockstar

Hey @KevinAlc0r,

 

// Find all studentId of all students who enroll to the course by filtering the STUDENT_COURSE table
Set(currentStudentIdsInCourse, 
    Filter(STUDENT_COURSE, CourseId = 37).StudentId
);

This part is fine I think we can agree on this. 

Could you please share with us, how many students are in one course. And also the use case: do you need this app to be fast or can there be a loading time? Is There a possibility to load the student names in the Background, so that the user would do something else in the meantime?

 

Because if you have time for the function to run you could use ForAll:

ForAll(currentStudentIdsInCourse,
    Collect(studentNamesInCourse, 
        LookUp(Student, studentid=ThisRecord.Value).StudentName)
    )
)

 

It's not the fastest way but delegable.

 

 

Feel free to mark my response as the solution by clicking"Accept as solution" if it resolved your issue, so others can find the answer. If you found the content helpful in other ways, you can show your appreciation by giving it a"Thumbs Up".

View solution in original post

VishalJhaveri
Impactful Individual
Impactful Individual

No, there are no alternative functions or methods for filtering process.

However you can try the method to retrieve more than 2000 records at a time:
Overcome 2000 items limit using Power Apps Collect function • Tomasz Poszytek, Business Applications... Re: 500 item limit in CDM entity search filter(nee... - Page 4 - Power Platform Community (microsoft... -> Scroll down in this link and find Mr. Dang(user) Solution How to overcome 500 items limit in PowerApps - CEO-Refleksje (michalguzowski.pl)


If my answer helps you please mark it as solution.

View solution in original post

iAm_ManCat
Most Valuable Professional
Most Valuable Professional

Hiya,

 

So since you already have the student IDs from the intermediate data source, you should be able to loop over that and do individual lookups that you add to a collections - that way there's no delegation issue as the lookups wouldn't be using 'in'. It won't be as fast but it will be scalable.

 

Set the course and list of StudentId for that course:

Set(currentStudentIdsInCourse,
    Filter(
        STUDENT_COURSE, CourseId = 37).StudentID
);

 

Then create a collection (rather than setting a variable) by iterating through all of those records and doing a lookup for each student detail based on that. This should get you a table (collection) of students with their details that you can then use as you need:

Clear(StudentNamesInCourse);
ForAll(currentStudentIdsInCourse As CSIIC,
    Collect(LookUp(STUDENT, StudentId = CSIIC.StudentId).StudentName)
)

 

You might need to tweak your code slightly (might need to use .Value instead of .StudentID) to reference the column names directly.

 

Not a perfect solution, but as it's doing individual lookups with equal operator it will not face delegation issues 👍


@iAm_ManCat
My blog


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

4 REPLIES 4
DBO_DV
Resident Rockstar
Resident Rockstar

Hey @KevinAlc0r,

 

// Find all studentId of all students who enroll to the course by filtering the STUDENT_COURSE table
Set(currentStudentIdsInCourse, 
    Filter(STUDENT_COURSE, CourseId = 37).StudentId
);

This part is fine I think we can agree on this. 

Could you please share with us, how many students are in one course. And also the use case: do you need this app to be fast or can there be a loading time? Is There a possibility to load the student names in the Background, so that the user would do something else in the meantime?

 

Because if you have time for the function to run you could use ForAll:

ForAll(currentStudentIdsInCourse,
    Collect(studentNamesInCourse, 
        LookUp(Student, studentid=ThisRecord.Value).StudentName)
    )
)

 

It's not the fastest way but delegable.

 

 

Feel free to mark my response as the solution by clicking"Accept as solution" if it resolved your issue, so others can find the answer. If you found the content helpful in other ways, you can show your appreciation by giving it a"Thumbs Up".
VishalJhaveri
Impactful Individual
Impactful Individual

No, there are no alternative functions or methods for filtering process.

However you can try the method to retrieve more than 2000 records at a time:
Overcome 2000 items limit using Power Apps Collect function • Tomasz Poszytek, Business Applications... Re: 500 item limit in CDM entity search filter(nee... - Page 4 - Power Platform Community (microsoft... -> Scroll down in this link and find Mr. Dang(user) Solution How to overcome 500 items limit in PowerApps - CEO-Refleksje (michalguzowski.pl)


If my answer helps you please mark it as solution.

iAm_ManCat
Most Valuable Professional
Most Valuable Professional

Hiya,

 

So since you already have the student IDs from the intermediate data source, you should be able to loop over that and do individual lookups that you add to a collections - that way there's no delegation issue as the lookups wouldn't be using 'in'. It won't be as fast but it will be scalable.

 

Set the course and list of StudentId for that course:

Set(currentStudentIdsInCourse,
    Filter(
        STUDENT_COURSE, CourseId = 37).StudentID
);

 

Then create a collection (rather than setting a variable) by iterating through all of those records and doing a lookup for each student detail based on that. This should get you a table (collection) of students with their details that you can then use as you need:

Clear(StudentNamesInCourse);
ForAll(currentStudentIdsInCourse As CSIIC,
    Collect(LookUp(STUDENT, StudentId = CSIIC.StudentId).StudentName)
)

 

You might need to tweak your code slightly (might need to use .Value instead of .StudentID) to reference the column names directly.

 

Not a perfect solution, but as it's doing individual lookups with equal operator it will not face delegation issues 👍


@iAm_ManCat
My blog


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


My original intent is to do this process inside a detail view. The user/student clicks on their profile/account information and they can see the courses that they have enrolled in. So I would say the app needs to be fast, some loading time is fine but I don't think the user will do anything in the background while waiting for the loading time.

 

Anyway, thanks a lot! The ForAll example is still another alternative and it is at least delegable.

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!   This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08  23 @WarrenBelz  31 @DBO_DV  10 @Amik  19 AmínAA 6 @mmbr1606  12 @rzuber  4 @happyume  7 @Giraldoj  3@ANB 6 (tie)   @SpongYe  6 (tie)     Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08  10@WarrenBelz 25 @DBO_DV  6@mmbr1606 14 @AmínAA 4 @Amik  12 @royg  3 @ANB  10 @AllanDeCastro  2 @SunilPashikanti  5 @Michaelfp  2 @FLMike  5 @eduardo_izzo  2   Meekou 2   @rzuber  2   @Velegandla  2     @PowerPlatform-P  2   @Micaiah  2     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27     Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8   SunilPashikanti8

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (787)