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

Filter with Multiple Rows from Collection, ForAll?

Hello,

 

I have a SQL database with work order line items and are connected by a Work Order Header ID, I use Collections to get the specific data I need from these large databases to use in the app.

 

I want a Collection with all line items for a Work Order and am able to do this for a single Work Order when I Set a variable and use it in my Filter:

 

 

//get Work Order Header ID of Last Entered Work Order Number, Set Variable

 

Set(LastEnteredWOHeaderID, LookUp('[dbo].[Source1]', WorkOrderNo = LastEntered.WorkOrderNo));

 

//Collect Work Order line items for all items that match Work Order Header ID

 

Collect(WODetailsData, Filter('[dbo].[Source2]', WOHeaderID = LastEnteredWOHeaderID))

 

 

The above results in showing all line items for that Work Order Header ID.

 

My problem is I cannot make this same Filter work when I try to pass multiple Work Order Header IDs at the same time using a Collection or a Set variable composed of a table of these values.

 

If I change = to "in" and reference the collection, I get thousands of results as if my filter doesn't know what to look for even though I try pointing it to the column in the collection containing the Work Order Header IDs.

 

The best luck I have had so far is using ForAll to iterate through the collection of Work Order Header IDs, but all it does is use the first record value for all and I end up with a collection of repeated line items of a single Work Order for as many rows I have in the collection:

 

ForAll(WOHeaderData, Collect(WODetailsData, Filter('[dbo].[Source2]', WOHeaderID in WOHeaderData.WOHeaderID)))

 

I am looking for a formula that would basically do what my first Filter does for each row of my collection of Work Order Header IDs all at once instead of 1 at a time.

 

Any help would be appreciated, this is my first post - Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Frequent Visitor

Re: Filter with Multiple Rows from Collection, ForAll?

@v-xida-msft Kris,

 

Your second formula worked other than 1 change, you rock!  Thank you.

 

I think the issue with the original was the formula wanting text values and by recreating the WOHeaderID column with RenameColumns it decided to work!  Great idea.

 

The only thing I had to change was "in" to "=":

Clear(WODetailsData);
ForAll(
       RenameColumns(WOHeaderData, "WOHeaderID", "WOHeaderID1"),
       Collect(WODetailsData, Filter('[dbo].[WorkOrderDetail]', WOHeaderID = WOHeaderID1))    
)

Thank you for all your help,

Riley

View solution in original post

4 REPLIES 4
Highlighted
Community Support
Community Support

Re: Filter with Multiple Rows from Collection, ForAll?

Hi @rileyrichards ,

Based on the formula you provided, I think there is something wrong with it. Please consider modify your formula as below:

Clear(WODetailsData);
ForAll(
WOHeaderData,
Collect(WODetailsData, Filter('[dbo].[Source2]', WOHeaderID = WOHeaderData[@WOHeaderID]))
)

Please take a try with above solution, check if the issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Frequent Visitor

Re: Filter with Multiple Rows from Collection, ForAll?

@v-xida-msft Kris,

 

Thank you so much for your response.

 

I tried your formula (Copy and Paste) and I ended up with only 1 Work Order's line items, seems like the "For All" did not iterate through all items and instead only looked at the first row.

 

When I change the formula back it gives me the repeated line items, which confirms I have the expected number of rows in WOHeaderData.

 

For example, the first Work Order has 19 line items.  When I run your formula I get only 19 rows in the WODetailsData collection.

 

When I run my formula where I end up with repeated line items, I get 114 rows because there are 6 rows in WOHeaderData: (6 rows * 19 line items) = 114 rows

 

Thank you again for your quick response, I really appreciate it.

Highlighted
Community Support
Community Support

Re: Filter with Multiple Rows from Collection, ForAll?

Hi @rileyrichards ,

Could you please share a bit more about the data structure of your WOHeaderData data source and '[dbo].[Source2]' data source?

 

Based on the neeeds that you mentioned, I think the solution I priovided above could achieve your needs. Please consider modify your formula as below:

Clear(WODetailsData);
ForAll(
WOHeaderData,
Collect(WODetailsData, Filter('[dbo].[Source2]', WOHeaderID in WOHeaderData[@WOHeaderID])) /* <-- Find matched records from Source2 table based on each one WOHeaderID from WOHeaderData data source, and save it into the collection */
)

Or

Clear(WODetailsData);
ForAll(
RenameColumns(WOHeaderData, "WOHeaderID", "WOHeaderID1"),
Collect(WODetailsData, Filter('[dbo].[Source2]', WOHeaderID in WOHeaderID1)) /* <-- Find matched records from Source2 table based on each one WOHeaderID from WOHeaderData data source, and save it into the collection */
)

Please take a try with above solution, check if the issue is solved.

 

More details about ForAll function in PowerApps, please check the following article:

ForAll function

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Frequent Visitor

Re: Filter with Multiple Rows from Collection, ForAll?

@v-xida-msft Kris,

 

Your second formula worked other than 1 change, you rock!  Thank you.

 

I think the issue with the original was the formula wanting text values and by recreating the WOHeaderID column with RenameColumns it decided to work!  Great idea.

 

The only thing I had to change was "in" to "=":

Clear(WODetailsData);
ForAll(
       RenameColumns(WOHeaderData, "WOHeaderID", "WOHeaderID1"),
       Collect(WODetailsData, Filter('[dbo].[WorkOrderDetail]', WOHeaderID = WOHeaderID1))    
)

Thank you for all your help,

Riley

View solution in original post

Helpful resources

Announcements
Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (5,378)