cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
TerryBerryWerry
Regular Visitor

Issue with random pairing - Secret Santa Power App

I'm trying to build a Secret Santa app for my company, but I'm trying to contain all functionality within the Power App - no SP lists and no workflows.

 

I have a solution built that appears to be 99% reliable, but there's one scenario I haven't been able to crack.

 

This is the rough approach:

- User selects multiple people from a combo box, and this creates 2 identical collections containing the email address and name of each selected employee.

 

I then shuffle both collections:

 

ClearCollect(
    col_Team,
    Shuffle(col_Team)
);
ClearCollect(
    col_Team2,
    Shuffle(col_Team2)
);

 

 

Next, I create a new collection to contain the pairs of Gifters and Receivers, and the aim is to:

  • Prevent a gifter from getting themselves as a receiver.
  • Prevent duplicates in receivers.

The code below generates a perfect match up MOST of the time:

 

ForAll(
    col_Team As _gifters,
    Collect(
        col_Matches,
        {
            Gifter: _gifters.Email,
            Receiver: First(
                Shuffle(
                    Filter(
                        col_Team2 As _receivers,
                        _receivers.Email <> _gifters.Email && !(_receivers.Email in col_Matches.Receiver)
                    )
                )
            ).Email
        }
    );
    
);

 

 

Most of the time, my new collection will look something like this:

Gifters:Recievers:
Person APerson C
Person BPerson E
Person CPerson A
Person DPerson B
Person EPerson D

 

However, sometimes I can get something like this:

 

Gifters:Recievers:
Person APerson C
Person BPerson D
Person CPerson A
Person DPerson B
Person E 

 

It seems that the formula can't account for the possibility that the last Gifter from the first collection hasn't already been drawn as a Receiver from the second collection, and therefore they can't be assigned as themselves and will simply be skipped.

 

I've tried various approaches, but have run into all sorts of difficulties using Lookups within ForAll loops etc.

 

The code above seems to be the most effeicient and most reliable, but it's not 100%. 

 

Does anyone know how I can achieve 100% reliability without going beyond the app and using lists or workflows?

 

Thanks.

0 REPLIES 0

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (3,954)