PowerApps Remove Duplicate in a Collection

In some cases, the collection may have duplicate data that needs to be removed before consuming it. This blog will discuss how to remove duplicates in a collection.

Sample Collection

In the above example, we need to remove the duplicate item which is ID : 3

Steps

  1. Need to find distinct based on some column (Example: CreatedDate)
  2. Go through the distinct collection and get the first record for the same column

1. Get Distinct (Collection Name is colSample)

ClearCollect(
colDistinct,
Distinct(
colSample,
CreatedDate
)
);

 Output for the above command

2. Clear the collection before collect

Clear(colFinal);

3. Loop through the Distinct collection and get the first item from the main collection

ForAll(
colDistinct,
Collect(
colFinal,
First(
Filter(
colSample,
CreatedDate = Result
)
)
)
);

Output for the previous command

How to test it?

  • Create a new empty screen
  • Add button or icon
  • Place the below code OnSelect property

Complete Command with Sample Data

ClearCollect(
colSample,
{
CreatedDate: “08-05-2021”,
Title: “PowerApp”,
ID:1
},
{
CreatedDate: “08-06-2021”,
Title: “UserGroup”,
ID:2
},
{
CreatedDate: “08-06-2021”,
Title: “UserGroups”,
ID:3
},
{
CreatedDate: “08-03-2021”,
Title: “UserCommunity”,
ID:4
}
);
ClearCollect(
colDistinct,
Distinct(
colSample,
CreatedDate
)
);
Clear(colFinal);
ForAll(
colDistinct,
Collect(
colFinal,
First(
Filter(
colSample,
CreatedDate = Result
)
)
)
);

Leave a Reply

Your email address will not be published.