A
C
D
I
R
S
T
What is the Shuffle function?
The Shuffle function is a Power Fx function that randomly reorders the records in a table or records in a collection. The function takes one argument, which is the table or collection that you want to shuffle. The Shuffle function returns a table with the same columns and data as the original table, but with the rows randomly reordered.
How to use the Shuffle function in Power Apps
To use the Shuffle function in Power Apps, you need to first create a collection and populate it with data. You can do this by using the Collect function, which adds records to a collection. Here’s an example of how to create a collection with four records:
ClearCollect(
MyCollection,
{ Item: “Apple”, Price: 0.99 },
{ Item: “Banana”, Price: 0.49 },
{ Item: “Orange”, Price: 0.79 },
{ Item: “Grape”, Price: 1.29 }
)
In this example, we're creating a collection called MyCollection with four records, each containing an Item and Price field. Once you have a collection with data, you can use the Shuffle function to randomize the order of the records. Here's an example of how to use the Shuffle function:
ClearCollect(
ShuffledCollection,
Shuffle(MyCollection)
)
In this example, we're creating a new collection called ShuffledCollection, which contains the same data as MyCollection, but with the records randomly reordered. To achieve this, we're using the Shuffle function, which takes MyCollection as its argument and returns a shuffled table. We're then using the ClearCollect function to add the shuffled table to the ShuffledCollection.
Using the Shuffle function for random selection
Another use case for the Shuffle function is randomly selecting an item from a collection. For example, you might have a collection of products and want to display a random product on the screen every time the app is opened. Here's an example of how to use the Shuffle function for random selection:
Set(
RandomProduct,
FirstN(
Shuffle(MyCollection),
1
)
)
In this example, we're using the Set function to store a randomly selected product in a variable called RandomProduct. To do this, we're using the FirstN function to select the first record from a shuffled version of MyCollection. The FirstN function takes two arguments: the table to select records from, and the number of records to select. In this case, we're selecting one record.
The Shuffle function is a powerful tool that can help you randomize data in Power Apps. Whether you want to display a list of items in a different order every time the app is opened, or you want to select a random item from a collection, the Shuffle function can make it easy. By following the steps outlined in this article, you can start using the Shuffle function in your own Power Apps projects.