cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Filter a collection for distinct items within separate categories

Ok, this should be straightforward to solve, but I can't find any combination of filter() and distinct() that works...

 

Let's say I have a collection as follows:

 

Gender          Hair Colour

Male               Ginger

Female           Brown

Male               Ginger

Male               Ginger

Female            Blonde

Male                Ginger

Female            Ginger

 

The aim is to use a countrows() function to make a label visible if 2 or more males have different hair colours.  Or, if 2 or more females have different hair types. 

 

Something along the lines of countrows(distinct(collection, hair colour)). 

 

So in this example, the label should appear, because different hair colours have been entered for females (even though this is not true for males). 

 

I am sure there's a way to do this using distinct() on both columns! Hope this is clear. Thanks. 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

@Anonymous 

I'll post anyways in case it help.  To get a result showing matching records...

 

aa.PNG

 

You would use some additional code at the bottom of what I already provided.

//Create a collection for testing duplicate removal
ClearCollect(myPeople,
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Brown"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Blonde"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Ginger"}
);

//Duplicate removal code
ClearCollect(myUniqueRecords,
    DropColumns(
        AddColumns(
            Distinct(
                AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
                CombinedColumns
            ),
            "Gender",First(Split(Result,";")).Result,
            "Hair Colour",Last(Split(Result,";")).Result
        ),
    "Result"
    )
);

//Count matching records in original collection
ClearCollect(
    myCountedRecords,
    AddColumns(
        myUniqueRecords,
        "MatchingRecords",
        CountRows(Filter(myPeople,Gender=myUniqueRecords[@Gender],'Hair Colour'=myUniqueRecords[@'Hair Colour']))
    )
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

View solution in original post

9 REPLIES 9
mdevaney
Super User
Super User

@Anonymous 

This was a fun exercise to figure out!

 

//Create a collection for testing duplicate removal
ClearCollect(myPeople,
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Brown"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Blonde"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Ginger"}
);

//Duplicate removal code
ClearCollect(myUniqueRecords,
    DropColumns(
        AddColumns(
            Distinct(
                AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
                CombinedColumns
            ),
            "Gender",First(Split(Result,";")).Result,
            "Hair Colour",Last(Split(Result,";")).Result
        ),
    "Result"
    )
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Anonymous
Not applicable

Thanks, I'm still trying to get my head around the code but appreciate your work here! Will report back asap. 

@Anonymous 

I'll try to explain it in plain English.

To find the unique values in a collection we use the DISTINCT function.  We want to perform but DISTINCT only works on a single column DISTINCT on two columns.  Therefore we must combine both columns together into a single column holding a text string and delete the duplicates.

 

Distinct(
    AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
    CombinedColumns
)

 

Now that we've deleted the duplicates we have another problem.  We only have a single column in our collection but our original collection had two.  We can ADDCOLUMNS and USE SPLIT + FIRST to isolate the Gender and SPLIT + LAST to isolate the Hair Colour.

 

AddColumns(
    Distinct(
        AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
        CombinedColumns
    ),
    "Gender",First(Split(Result,";")).Result,
    "Hair Colour",Last(Split(Result,";")).Result
)

 

Finally, we delete the column we used to remove any duplicate values which is now called Result.  The output from the DISTINCT function is always called Result.

 

ClearCollect(myUniqueRecords,
    DropColumns(
        AddColumns(
            Distinct(
                AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
                CombinedColumns
            ),
            "Gender",First(Split(Result,";")).Result,
            "Hair Colour",Last(Split(Result,";")).Result
        ),
    "Result"
    )
)

 

Suggest you try this step by step like I did and look at the effect each code block has on the Collection.

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

 

@Anonymous 

Did you have a chance to try my solution?  Noticed you were online so I thought I would ask.

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Anonymous
Not applicable

@mdevaney  apologies-I have been snowed under with an impending deadline (we are trying to get an app out by christmas!)

 

I will give your solution a check at the first opportunity, I promise. I am very grateful for the time and effort you have put into helping me with this issue. I just need to fix some other stuff with my current project 🙂

@Anonymous 

The reason I had asked was the question you posted this morning had some similarities (although I did not fully understand it).  My thought was that once you had the unique pairings using my method it would be easy to do your checks for which records had duplicates in the original collection.

Just wanted to make sure you saw my response in case it was helpful solving your current problem. Best of luck building your app.

Link To New Post:

https://powerusers.microsoft.com/t5/Building-Power-Apps-Formerly/Check-if-a-record-matches-another-record-in-a-collection-THEN/m-p/419685#M124200

@Anonymous 

I'll post anyways in case it help.  To get a result showing matching records...

 

aa.PNG

 

You would use some additional code at the bottom of what I already provided.

//Create a collection for testing duplicate removal
ClearCollect(myPeople,
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Brown"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Blonde"},
{Gender: "Male", 'Hair Colour': "Ginger"},
{Gender: "Female", 'Hair Colour': "Ginger"}
);

//Duplicate removal code
ClearCollect(myUniqueRecords,
    DropColumns(
        AddColumns(
            Distinct(
                AddColumns(myPeople,"CombinedColumns",Gender&";"&'Hair Colour'),
                CombinedColumns
            ),
            "Gender",First(Split(Result,";")).Result,
            "Hair Colour",Last(Split(Result,";")).Result
        ),
    "Result"
    )
);

//Count matching records in original collection
ClearCollect(
    myCountedRecords,
    AddColumns(
        myUniqueRecords,
        "MatchingRecords",
        CountRows(Filter(myPeople,Gender=myUniqueRecords[@Gender],'Hair Colour'=myUniqueRecords[@'Hair Colour']))
    )
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

View solution in original post

Anonymous
Not applicable

@mdevaney  Thanks so much for the help with this, and sorry it's taken me so long to get back to you. I have finally had a chance to check out your solution, and it has saved me a lot of headaches! 

 

Thank you very much. Hopefully this will save a lot of time for future powerapps community folk! 🙂 

Thanks for coming back to mark the solution @Anonymous !

 

I had a very good time thinking about how this could be done.  I'll be sure to watch out for other interesting questions from you in the future 😺

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (1,067)