Expressions

Collecting an item multiple times in a collection

Another interesting scenario that I came across at the Power Apps Community forum was the need to add an item multiple times into a collection based on a number entered by the user.

The first step is to create a collection of numbers:

ClearCollect(NumberCollection,["1","2","3","4","5","6","7","8","9","10",...,"98","99","100",...])

Populate this collection until a max number that you want to allow the user to enter. Create this collection in the OnStart property of the app or in the OnVisible property of the screen where the user will input the number (or any screen prior to this screen).

Let's say the user enters a number in a text input control named TextInput1. To keep it simple, let's assume that the item that needs to be collected multiple times is in a label named Label1.

The OnSelect property of the button which will be used to enter the text from Label1 multiple times based on the number from TextInput1 in a collection (Collection1) will be:

ForAll(FirstN(NumberCollection,Value(TextInput1.Text)), Collect(Collection1, Label1.Text))

The text from TextInput1 is first converted into a number (denoted by n moving forward) using the Value function. The FirstN function then returns the first set of records from the NumberCollection with the number of records equal to n. Then finally the ForAll function looks through n number of times and each time it collects the text from Label1 into Collection1.

Here is a working example:

Hopefully, this will help anyone with a need to repeat an action multiple times based on a number captured dynamically (vs a static number).

Have fun! Get addicted!

1 thought on “Collecting an item multiple times in a collection”

Leave a Reply