The Distinct function in Power Apps is used to extract unique values from a column of a table or collection. This function is particularly useful for removing duplicates and obtaining a list of unique values, such as when creating dropdown menus or filtering data.
PowerApps – Distinct() function Syntax
- Table: The table or collection from which you want to extract unique values.
- Column: The column for which you want to find distinct values.
Notes:
- Column Name: Ensure that the column name passed to the Distinct function is correct and exists in the table or collection.
- Combining with Other Functions: Combine Distinct with other functions like Filter, Sort, CountRows, and Concat to perform complex data manipulations and analyses.
- Performance: Be mindful of the performance when using Distinct with large datasets. Consider using delegation where possible to handle large data sources efficiently.
Suppose you have a collection named Products and you want to extract a list of unique product names.
- Create the Collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500},
{ProductID: 3, ProductName: "Laptop", Price: 1200},
{ProductID: 4, ProductName: "Monitor", Price: 300}
)
);
- Extract Unique Product Names:
- Add a Label control.
- Set the Text property of the Label to:
Concat(Distinct(Products, ProductName), Result & ", ")
This will display the unique product names: "Laptop, Tablet, Monitor".
Suppose you want to populate a dropdown menu with unique product categories from a collection.
- Create the Collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductCategory: "Electronics"},
{ProductID: 2, ProductCategory: "Furniture"},
{ProductID: 3, ProductCategory: "Electronics"},
{ProductID: 4, ProductCategory: "Appliances"}
)
);
- Populate the Dropdown Menu:
- Add a Dropdown control.
- Set the Items property of the Dropdown to:
Distinct(Products, ProductCategory)
This will populate the dropdown with unique product categories: "Electronics", "Furniture", "Appliances".
PowerApps – Count Unique (Distinct) Values
Suppose you want to count the number of distinct product categories in a collection.
- Create the Collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductCategory: "Electronics"},
{ProductID: 2, ProductCategory: "Furniture"},
{ProductID: 3, ProductCategory: "Electronics"},
{ProductID: 4, ProductCategory: "Appliances"}
)
);
- Count the Distinct Product Categories:
- Add a Label control.
- Set the Text property of the Label to:
CountRows(Distinct(Products, ProductCategory))
This will display the number of unique product categories, which is 3.