PowerApps ForAll Function with examples

In this PowerApps tutorial, We will discuss what is PowerApps ForAll function and its syntax, then we will see how to use the ForAll function in PowerApps. Also by taking some simple scenarios, We will cover all these below topics:

  • PowerApps ForAll Function Examples
  • Powerapps forall patch
  • Powerapps forall collection
  • Powerapps forall set variable
  • Powerapps forall gallery
  • Powerapps forall split
  • Powerapps forall patch lookup
  • Powerapps forall current item
  • Powerapps forall sum
  • Powerapps forall patch SharePoint list
  • Powerapps forall Combobox selecteditems

PowerApps ForAll Function

  • PowerApps ForAll function helps to evaluate the formula and perform actions for all the records in a table. Simply we can say it evaluates some functionality on each row of a particular table/collection or a database.
  • In the Powerapps ForAll function, the input and return values both are the same. That means whatever you will apply the input value, it will return the same value.
  • For example, in the For loop, if you are using a table, then the return value will be the table only. Not only a table but also you can use a collection or single-column table also.
  • If the return value is a bank value, then there is no record in the result table. It may be, there will be fewer records in the result table than the source table.
  • In the ForAll function in PowerApps, you can perform multiple operations by using a separator (;).
  • One important thing you should know is, As some PoweraApps functions like UpdateContext, ClearCollect, Clear, Remove, Set, etc. can hold the variable which is vulnerable to the ordering issues for the For loop. So we can not use these types of functions.
  • ForAll is not delegable while other functions may be, such as a Filter.

You may like PowerApps AddColumns Function with Examples.

PowerApps ForAll Function Syntax

Below represents the syntax of PowerApps ForAll function:

ForAll(Table, Formula)
  • Table = This is required to specify the table to be acted upon.
  • Formula = This is also required that specifies the formula to evaluate for all records of the Table.

PowerApps ForAll Function Examples

Example – 1: Using Calculation

  • In this example, We will see a simple calculation by using the Squares data source.
PowerApps ForAll Function
  • Here, We will create the Data source as a Powerapps Collection. For that, We need to apply this below formula on Button’s OnSelect property as:
OnSelect = ClearCollect( Squares, [ "16", "25", "36" ] )
  • Refer to the below screenshot. Preview (F5) the app and click on the button, then the Powerapps collection (Squares) will be created.
PowerApps ForAll Function examples
PowerApps ForAll Function
  • Next, Insert a PowerApps Data table to evaluate some formulas that how it can work with. Select the Data Table and apply the below formulas on its Items property as:
ForAll( Squares, Sqrt( Value ) )

Where,

Squares = PowerApps Collection Name

PowerApps ForAll function example
PowerApps ForAll Function Example
  • Unlock the Data table and Choose the fields that you want to add to the table.
  • Select the table -> Go to Properties -> Click on the Edit fields from Fields section -> + Add field -> Check on the Value -> Click Add as shown below.
PowerApps ForAll Functions
ForAll function in PowerApps
  • Once you will preview the app, you can see the result in the data table as like the below screenshot.
PowerApps ForAll Function Syntax
How to use ForAll function in PowerApps

Refer to the below table for further formulas, its descriptions and also Outputs:

Formula Description Output
ForAll( Squares, Sqrt( Value ) ) or Sqrt( Squares ) For all the records of the input table, calculates the square root of the Value column. The Sqrt function can also be used with a single-column table, making it possible to perform this example without using ForAll. 4,5,6
ForAll( Squares, Power( Value, 3 ) ) For all the records of the input table, raises the Value column to the third power. The Power function does not support single-column tables. Therefore, ForAll must be used in this case. 4096, 15625, 46656

Example – 2: Using a Connection

  • In this example, We will use a Data source named “Statements“. This table has these below values as:
Powerapps forall collection
  • In the same way as the previous example, Create this data source as a collection. Put this below formula on Button’s OnSelect property as:
OnSelect = ClearCollect( Statements, [ "Goodmorning", "Goodbye", "Hello"] )
  • Save and Preview the app. Once you will click on the button, then the Statement collection will be created in the Powerapps.
  • Next, Insert a Data table (same as the previous one) and perform with these below formulas to evaluate the different types of outputs. Refer to the below table:
Formula Description Output
ForAll(Statements, MicrosoftTranslator.Translate( Value, “es”)) For all the records in the Statements table, translate the contents of the Value column into Spanish (abbreviated “es”). “Buenosdias”
“Adios”
“Hola”
ForAll(Statements, MicrosoftTranslator.Translate( Value, “fr”)) For all the records in the Statements table, translate the contents of the Value column into French (abbreviated “fr”). “Bonjour”
“Au revoir”
“Salut”

Read Power Apps List Box Control – Complete tutorial

Powerapps forall patch

  • PowerApps Patch function is used to modify single or multiple records of a data source. In the other sense, the PowerApps Patch function is used to update the records in a data source without affecting other properties.
  • Similarly, to update the bulk amount of records in PowerApps, you can use the PowerApps ForAll patch function. To get more knowledge about the PowerApps Patch function, you can refer to the below link:

Powerapps forall set variable

  • Suppose you want to set a variable within the Powerapps ForAll function. Then, first of all, a simple question that may come to your mind is, Is it possible to set a variable within a ForAll function?
  • The answer to this question is, No. You can not set a variable within a ForAll function. Currently, it is not supported in the PowerApps.
  • As an alternative solution, you can achieve your needs by using the PowerApps collection. Although it does make the “code” a bit more difficult to read and also works well.
  • Follow these below steps to work with the Powerapps forall set variable:

Step-1:

To set a variable, apply this below formula on App’s OnStart property as:

OnStart = ClearCollect(
    colVariables,
    {colvarVariableOne: "POWERAPPS"}
);

Where,

  1. ClearCollect = ClearCollect is a method i.e. used to clear all the records from the Powerapps Collections and add a new record into it.
  2. colVariables = This is the variable name
  3. “POWERAPPS” = This is a string value that I have specified
Powerapps forall set variable
Powerapps forall set variable

Step-2:

To see or read the value, Insert a Label control and put this below formula on its Text property as:

Text = First(colVariables).colvarVariableOne

Save and Publish the app. Once you will open the app, you can see the specified value in the Label control as shown below.

Powerapps forall set variables
Powerapps forall set variable

Step-3:

Similarly, suppose you want to set the value use. Then add a Button input and apply this below formula on its OnSelect property as:

OnSelect = Patch(
    colVariables,
    First(colVariables),
    {colvarVariableOne: "NEW DEMO"}
);

Refer to this below screenshot:

Power apps forall set variable
Powerapps forall set variable example

Save and Preview the app. Press the button, then you can see the specified value in the Label control as shown below.

set variable in Powerapps forall function
set variable in Powerapps forall function

Step-4:

One thing that can catch you out is that collection column types are set when the collection is initiated so set the default value to be of that type. For example to create a datetime column use:

ClearCollect(colVariables, {colvarHoliday:DateTimeValue("01/01/2000 00:00"});

Powerapps forall gallery

In this example, We will see how to work ForAll function with PowerApps Gallery.

  • Basically, I have a Powerapps Vertical Gallery control. Also, I have two text input controls as “txtCarID” and “txtCarName“. Here what I want to do is, a user will fill in the text boxes and then click on the Save button.
  • Once the user will click on the save button, then the data will save in the Gallery control and also in the PowerApps Collection. Refer to the below screenshot.
Power apps forall gallery
Powerapps forall gallery
  • Select the Save button and apply this below formula on its OnSelect property as:
OnSelect = Collect(
    CarDetails,
    {
        ID: txtCarName.Text,
        Name: txtCarID.Text
    }
)

Where,

  1. CarDetails = PowerApps Collection name
  2. ID, Name = Header of the specific collection
  3. txtCarName, txtCarID = Text input control name
forall gallery in Powerapps
Powerapps forall gallery example
  • Save and Preview the app. Enter data in the ID and Name fields and then click on the save. The data will display in the gallery when the Data source name should be the right collection name. You can check it by using Gallery’s Fields property. See the below screenshot.
Powerapps forall function in gallery
Powerapps forall gallery
  • Similarly, If you will go to the PowerApps Collection (CarDetails), you can see the saved data as shown below.
Powerapps forall gallery
Powerapps forall function in gallery

Powerapps forall split

In this scenario, we will see how to split a text input in Powerapps forall function. You can apply the below formula to split a string and loop through the result of it.

ForAll(
    Split("This is a simple demo in PowerApps, " "),
    Collect(
        collectUsingSplit,
        Result
    )
);

In this above formula, I want to split the specified sentence with each and every space and then the result will be stored in the PowerApps Collection named as collectUsingSplit.

NOTE:

Not only you can split the string by a space (” “), but also you can split by using delimiter like Comma (,), Semicolons (;) etc.

Example:

  • Below represents two text input controls as “Car ID” and “Car Name“. I want to split the text inputs of the car name field and it will perform on the Powerapps Button input control (Click to split).
Powerapps forall split
Powerapps forall split
  • Select the button (Click to split) and set the below formula on it’s OnSelect property as:
OnSelect = ForAll(
    Split(
        txtCarName.Text,
        " "
    ),
    Collect(
        collectionUsingSplit,
        Result
    )
);

Where,

  • txtCarName = Car name text input control name
  • collectionUsingSplit = Powerapps collection name where the result will be stored
Power apps forall split
Powerapps forall split
  • Now Save and Preview the app. Enter some Car name by giving some spaces and click on the button (Click to split).
Powerapps forall function split
Powerapps forall split example
  • Next, go to the specified collection (collectionUsingSplit). You can see the car names as like the below screenshot.
Powerapps forall splits
Powerapps forall split

Powerapps forall patch lookup

  • Basically what happens is, in the ForAll function, you do not reference the collection name, only the column name of the collection. Suppose, {ID: ID} is the right syntax where only the column is references instead of {ID: Collection.ID}
  • Refer to the below syntax i.e. without reference to the table. You can use the column name and it takes the current value in the ForAll loop.
Patch(Datasource, {ID: ID}, {Column_A: Left(ReminderDates, 10), Column_B: Mid(ReminderDates, 12, 10), Column_C: Right(ReminderDates, 10)})

Example:

ForAll(cResponses, Patch(
	'[dbo].[Items]', 
	Defaults('[dbo].[Items]'), 
	{Survey_Key: Value(lblNewSurveyKey.Text)}, 
	{SurveyResponse_Key: Response_Key}, 
	{Survey_Status: rbStatus.Selected.Value}, 
	{Survey_Comments: txtTraining.Text}, 
	{Survey_CreatedBy: lblUserName_1.Text}, 
	{Survey_CreatedOn: Now()}, 
	{Survey_ModifiedBy: lblUserName_1.Text}, 
	{Survey_ModifiedOn: Now()}))

Powerapps forall current item

In this example, We will check how to work with the current item or record that is processing in the ForAll loop.

  • Suppose I have PowerApps ForAll function and need to patch the record. I need id of the current record in the for all function to use it in the patch function.
  • Let us take a simple example. Our aim is to update several records i.e. currently having a Status of “Open” to the status “Closed”. As we have already the ID of all records that we want to update stored in a collection called allRecords. Refer to the below code:
ClearCollect(
    allRecords,
    {ID:2001},{ID:1205},{ID:1313},{ID:1460},{ID:1510}
);

Where,

allRecords = PowerApps Collection Name

  • To perform the patch we can use this code. As you can notice that how I have referenced the current ID being looped over by writing allRecords[@ID]. This technique is called disambiguation.
  • The column ID exists in both allRecords and mySharePointList so we must tell PowerApps to look at allRecords using the square brackets otherwise it will just look at the ID column for the current scope of LOOKUP which is mySharePointList. Refer the below code:
ForAll(
    allRecords,
    Patch(
        mySharePointList,
        LookUp(mySharePointList, ID: allRecords[@ID]),
        {Status: "Closed"}
);

Powerapps forall sum

In this scenario, We will see how we can use PowerApps sum function in ForAll function.

  • In the below screenshot, you can see there is a PowerApps Collection named ProductDetails. In that collection, there is a column named Price that specifies all the individual product prices.
  • Here what I want to do is, I need to sum all of the Product Prices from that specific collection and the result will display in a Label control.
Powerapps forall sum
Powerapps forall sum
  • To do this, Apply this below formula on Label’s Text property as:
Text = Sum(ProductDetails, Price)

Where,

  1. ProductDetails = PowerApps Collection Name
  2. Price = Column of the specific collection
Powerapps forall sum function
Powerapps forall sum example
  • Now Save and Preview the app. You can see the sum of the product price in the label control as shown in the below screenshot.
Powerapps sum in forall function
Powerapps forall sum
  • Similarly, Enter the Product Name, Price, and the Quantity fields and then click on the button (Click to Collect). Then you can see the total price sum in the label control as shown below.
Power apps forall sum
Powerapps sum in forall function

Powerapps forall patch SharePoint list

In this topic, We will discuss how the PowerApps forall Patch function will work with the SharePoint List.

  • Suppose you are trying to patch the collection created through collect to the SharePoint list. In the PowerApps screen, I have a SharePoint list named Products.
  • Let’s assume that all of the referenced controls (like TextInput_ProductName) are part of your gallery. In this case, you need to reference the input controls as part of the gallery.
ForAll(
   ProductDetails.AllItems, 
      Patch('Products', 
         Defaults('Products'), 
         {'Product Name': TextInput_ProductName.Text, 
          'Product Type': DD_ProductType.Selected, 
          Department: DD_Department.Selected, 
          'Customer Name': TextInput_Customer.Text, 
          Quantity: Value(TextInput_Quantity.Text),
	  Reason: DD_Reason.Selected, 
          Comment: TextInput_Comment.Text}))

Where,

ProductDetails = PowerApps Gallery control

  • This will go through each item in your Gallery and patch it to the SharePoint list.
  • Suppose in case, you found that the “YourGalleryName.AllItems” does not work for you i.e. ProductDetails.AllItems
  • Then instead of that above code, you can use this below formula as:
ForAll(
   YouGalleryName, 
      Patch('SP List Name', 
         Defaults('Sp List Name'), 
         {'Column Name': LineColumnName, 
          'Column Name2': LineColumnName2 
          }))

Where,

LineColumn(s) = Column Defaut value

Powerapps forall Combobox selecteditems

Here we will see how to work with Powerapps forall Combobox selecteditems.

  • Suppose you want to get all selected items from a PowerApps Combobox control. The below screenshot represents the PowerApps Combobox control and a Label control.
  • The Combobox control is having items on its Items property as:
Items = ["APPLE", "ORANGE", "GRAPES", "MANGO"]
  • In this scenario what I want to do is, whatever I will choose the value from the Combobox, that will show in a label control.
  • For that, Select the Label control and apply this below formula on its Text property as:
Text = ComboBox1.Selected.Value

Where,

ComboBox1 = Combo box control name

Powerapps forall Combobox selecteditems
Powerapps forall Combobox selecteditems
  • Now Save and Preview the app. Select one or multiple items from the combo box. Here you can see only the last selected combobox entry rather than all items.
  • To display all selected items (from the combo box) in the Labe control, follow the below formula.
Powerapps forall Combobox selecteditem
Powerapps forall Combobox selecteditems example
  • Select the Label control and apply this below formula on it’s Text property as:
Text = Concat(
    ComboBox1.SelectedItems,
    Value & ","
)
Combobox selecteditems in Powerapps forall function
Powerapps forall Combobox selecteditems
  • Again save and preview the app. Select some items from the Combo box control. Then at the same time, the selected items will be displayed in the label control with a delimiter (,) as shown in the below screenshot.

NOTE:

Keep on note that, “Selected” is mostly used in “single selection” mode (ComboBox1.SelectMultiple=false). Similarly, “SelectedItems” property however contains all the selected items in “multiple selection” mode.
Combobox selecteditem in Powerapps forall function
Powerapps forall Combobox selecteditems

Also, you may like these below PowerApps Tutorials as:

In this Powerapps tutorial, We discussed what is PowerApps ForAll function and how to use the ForAll function in PowerApps. Also by taking some simple scenarios, We covered all these below topics:

  • PowerApps ForAll Function Examples
  • Powerapps forall patch
  • Powerapps forall collection
  • Powerapps forall set variable
  • Powerapps forall gallery
  • Powerapps forall split
  • Powerapps forall patch lookup
  • Powerapps forall current item
  • Powerapps forall sum
  • Powerapps forall patch SharePoint list
  • Powerapps forall Combobox selecteditems
>