Introduction

In this article, I am going to explain some important and useful formula to develop an app in PowerApp.

Formulas
Fetch Manager properties from user profiles

To fetch the user’s manager properties, first, we need to connect to the Office365Users to PowerApp.

Syntaxes

Mail - Office365Users.Manager(User().Email).Mail

(Where the User().Email is current login user’s email. You can use any other user’s email to get the one’s respected Manager)

Department - Office365Users.MyProfile().Department

Set the visibility of control based on users

Let’s say as an example, I want to show a setting button to only admins, not to the remaining users.

To achieve this, create a SharePoint list for admins, then set a condition on the visibility of a button to check if the user is available in the list to show the button.

Look at the below screenshot to understand better,

Formulas For App Development In Powerapps
Formulas For App Development In Powerapps
Formulas For App Development In Powerapps
Syntax

Visible - If(User().FullName in Admins.FullNameText, true, false)

Set the current user and user’s manager by default on screen load

onVisible of the screen,

For Current user,

  1. UpdateContext({
  2. CurrentUser: {
  3. '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
  4. Claims: "i:0#.f|membership|" & Lower(User().Email),
  5. Department: "",
  6. DisplayName: User().Email,
  7. Email: User().Email,
  8. JobTitle: ".",
  9. Picture: "."
  10. }
  11. });

For Manager,

UpdateContext({managerProfile - Office365Users.Manager(Office365Users.MyProfile().Id)})

On button click post the manager column as below,

onSelect

  1. Patch('MyList', Defaults('MyList'), {
  2. Manager: {
  3. '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
  4. Claims: "i:0#.f|membership|" & Lower(managerProfile.Mail),
  5. Department: managerProfile.Department,
  6. DisplayName: managerProfile.DisplayName,
  7. Email: managerProfile.Mail,
  8. JobTitle: managerProfile.JobTitle,
  9. Picture: User().Image
  10. },
  11. });

Update a particular record using Patch

Property - OnSelect,

Syntax

Patch('MyList', {ID:IDNum}, {Title: "My new title"})

Explanation

Enable/Disable Button

To enable/disable a button based on selection:

Property - Display mode,

Syntax

If(IsBlank('MyList Box'.Selected.Value),DisplayMode.View,DisplayMode.Edit)

Explanation

Display some name on screen load

Whenever you are navigated from one screen to another screen based on selection of an item, the below syntax will be used to display the selected item’s property.

Syntax

someGallery.Selected.DisplayName (These are just to get some idea to implement in your applications)

Explanation

Screen Navigation

Case - When you wish to navigate to a particular named screen from the current screen by clicking a button/Icon, put the below syntax on button’s onSelect property.

Property - onSelect,

Syntax

Navigate(SomeScreenName, ScreenTransition.None)

Explanation

Post data to SharePoint list

Case - When you want to post input form’s data to a SharePoint list on a particular button click.

Button’s Property - OnSelect,

Syntax

SubmitForm(AddedEditForm);NewForm(AddedEditForm)

Explanation

Delete a selected record from List

Case - When you want to delete a selected item from a list box by clicking on a button.

Button’s Property - OnSelect,

Syntax

Remove(MyList, First(Filter(MyList,EmpName=ListBox.Selected.EmpName)))

Explanation

Delete a particular record from the list by using ID

Button’s Property - OnSelect,

Syntax

Remove(MyList, First(Filter(MyList,ID=Gallery.Selected.ID)))

Explanation

Delete Bulk Records from List

Case - When you want to perform deletion on bulk records by clicking a button.

Button’s Property - OnSelect,

Syntax

ForAll(CollectionForRemove,Remove(MyList, First(Filter(MyList,ID= CollectionForRemove [@ID]))));

Explanation
Show some filtered Items in a gallery

Case - When you want to display particular categorized items in a gallery.

Gallery’s Property - Items,

Syntax

Filter(MyList,EmployeeName.DisplayName =User().DisplayName) (Where I’m filtering current user’s items)

Explanation

Show only unique records/Remove duplicate

Case - When you wish to show only unique items on a Gallery/ListBox

Gallery’s Property - Items,

Syntax

Distinct(Filter(MyList,EmployeeName.DisplayName =User().DisplayName),EmployeeName.DisplayName)

Explanation

Display Current Date and Time

Case - When you want to display Current Date and Time on a label/text field

Label’s Property - Text,

Syntax

Now()
(Ex: DataCardAdminUserValue.Selected.DisplayName & "-" &Now())

Show the only Year,

Text(Now(), "[$-en-US]yyyy")

Set selected item Background color

Case - When you want to highlight selected item from a gallery

Gallery’s Property -TemplateFill,

Syntax

If(Year=SomeGallary.Selected.Year,
RGBA(255, 255, 255, 1),
RGBA(0,0,0,0)
)

Explanation

Bulk records update

Case - When you want to perform updating on bulk records by clicking a button

Button’s Property -OnSelect,

Syntax

ForAll(CheckedItems,Patch(ChecklistItemsSource,LookUp(ChecklistItemsSource, Id = CheckedItems[@Id]),{Status:"Done"}))

Explanation

Update a particular record

Case - When you want to perform update on a particular record by clicking a button

Button’s Property - OnSelect,

Syntax

Patch(MyList, {ID:SelectedId}, {Status: "Submitted"});

Explanation

This is just similar to the above case, except, where we are updating the item with user input ‘Submitted’ to Status column by passing a particular ID.

Collection’s indexed values

Case - When you want to get a particular item’s property from the collection using an index.

Property - any text/label,

Syntax

Last(FirstN(MyCollection, IndexValue)).ColumnName
(Ex: Last(FirstN(SomeCollection, 1)).ID
Last(FirstN(MyCollectionForEmployeeInfo, 3)).Employee.DisplayName
)

Explanation

Set items to a collection

Case - When you want to create a collection/Array with filtered items on a button click

Button’s Property:OnSelect/onChange,

Syntax

ClearCollect(UserDefinedCollectionName,Filter(‘ListName’,EmpName=Gallery.Selected.EmpName))
(Ex: ClearCollect(CollectionForselectedQuarter,Filter('Key Initiatives Quarters', YearsFromHeaders_3.Selected.Result = Year&&hrlw=AllAdmins.Selected.Result&&Quarter=Value(Dropdown2.Selected.Value))))

Explanation

Condition in MS flow

Case - When you want to set condition with array length

Edit in advance mode

Syntax

For:Array length greater than 0,

@greater(length(body('Filter_array_1')), 0)

For:Array length is equals to 0,

@equals(length(body('Filter_array_2')), 0)

Expand/Collapse

Case - When you want to hide and show two icons, set variable and use as following:

onselect
UpdateContext({ExpandColapse: !ExpandColapse})

visible
ExpandColapse

Show a particular filtered item from a Sharepoint list

Case - Filter from entire list on page load

Property - Text,

Syntax

IsBlank(LookUp(MyList,EmployeeName=CurrentUserName&&Year=RunningYear,Employee.DisplayName))

Explanation
Where it will filter and return the Employee Name from the SharePoint list where it has EmployeeName=CurrentUserName and Year=RunningYear, and the CurrentUserName and Running are the variables which are defined on the screen’s OnVisible under Advanced.
Formulas For App Development In Powerapps
Set Variable on click

Case: Set variable on an action

OnSelect/onChange

Set(ForUnique,"New Initiative"& "-" &Now());

Explanation

‘ForUnique’ is a user-defined variable name setting with a string and current datetime
Reset form on Submit button using while using the patch

Case - When you want to reset form while using submission with patch methods

Property1: OnSelect,

Syntax

UpdateContext({ResetVar: true});UpdateContext({ResetVar: false})
Property2:Default,
ResetVar

Conclusion

This article will help us to develop any kind of customized applications with the help of the above formula. I hope we can achieve almost any kind of issues or required things with the help of those formulas. This will be helpful to those who want to start with PowerApp. Expect many more things related to Microsoft Flows in my coming articles.

Thank you.