Generating Random Numbers in Power Apps Best Practices & Solutions

Generating Random Numbers in Power Apps: Best Practices & Solutions

Randomly selecting records or rows from a data source is a frequent requirement in app development. Whether you’re building a game, a dashboard, or a task allocation app, being able to fetch random data comes in handy. In Power Apps, there are specific functions designed for this purpose. However, understanding their strengths and limitations is key to using them effectively. This blog post will guide you through the process and provide you with efficient solutions to common challenges.

Why Randomize Records in Power Apps?

In Power Apps, there can be several scenarios where you might want to pick one or multiple random records from a data source, such as:

  • Displaying random information on a dashboard.
  • Fetching a random question for gaming applications.
  • Selecting a random employee for task assignments.

The Basic Approach: Using the Shuffle Function

The most straightforward method to fetch a random record is by utilizing the Shuffle function. When provided with an input table or data source, it returns a randomized table of records. For instance, if we consider a SharePoint list of property records, you can retrieve a single random record with the following formula:

    Set(newVarRecord,
        First(Shuffle(PropertyList))
    )
    

Then, to display the selected record, you can incorporate a display form on your screen and set its item property to newVarRecord.

Limitations of the Shuffle Function

While the Shuffle function is user-friendly, it’s not delegable. This means it may not return random records from larger data sources, especially those exceeding 2,000 records. For example, if the app’s “data row limit” is set to 10, the function will fetch 10 rows and select a random entry from this subset.

Overcoming the Delegation Limit of Shuffle

To truly harness the full potential of your data source, it’s often vital to fetch random records from the entire set. If your data source includes a numeric identity column, this attribute can be leveraged to retrieve a random record. The approach involves using the Rand() function to produce a number within the range of the highest numeric value and then fetching the record that surpasses this number.

Assuming your SharePoint list has a column named “PropertyID” that contains a unique number for each entry, the formula to achieve this would be:

    With({maxID:First(FirstN(SortByColumns(PropertyList, "PropertyID",Descending),1)).PropertyID},
          With({randomNum:maxID * Rand()},
                 Set(newVarRecord, First(FirstN(Filter(PropertyList, PropertyID >= randomNum ),1))))
    )
    

This formula efficiently calculates a random number and then uses it to delegate and fetch a random record.

Reference: 

 

Conclusion

In Power Apps, the Shuffle function provides a simple way to obtain random records. However, its delegation limitations can be a challenge when working with larger data sources. But with a thoughtful approach and the right formulas, you can bypass these limitations and achieve desired results.

If you have any questions or need further assistance with Power Apps or any other technical issues, do not hesitate to contact us. Our team is always here to help, and we offer expert consultation services that can assist in resolving your challenges.

Enhanced Support Widget