Blog
Dataverse - How to filter Dataverse choice columns
June 7. 2021
With Dataverse, the syntax to filter a table based on a choice column may not be entirely intuitive. This post describes the methods we can use to filter tables based on choice values.
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)
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
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
)
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:
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.
Switch(txtDocmentTypeSearch.Text,
"Audio",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Audio),
"Document",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Document),
"Image",
Filter(PropertyDocuments, DocumentType=[@DocumentType].Image),
PropertyDocuments
)
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,At runtime, the user can now type a document type value into the text input control, and the gallery control will show matching records.
DocumentType=cboDocumentTypeSearch.Selected.Value
)
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.- Categories:
- dataverse
Related posts
- Error - Diagnosing the error "Network error when using the Patch function" when saving data with a form
- Dataverse - How to work around the error "multiple levels of many-to-one relationship expansion aren't supported"
- Dataverse - The easiest way to modify Dataverse data outside of Dataverse and Power Apps
- Dataverse - How to use a checkbox control a set a yes/no field in Dataverse
- Dataverse - How to create Entity Relationship diagrams
- Dataverse - How you can more quickly bulk update data using the SQL language
- Dataverse - How to fix the bug in the 'Business Rules' editor that prevents numeric values from saving
- Dataverse - What are the benefits, and how to create formula columns
- Dataverse - How to sort the available choice items from a choice column
- Error - Unable to modify Dataverse tables with error message, Language id should not be null
- Dataverse - How to retrieve FetchXML or SQL for Dataverse views
- Data - Why is Dataverse so fast?
- Dataverse - How to create and use Dataverse views in Canvas Apps
- Dataverse - How to switch to the classic designer when the option is not available
- Licensing - What are Dataverse Restricted tables?
- Media - How to save and work with microphone recordings
- Dataverse - how to access tables from another environment
- Dataverse - How to Patch the 5 most complex data types