Today I tried to create a form in PowerApps where I needed to filter the options available in the drop down depending on an option selected in a different field.
The initial setup
I have the following plumbing already done.
- I created a datasource Contact List_1 which is a list of contacts in SharePoint
- I created a datasource Opportunities which uses SharePoint list called Opportunities
- The both Opportunities and Contact List have a lookup column called Client
- The Contact list has a single line of text column, Fullname
- My Opportunity list has multi select lookup column, OpportunityContacts
I now want my PowerApp to only show the contacts relevant to the client selected.
Initially PowerApps would use Choices(Opportunities.OpportunityContacts) to get all my contacts in the Opportunities datasource and my contacts appear in my drop down. This is good but if you want to only show contacts related to a specific client then this isn’t useful. On purpose I called my Client column in my contacts list Company. Just so that it becomes easier to analyse the functions following in this post.
PowerApps filter function
To do the filtering we will need to use the Filter function.
So we would be looking at something like this:
So for each of my contacts I’m going to filter by looking in the Contact List 1 datasource if the Company name matches the client name in my opportunity.
A “Value in <List of contacts>” would be the rough construction that I’m going to use.
Filter(Choices(Opportunities.OpportunityContacts), Value in <List of contacts>)
To filter the list of contacts by the Company name I’m having to look at the data coming from the ‘Contact List 1’ datasource and filter by Company name and rerturn the full name of the contact.
The folloiwng Filter funciton will do this: Filter(‘Contact List_1’, Company = DataCardValueClient.Text).FullName
Putting all the differnt elemtns together I’m looking at this:
Filter(Choices(Opportunities.OpportunityContacts),Value in Filter(‘Contact List_1’, Company = DataCardValueClient.Text).FullName )
The complete cascaded dropdown
With the above filters in place I’m now getting a drop down that lists only my relevant contacts for a client.