Dataverse - How to filter Dataverse choice columns

With Dataverse, we can create tables with choice columns. A choice column defines a list of acceptable values, and data entry forms typically display these through drop down or combo box controls.

Whilst this offers a simple way to restrict values during data entry, the syntax to filter and to retrieve records that match a choice value isn't entirely straightforward. This post describes this topic in more detail, including how to filter a table by the choice value text.

How to filter by a Dataverse choice column

To give a simple example, let's take the example of a "property document" table. Each row in the table stores a file attachment and associated meta data. This table includes a choice column called DocumentType, as shown in the screenshot beneath.


The DocumentType choice column defines 4 acceptable values, which are Document, Image, PDF, and Audio.


From Power Apps, let's imagine that we want to show a list of records that match the document type PDF. To add this functionality, we add a gallery control to a screen and set the Items property to the following formula:
Filter(PropertyDocuments, DocumentType=[@DocumentType].PDF)

The highlight of this formula is that Power Apps exposes the available choice values through an enumeration with a keyword that consists of the column name prefixed with the @ symbol, and surrounded by square brackets.

As the screenshot beneath shows, we can type [@DocumentType] into the formula bar, and the code completion feature of the designer shows the available choice items.


Filtering a table through the drop-down value of choice items

A common requirement is to build a search screen where users can filter by a specific value. We can add this feature by adding a combo box or drop down control, and setting the items property to the following formula:
 
Choices(PropertyDocuments.DocumentType)
The Choices function returns the available choice values for a specified column. Assuming that we name our combo box control cboDocumentType, we can set the items property of the gallery control the following formula.

Filter(PropertyDocuments, 
DocumentType=cboDocumentType.Selected.Value
)


At run time, the user can then filter the gallery by the selected choice item in the combobox control.

Filtering a Dataverse choice column by the text value

There's sometimes the need to filter a choice column by the text of the choice item. A typical use case scenario is where we want to filter a choice column by a variable, or some other user input. Unfortunately, this task isn't entirely easy to carry out.

For example, let's say that we want to return all records with a document type that matches the text value "Audio". As we might expect, it's not possible to filter a choice column by a text value due to a mismatch in data types.



It's also not possible to match a choice value based on choice item that we return through a call to LookUp. This is unfortuanate because it uses syntax that is logically correct and syntax that I would expect to work.


Therefore, this limitation can force app builders to workaround this problem with formula that looks like this:
Switch(txtDocmentTypeSearch.Text, 
"Audio",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Audio),
"Document",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Document),
"Image",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Image),
PropertyDocuments
)
This not ideal because it imposes the requirement to hardcode every possible choice item. If we subsequently add an additional choice item to the column, we would need to respectively modify the formula.

Therefore, another workaround is to use a hidden combobox. This works on the basis that although it isn't possible to filter a choice column by a call to LookUp, it's possible to filter by the selected item of a combobox.

Let's suppose want to return records that match the document type that a user enters into a text input control called txtDocumentTypeSearch. We would add a combobox (let's call this cboDocumentTypeSearch), set the visible property to false, and set the items property to the following formula:
 
Filter(Choices(PropertyDocuments.DocumentType), 
Text(Value)=txtDocumentTypeSearch.Text
)

To specify a case insensitive search, we would apply the Lower function to both sides of the equality operator.

Next, we would set the items property of the gallery control to the formula beneath:
 
Filter(PropertyDocuments, 
DocumentType=cboDocumentTypeSearch.Selected.Value
)
At runtime, the user can now type a document type value into the text input control, and the gallery control will show matching records.

Conclusion

With Dataverse, we can create tables with choice columns. The syntax to filter a table based on a choice column may not be entirely intuitive. This post described how to filter a table by a choice column, including how to filter a table by the choice item text.

Related posts

Dataverse - How to set yes no values with checkbox- Walkthrough
January 29, 2025
Dataverse - Add image column missing when creating table - how to fix
January 19, 2025
Data - How to view the progress and status of a Dataverse CSV/Excel Import
January 15, 2025
Error - Diagnosing the error "Network error when using the Patch function" when saving data with a form
May 05, 2024
Dataverse - How to work around the error "multiple levels of many-to-one relationship expansion aren't supported"
April 05, 2024
Dataverse - The easiest way to modify Dataverse data outside of Dataverse and Power Apps
February 18, 2024
Dataverse - How to use a checkbox control a set a yes/no field in Dataverse
July 10, 2023
Dataverse - How to create Entity Relationship diagrams
January 22, 2023
Dataverse - How you can more quickly bulk update data using the SQL language
January 14, 2023
Dataverse - How to fix the bug in the 'Business Rules' editor that prevents numeric values from saving
January 05, 2023
Dataverse - What are the benefits, and how to create formula columns
July 20, 2022
Dataverse - How to sort the available choice items from a choice column
May 12, 2022
Error - Unable to modify Dataverse tables with error message, Language id should not be null
September 27, 2021
Dataverse - How to retrieve FetchXML or SQL for Dataverse views
July 13, 2021
Data - Why is Dataverse so fast?
June 15, 2021
Dataverse - How to create and use Dataverse views in Canvas Apps
June 09, 2021
Dataverse - How to switch to the classic designer when the option is not available
May 26, 2021
Licensing - What are Dataverse Restricted tables?
March 26, 2021
Media - How to save and work with microphone recordings
March 09, 2021
Dataverse - how to access tables from another environment
February 23, 2021
Dataverse - How to Patch the 5 most complex data types
January 19, 2021