ForAll function in Power Apps

The ForAll function evaluates a formula and performs actions for all the records in a table. In this context it works as a for each loop in the power app. The ThisRecord operator is used to refer the current record in the ForAll function in power apps.

Note: The formula returns a table. If the result of the formula is a single value, the resulting table will be a single-column table. If the result of the formula is a blank value, then there is no record in the result table for that input record. In this case, there will be fewer records in the result table than the source table.

Power Apps Syntax ForAll(Any Table, Something that we want to do for each record or item in the table)
Both the parameters of the function are required.

Exercise: The demonstration of ForAll function in a gallery control in Power Apps. A. For All Function with Collection
We can use the ForAll function with the collection datasource in powerapps.
Step 1: In the Power Apps Studio, Select the + Insert tab and add a Button control.

ForAll function in Power Apps

Step 2: Select the Button control, and set its Text property to "Create Collection". And set the OnSelect property to the following powerapps formula.

Power Apps Formula

ClearCollect (StudentsCollection,
{Name: "Ashish", City: "Sonipat", Marks: 80},
{Name: "Abhishek", City: "Mumbai", Marks: 70},
{Name: "Ayesha", City: "Delhi", Marks: 60},
{Name: "Priya", City: "Gurgaon", Marks: 40},
{Name: "Anjali", City: "Noida", Marks: 30},
{Name: "Shreya", City: "Delhi", Marks: 70}
)
ForAll function in Power Apps

Step 3: Hold the Alt Key and press the Button control. Add a Gallery control to the screen and choose StudentsCollection from the data source pop-up or set its Items property from the dropdown to StudentsCollection.

ForAll function in Power Apps

Do the necessary changes in the gallery, and it is looking like the image above. The current item in the gallery is accessed by ThisItem.ColumnName.

For example, the name of the collection is accessed by using the ThisItem.Name.

ForAll function in Power Apps

Step 4: Add another screen and add one Button control, set its Text property to "ForAll Collection" and set its OnSelect property to this formula:

Power Apps Formula

Clear(PassStudents);
ForAll(
Gallery1.AllItems,
If(
ThisRecord.Marks > 50,
Collect(
PassStudents,
{
StudentName: Name,
StudentCity: City,
StudentMarks: Marks
}
)
)
)

The overall purpose of this code seems to be filtering out students with marks greater than 50 from the "Gallery1.AllItems" collection and collecting their names, cities, and marks into a new collection named "PassStudents".

ForAll function in Power Apps

Hold the Alt key and click on “ForAll Collection” button. This creates the collection in the power apps.

Step 5: Add one more Vertical Gallery, and choose PassStudents from the data source pop-up or set its Items property to PassStudents.

ForAll function in Power Apps

In the following image, we can see that now the collection has StudentName for Name column and StudentCity for City column and StudentMarks for Marks column.

ForAll function in Power Apps

Step 6: Do the necessary changes in the gallery, and it is looking like the image above. The current item in the gallery is accessed by ThisItem.ColumnName.

We can also see that we have only records in this gallery where the Marks column value is more than 50, as stated in the formula.

ForAll function in Power Apps

B. Sharepoint as a Datasource

We can use ForAll function with the Sharepoint list as the datasource. The following is the sharepoint list named Teachers Data which we are using in the section.

ForAll function in Power Apps

Add the Teachers Data sharepoint list in the power apps. And then add one button control and set its OnSelect property to the following power apps formula.

Power Apps Formula

Clear(Teachers);
ForAll(
'Teachers Data',
If(
ThisRecord.'Teacher salary' > 25000,
Collect(
Teachers,
{
Name: 'Teacher name',
Qualification: 'Teacher Qualification',
Salary: 'Teacher salary'
}
)
)
)
ForAll function in Power Apps

Add one Gallery control and set its Items property to Teachers.

ForAll function in Power Apps

We can see the Teachers table.

ForAll function in Power Apps

Use of Sequence Function with ForAll Function
We can use the Sequence function in Power Apps with the ForAll function to iterate the function to a specific number of times.
The Sequence function generates a single column table of sequential numbers, such as 1, 2, 3. The name of the column is Value.
For example, Sequence(5) is equivalent to [1,2,3,4,5].

ForAll function in Power Apps

Power Apps Formula

ForAll(Sequence(5), {Result: ThisRecord.Value})
//The above formula can be written like this
//ForAll(Sequence(5), {Result: Value})

By using the above formula, we can change the column name from Value to Result.

ForAll function in Power Apps