web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Filter Gallery results...
Power Apps
Unanswered

Filter Gallery results based on a tree relationship

(0) ShareShare
ReportReport
Posted on by 51

Hello all - I have been unable to find a solution (if one exists) to the following challenge. Any assistance is appreciated!

 

I have a single table "Samples" that stores various laboratory sample preparations. The columns for this simplified version of table are the following:

* sample_id

* made_by

* parent_sample_id (optional input, a lookup that links to another sample_id)

 

So you can image a world, where "Person A" prepares "sample a1", of which a portion is removed, split, and further prepared by "Person B" to generate "sample b1" and "sample b2". Next, a portion of "sample b2" is then removed, split, and further prepared by "Person C" to generate samples "sample c1", "sample c2", and "sample c3". Essentially, a tree data structure. This is very common in labs where samples are prepared by multiple individuals at different time points along the evolution to a final sample to be used in an experiment.

 

Generating the database was not an issue. However, I (my lab) would very much like the ability to filter a gallery result of a combobox selection where-by all sample records are listed that originate from a selected "root" sample. (all children, grandchildren, and so on....) So if "sample b2" was selected, then [ "sample c1", "sample c2", "sample c3" ] are shown in the gallery. Likewise, if "sample a1" is selected, then ["sample b1", "sample b2", "sample c1", "sample c2", "sample c3" ] are shown in the gallery.

 

I cannot figure out a method of doing this. The best I can find is just obtaining the immediate children of the selected sample (but not the grandkids):

 

Gallery items: Filter( Samples, parent_sample_id.sample_id = ComboBox.Selected.sample_id)

 

where by the items in the ComboBox are: Choices(Samples.parent_sample_id)

 

I was looking for a way of creating a while loop in which I could add in various samples as I recursively make my through the tree, but alas, I failed. 

Categories:
I have the same question (0)
  • Shujaath_Khan Profile Picture
    1,111 Super User 2024 Season 1 on at

    Hi @mike_a 

    Did you try ForAll on Gallery items?

     

    Button1.OnSelect=
    Clear(collFilteredList);
    ForAll(
     Gallery4.AllItems,
     If(
     ThisRecord.parent_sample_id = ThisRecord.sample_id
     ),
     Collect( collFilteredList, { parent_sample_id : ThisRecord.parent_sample_id, sample_id: ThisRecord.sample_id }, )
     ,
     false
     )
    )

     

    Thanks

    Please click Accept as Solution on this reply if it helped to solve your issue, and thumbs-up if you feel this is useful or important in other ways as a boost.

  • mike_a Profile Picture
    51 on at

    Many thanks shujaathkhan!

    I could not get the code you wrote to work as is. In particular, it still lacks the recursive ability of a while loop to explore the tree. However, your suggestion of a ForAll and Collect solution was a good start. So in the end, i've selected the following (really ugly) code that runs a ForAll 3 times, enough to capture most common scenarios:

     

    So, on a button press, I generate the following "FilteredList"

     

    clearCollect(FilteredList,ComboBox.Selected);
    ForAll(Samples As Sample_record,If(Not(IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Parent_Sample.Sample_ID))) && IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Sample_ID)),Collect(FilteredList, Sample_record)));
    ForAll(Samples As Sample_record,If(Not(IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Parent_Sample.Sample_ID))) && IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Sample_ID)),Collect(FilteredList, Sample_record)));
    ForAll(Samples As Sample_record,If(Not(IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Parent_Sample.Sample_ID))) && IsBlank(LookUp(FilteredList,Sample_ID = Sample_record.Sample_ID)),Collect(FilteredList, Sample_record)));

     

    And the "FilteredList" is used as the Gallery Items. It works pretty well. Should I need to go deeper into the tree in the future, I can just C&P more ForAll lines I suppose. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 358 Most Valuable Professional

#2
11manish Profile Picture

11manish 207 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 150 Super User 2026 Season 2

Last 30 days Overall leaderboard