cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

getting a random item set from a collection

hi,

I have a collection with 15 items and want to get a random set of 8 items out of it.

without a do-while-loop, how would I do that and still prevent to get duplicate items in the result set?

thanks

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Super User III
Super User III

Re: getting a random item set from a collection

Hi @GN

 

One way to do this is to add a random number column to your collection with the Rand function. You can then sort this result by the random number column, and return the top 8 rows.

 

In my example, the collection name is User.

 

RandomNumber.JPG

 

The formula that returns 8 random records looks like this:

 

FirstN(
   SortByColumns(
      AddColumns(User, "RandomNumber", Rand()), 
      "RandomNumber"
   ),
8 )

View solution in original post

Highlighted
Solution Sage
Solution Sage

Re: getting a random item set from a collection

Hello,

You can also use the Shuffle function
Sample:

Button1.OnSelect = ClearCollect(A,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
Gallery1.Items = A

Button2.OnSelect = ClearCollect(B,FirstN(Shuffle(A),8))
Gallery2.Items = B

 

Press Button1 and then Button2 and check Gallery2

View solution in original post

6 REPLIES 6
Highlighted
Super User III
Super User III

Re: getting a random item set from a collection

Hi @GN

 

One way to do this is to add a random number column to your collection with the Rand function. You can then sort this result by the random number column, and return the top 8 rows.

 

In my example, the collection name is User.

 

RandomNumber.JPG

 

The formula that returns 8 random records looks like this:

 

FirstN(
   SortByColumns(
      AddColumns(User, "RandomNumber", Rand()), 
      "RandomNumber"
   ),
8 )

View solution in original post

Highlighted
Frequent Visitor

Re: getting a random item set from a collection

just the idea I was looking for! thx alot. will try it.

Highlighted
Microsoft
Microsoft

Re: getting a random item set from a collection

 
Highlighted
Solution Sage
Solution Sage

Re: getting a random item set from a collection

Hello,

You can also use the Shuffle function
Sample:

Button1.OnSelect = ClearCollect(A,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
Gallery1.Items = A

Button2.OnSelect = ClearCollect(B,FirstN(Shuffle(A),8))
Gallery2.Items = B

 

Press Button1 and then Button2 and check Gallery2

View solution in original post

Highlighted
Super User III
Super User III

Re: getting a random item set from a collection

@Mike8- thanks for the suggestion of using the Shuffle function. That's a really neat solution!

Highlighted
Solution Sage
Solution Sage

Re: getting a random item set from a collection

🙂

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Tech Marathon

Maratón de Soluciones de Negocio Microsoft

Una semana de contenido con +100 sesiones educativas, consultorios, +10 workshops Premium, Hackaton, EXPO, Networking Hall y mucho más!

Top Solution Authors
Top Kudoed Authors
Users online (5,176)