Blog
Data - How to create bulk test/dummy records with random values
When building apps, it's useful to be able to populate tables with dummy or test records so that we can test and see how our app behaves. This post demonstrates formula to generate multiple records with random values, and how to add those records to a table.
Example - the table where we'll create test records
Basic Syntax - How to add 100 dummy/test records to a table
Let's look at the basic syntax to create 100 test records. From our app, we connect to the source table, and apply the following formula to a button
Collect(Properties,
ForAll(Sequence(100,1,1),
{Address1:"Address " & Text(Value)}
)
)
This formula relies on several key functions. The Collect function adds multiple new records to a table (in this example, Properties is the name of the table)
For each record in this sequence, we apply the ForAll function to return a record with the field name "Address1", with a value that matches the number of the sequence and is prefixed with the text "Address".
Generating random whole numbers
Starting with how to generate random numbers, here's the formula to create a random number between a start and end value (1 and 8 in this example).
With({randMin:1, randMax:8},
RoundDown(Rand()*(randMax-randMin + 1) + randMin,0)
)
Generating random decimal numbers
To generate random decimal numbers, we can modify our formula from above, and specify the target number of decimal places in the call to the RoundDown function.
As an example, here's the formula to generate a random value between 100,000 and 200,000, with 2 decimal places.
With({randMin:100000, randMax:200000},
RoundDown(Rand()*(randMax-randMin + 1) + randMin,2)
)
Generating random dates
To generate a random date, we can generate a random number, and to add or subtract that number from a base date. Here's how to how to generate a random date within the past four years (eg - 365 days *4).
With({randMin:1, randMax:(365 * 4)},
DateAdd(Now(),
-1 * RoundDown(Rand()*(randMax-randMin + 1) + randMin,0)
)
)
Generating random LookUp values
Where we want set a choice column to a random record, we can call the Shuffle function. The Shuttle function re-orders the records in a table in a random way, and we can select the first record from the result to retrieve a random record.
First(
Shuffle(PropertyTypes)
)
Generating random boolean, yes/no, true/false values
To generate a random boolean value, we can apply the formula beneath.
Round(Rand(),0) = 1
How to create test records with random data
Collect(Properties,
ForAll(Sequence(100,1,1),
With({
roomsMin:1,
roomsMax:8,
daysMax:(365*5)
},
{
Address1:"Address " & Text(Value),
AquisitionDate:
DateAdd(Now(),
-1 * RoundDown(Rand()* daysMax,0)
),
Bedrooms:
RoundDown(Rand()*(roomsMax-roomsMin + 1) + roomsMin,0),
Garden: If((Round(Rand(),0) = 1),
'Garden (Properties)'.Yes,
'Garden (Properties)'.No
),
PropertyType:
First(Shuffle(PropertyTypes))
}
)
)
)
Conclusion
- Categories:
- data
- Data - How to find the common rows from 3 or more collections
- Data - How to show the distinct rows from 2 data sources or collections
- Data - How to implement circular rotational date sorting
- Bug - What to do when the data section of the Power Apps Maker portal doesn't work
- Data - Combine columns from separate tables into a single table
- Formula - Transposing/converting rows to columns- an almost impossible task?
- Data - How to rename field names in a record
- Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
- Data - Retrieving news/forum/blog articles with RSS
- Data - How to sort by partial numbers in a text field
- Data - How to return the last record from a table
- Data - 3 things you should know before using the MySQL or PostgreSQL connectors
- Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
- Data - How to enforce unique values (or prevent duplicate values) in one or more columns
- Data - How much mobile data does Power Apps consume? What ways can we minimise this?
- Data - How to save and retrieve Google calendar entries
- Data - How to save and retrieve Google contacts
- SQL - Caution! This is how users can hack shared SQL connections
- SharePoint – 2 Mistakes to avoid when importing Excel data
- SQL - Don't let this DateTime bug catch you out!
- Settings - What's the purpose of the "Explicit Column Selection" Setting?
- SQL Server for Beginners Part 3 - Installing On-Premises Gateway
- SQL Server for Beginners Part 2 - Installing Management Studio
- SQL Server for Beginners Part 1 - Installing SQL Server
- Searching data–What you need to know about case sensitivity
- Images - How to create images that can change depending on data
- Excel - Reasons NOT to use Excel as a data source
- SharePoint - What you need to know about Filtering Data
- Formulas - Generating Row Numbers