Skip to content
Home » With function in Power Apps

With function in Power Apps

The With function in Power Apps evaluates a formula for a single record. It improves the readability of complex formulas by dividing it into smaller named sub-formulas and make them easier to understand.

The formula can calculate a value and/or perform actions, such as modifying data or working with a connection.

With( Record, Formula )

  • Record – Required. The record to be acted upon. For names values, use the inline syntax { name1: value1, name2: value2, … }
  • Formula – Required. The formula to evaluate for Record. The formula can reference any of the fields of Record directly as a record scope.

Let’s take a look at below formula, which display the name and email id of current users on text input control.




This formula is using Office365users data source to get the details of current user.

"User: " & Office365Users.UserProfileV2(User().Email).displayName 
& Char(13) & "Email: " & 
Office365Users.UserProfileV2(User().Email).mail

You can see, it returns the current user’s details.

Although, it is returning a data as expected. But note that, the Office365users data source is called for two times, one for getting current user’s name and second time for getting mail id.

Let’s consider a situation where you need to add few more details in this formula such as department name, job title and city then you have to call Office365users data source every time to get each individual details.

Once you add these details, you will see formula becomes more complex, lengthy and hard to read and understand.

Also, it duplicated the work effort as writing the same piece of code again and again as shown below.

"User: " & Office365Users.UserProfileV2(User().Email).displayName & Char(13) & 
"Email: " & Office365Users.UserProfileV2(User().Email).mail & Char(13) & 
"Department Name: " & Office365Users.UserProfileV2(User().Email).department & Char(13) & 
"Job Title: " & Office365Users.UserProfileV2(User().Email).jobTitle & "City: " & 
"City" " & Office365Users.UserProfileV2(User().Email).city

Using With function

To simplify the above formula, and make it easier to understand also reduce the multiples call to Office365Users data source, we can use With function as shown below.

With(
{currUserDetails: Office365Users.UserProfileV2(User().Email)},
"User: " & currUserDetails.displayName & Char(13) & 
"Email: " & currUserDetails.mail
)

You can see, it returns same output as well as reduce the complexity of formula.

Let’s display all details and you can see, how it simply the complex formula to efficient and more readable format also reduce the duplicate work.

Also, this time Office365Users data source is called one time only.

With(
{currUserDetails: Office365Users.UserProfileV2(User().Email)},
"User: " & currUserDetails.displayName & Char(13) &
"Email: " & currUserDetails.mail & Char(13) & 
"Department Name: " & currUserDetails.department & Char(13) & 
"Job Title: " & currUserDetails.jobTitle & 
"City: " & currUserDetails.city
)

One more example of using the With function

Create a formula to calculate such as adding, subtraction, multiplication and division of two input values coming from text input controls.




As you can see, here we have taken two text input control named txt_value1 and txt_value2 to take input values value 1 and value 2 from user.

Other than this, there are two more controls, one is drop down control named drp_operation which allow user to select desired operations from list such as ADD, MUL, SUB, and DIV and another is text label control named lbl_output which display the result.

Now, you can see the formula written on the Text property of text label control txt_output.

With (
{
val1: Value(txt_val1.Text),
val2: Value(txt_val2.Text),
opr: drp_operation.SelectedText
},
Switch(
opr.Value,
"ADD",
val1 + val2,
"MUL",
val1 * val2,
"SUB",
val1 - val2,
"DIV",
val1 / val2
)
)

Let’s understand the above formula, first With function capture all the input values such as values in txt_valu1, txt_value2 and drp_operation together then Switch function evaluate a single condition against multiple possible matches and execute the corresponding expression and return the result. In the calculation, we have just used the reference of input values captured by With function.

Let’s run the app, and you can see the output. You can see, it substracts the Value 2 from Value 1 and display the output.

 

Also Read..

Add Alternate background color to rows

Sequence function in Power Apps

Filter and search an items in Gallery Power Apps

Text function to format date/time or numbers to text string

 

 

 

Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.