web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Issue with random pair...
Power Apps
Answered

Issue with random pairing - Secret Santa Power App

(0) ShareShare
ReportReport
Posted on by

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.

Categories:
I have the same question (0)
  • AlvinCassar Profile Picture
    50 on at

    Hi, by any chance did you find a fix for your problem, as I am facing the same issue.

     

  • Verified answer
    TerryBerryWerry Profile Picture
    on at

    Hi AlvinCassar,

     

    I actually came up with a solution myself - it might be a big long winded, and probably not the best way, but it works 100% without issue. I'll try my best to explain in steps below:

     

    1. I create a collection of at least 3 colleagues from a people picker. Let's call it it col_Colleagues

     

    2. I create a new collection, that is simply the shuffled version of the previous collection.

     

    ClearCollect(
    col_Shuffle,
    Shuffle(col_Colleague)
    );

     

    3.  Next, I create another collection, col_Gifters - this is designed to be the person receiving the email to say they've been paired with someone (coming in the next step). I loop through the shuffled collection, and assign an order number.

    ForAll(col_Shuffle, //loop through the shuffled collection
     Collect(col_Gifters,
     {
     Email: ThisRecord.Email, //add the email address of colleague
     Order: CountRows(col_Gifters) + 1 //assign an order number
     }
     )
    );

     

    4.  I then loop through the col_Gifters collection, and create a new collection (col_Receivers), adding the colleague email, but incrementing the order number by 1.

     

    ForAll(col_Gifters As _gifters,
     Collect(col_Receivers,
     {
     Email: _gifters.Email, 
     //for the order, if the current record is the last record of col_Gifters, the order 
     //number is set to 1, otherwise it's incremented by 1
     Order: If(_gifters.Order = CountRows(col_Gifters),1,_gifters.Order + 1)
     }
     )
    );

     

    5. So now we have a collection for Gifters, and a collection for Receivers. Next step is to sort the Receivers collection by the order number.

    ClearCollect(col_Receivers,
     Sort(col_Receivers, Order, SortOrder.Ascending)
    );

     

    6.  So now if you look at both collections, Gifters is every colleague with an order number, lets say 1 to 5, and the Recievers is the same collection, but every colleague has moved down a place in the order. So if you hold each collection side by side, every colleague is matched with someone uniquely. How you proceed from here is optional, but I created one last collection to have the matched colleagues.

     

    ForAll(col_Gifters As _gifters,
     Collect(col_Matches,
     {
     GifterEmail: _gifters.Email,
     //receiverEmail is set based on the colleague from the gifters collection with the
     //same order number, meaning there can be no errors in matching
     ReceiverEmail: LookUp(col_Receivers, Order = _gifters.Order).Email
     }
     )
    )

     

    7. You can now loop through the col_Matches and send an email to the gifters email address, and add the receiver email address or name into the email body. Every colleague will be matched, and there's no chance of someone getting themselves. 

  • AlvinCassar Profile Picture
    50 on at

    Thank you TerryBerryWeey,

     

    Worked perfect and thank you for the explanation.  😀

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 834

#2
Valantis Profile Picture

Valantis 533

#3
Haque Profile Picture

Haque 410

Last 30 days Overall leaderboard