Power Apps Tips & Tricks
Using Double Quotes
When writing text in Power Apps controls, you may need to add a double quote as part of your text. Power Apps uses the Power Fx language. It would be best to convert the " to &Char(34)
to encode them into the correct character.
Validate a whole number
In Power Apps, we can configure a text control to accept a Number value. However, users can still input decimal values. To enforce the input of whole numbers, we can implement some validation.
If(Value(TextInput1.Text) <> RoundDown(Value(TextInput1.Text),0),false, true)
Add a line break
If you're creating some text and need to add a line break so the text appears on another line, you can use char(10)
where you want the line break to occur.
Concatenate(
"Name: ",
User().FullName,
Char(10),
User().Email
)
Search O365 Users in a Combobox
We can search all users in the tenant by adding the Office365Users connector to our Power App and then hooking it up in a ComboBox control.
- Add Office365Users data connector.
- Add a ComboBox control.
- Enable searching in the ComboBox control.
- In the Items property of the ComboBox control, add the following formula.
Office365Users.SearchUserV2({searchTerm:Self.SearchText, top: 20})
When the user searches in the Combobox, it will search all the users in Office 365 (using their Display Name by default).
We can also display additional fields in the ComboBox control, helping to find the correct user.
- Select the Fields property, and click Edit.
- Change the Layout to Double.
- Select the Primary Text and Secondary Text fields that you want to show. You can also change the SearchField here.
Power Apps TimeZoneOffset Function
We can use the TimeZoneOffset function in Power Apps to adjust for the current time zone from UTC time. We would typically want to save our Dates as UTC time in any database we're using, and convert those dates to the local time zone when viewing them in our app.
This example takes the date value named StartTime, which is a UTC time, and subtracts the TimeZoneOffset value in minutes to get the local time.
DateAdd( StartTime, −TimeZoneOffset( StartTime ), TimeUnit.Minutes )
Power Apps Host Property
The Power App Host property provides helpful information about the application, including the BrowserUserAgent, OSType, SessionID, and TenantID.
In the formula bar, type Host followed by a dot and the property you wish to access.
Property | Description |
---|---|
Host.BrowserUserAgent | Tells you what browser the user uses to access the app. |
Host.OSType | Tells you what Operating System the user uses to access the app. |
Host.SessionID | A unique Session ID for the current user. Useful for troubleshooting. |
Host.TenantID | The Tenant ID for the current user. Useful for troubleshooting. |
Create an incrementing number
This example shows how to create an incrementing number in a column of our Power Apps collection. Helpful in displaying sequential numbers in a list of created items.
In this example, we have a collection called colMyCollection. We will use the ForAll Function to loop through the number of records created by our Sequence Function. The Sequence Function counts the number of rows in our collection. If there are 7 rows, the Sequence will be 7.
Next, we'll have a Patch, where we patch the items in our collection. It's vital to sort the items in the order you want them numbered.
Finally, we'll use the FirstN and Last functions to get the record in the collection that matches the current Sequence number in our ForAll. Then, we update the column we want to use for our number, in this example, MyIncrementedNumberColumn, setting it to the Value of the Sequence record.
ForAll(
Sequence(CountRows(colMyCollection)),
Patch(
Sort(colMyCollection, SortColumn, SortOrder.Ascending),
Last(
FirstN(
colMyCollection,
Value
)
),
{
MyIncrementedNumberColumn: Value
}
)
)
Validate an email address.
A common request when building Power Apps is to validate the email address of a text box. This can be done easily using the IsMatch function. The function also performs various validations using regular expressions. The Match.Email property can be used to check if it’s a valid email automatically.
If(
IsMatch(
txtValidationEmail.Text,
Match.Email
),
true // The email address is valid
false // The email address is not valid
)
Use the And/Or Function for readability
When creating If conditions, we have the option to do something like this:
If(IsBlank(txt_Name.Value) And IsBlank(txt_Description.Value) And IsBlank(txt_Customer.Value) And IsBlank(txt_Comments.Value) And IsBlank(txt_JobTitle.Value),
true,
false
)
The problem with this approach is that it becomes tough to read, especially when several conditions are added.
Instead, use the And/Or Functions to make it cleaner. Each condition gets its own line.
If(
And (
IsBlank(txt_Name.Value),
IsBlank(txt_Description.Value),
IsBlank(txt_Customer.Value),
IsBlank(txt_Comments.Value),
IsBlank(txt_JobTitle.Value)
),
Notify("Name is required", NotificationType.Warning);
UpdateContext({locContinue: false}),
UpdateContext({locContinue: true})
);
Format a Number
If we have a number value in our Power App that we want to format to include a comma and decimal, we can use this.
Text(
Value("2645.40"),
"[$-en-US]#,##0.00"
)
The above example will output the value: 2,645.40
.
Determine if a user is in a Security Group
Sometimes we want our Power App to check a Security Group or Microsoft 365 Group to see if a user is in that group.
We need to access Azure Entra and locate the desired group. On the overview page, it will show the group's Object ID. We'll need this value in our formula.
- Edit your Power App.
- Open App.Formulas and enter the formula below. Update the fxMyGroupId with your group ID and update the formula name to include your group name so it's easier to reference.
fxMyGroupId = "{YOUR GROUP ID GUID}";
fxIsCurrentUserInMyGroup = If(
IsEmpty(
Filter(
Office365Groups.ListGroupMembers(fxMyGroupId).value,
Lower(mail) = Lower(User().Email)
)
),
false,
true
);
- You can now reference
fxIsCurrentUserInMyGroup
from anywhere in your app.
Member discussion