Yes, you can filter a Power Apps gallery to show only the records belonging to the current user. This is done by adding a Filter function to the Items property of the gallery, comparing the creator's name (or a user ID column) with the User() function. You must also ensure your data source has a column that stores the user information to compare against.

Filtering a gallery by the current user is a very common requirement in business applications. It ensures that a user only sees data they are authorized to view, such as their own tasks, submissions, or inventory items. This guide explains exactly how to set it up, from ensuring your data is ready to writing the correct formula and handling potential errors.

What You'll Need

  • A Power Apps environment with a data source (e.g., SharePoint list, Dataverse table, or SQL database).
  • A data source that has a column storing the user who created or owns the record. Common column types are 'Created By' (SharePoint), 'Owner' (Dataverse), or a custom 'CreatedUser' text or email column.
  • A screen with a gallery control on it.
  • A basic understanding of the Power Apps formula syntax.

Step-by-Step

1. Prepare Your Data Source

The first and most critical step is to verify that your data source contains the information needed to identify the user. The Filter function needs a column to compare against the 'User()' function.

In many systems, like SharePoint, the 'Created By' column is a lookup to the User Information list. This means the value stored in the column is not the user's name or email, but a complex internal ID. Power Apps can compare this ID to the 'User()' function directly, but it is often more reliable to use a text-based column if available.

2. Add a Gallery to the Screen

Add a gallery control to your screen. You can do this by selecting the screen in the tree view on the left, going to the 'Insert' tab on the top ribbon, and clicking 'Gallery'. Choose a gallery style that fits your design needs.

3. Set the Gallery Items Property

Select the gallery control on the canvas. In the formula bar at the top, look for the 'Items' property. Delete any default text that might be there (like 'Gallery1.Items') and type the following formula:

Filter(MyDataSource, CreatedBy = User())

Note: Replace 'MyDataSource' with the actual name of your data source (e.g., 'SharePointList1'). Replace 'CreatedBy' with the actual name of the user column in your data source.

This formula instructs Power Apps to get all items from 'MyDataSource' where the value in the 'CreatedBy' column matches the current user. The 'User()' function returns the display name and email of the person currently using the app.

4. Handle the 'User()' Function for Dataverse

If your data source is Microsoft Dataverse, the 'User()' function works slightly differently. It returns a complex object containing the user's details. To compare it to a column, you typically need to reference the 'Email' or 'Name' property of the returned object.

Here is how you would write the formula for a Dataverse table:

Filter(MyDataverseTable, Owner.Email = User().Email)

Note: Replace 'MyDataverseTable' with your table name and 'Owner' with the name of the column that stores the user relationship.

This formula ensures that the owner of the record matches the email address of the currently logged-in user.

5. Test the Gallery

Save and publish your app, then test it. Sign in with a user account that has records in the data source. You should see only their records in the gallery.

Now, sign out and sign in with a different user account. The gallery should be empty or show only records created by that new user.

Common Mistakes

  • Using the wrong user column: Some data sources have multiple user-related columns, such as 'Modified By', 'Created By', or 'Owner'. Make sure you are filtering on the column that represents the person who should be seeing the data. For example, 'Modified By' might be less relevant if you want to show records created by the user, whereas 'Owner' might be more appropriate for asset management.
  • Ignoring the data type: As mentioned earlier, comparing a complex ID (like SharePoint's 'Created By') can sometimes be tricky. If the gallery appears empty, check the data types of the columns. It is often safer and more predictable to compare a text-based column (like an email address) to the 'User()' function.
  • Not handling the case sensitivity: Text comparisons are often case-sensitive. While 'Email' and 'email' might look the same to a human, Power Apps might not consider them equal. However, the 'User()' function and most system columns return consistent casing, so this is less of an issue in this specific scenario.

Tips

  • Use a specific email column: If your data source allows it, create a custom column that stores the user's email address. This makes the filtering formula simpler and less prone to errors caused by different data formats or lookup complexities. Your formula would then be: Filter(MyDataSource, EmailColumn = User().Email).
  • Preload the data: If your data source is large, consider preloading the data or using a collection to cache the user's records. This can improve performance, especially on mobile devices.
  • Use the 'If' function for conditional filtering: You can combine the 'Filter' function with the 'If' function to create more complex rules. For example, you could show all records if the user is an administrator, and only their own records otherwise. If(User().Email = "admin@example.com", MyDataSource, Filter(MyDataSource, CreatedBy = User())).

Troubleshooting

Gallery is Empty

If your gallery is empty, it means no records match the filter criteria. This can happen for several reasons:

  • Column name is incorrect: Double-check the exact name of the column in your data source. A typo or a missing space can cause the formula to fail silently or return no results.
  • Data source is not connected: Ensure that your app is connected to the correct data source and that there are no connectivity issues. You can test this by looking at the 'Connectors' pane in Power Apps Studio.
  • User context is incorrect: If you are testing the app in Power Apps Studio, the 'User()' function might return the context of the creator of the app, not the user you are trying to simulate. To test with a different user, use the 'Test' feature in Power Apps Studio to simulate a specific user's login.
  • Data type mismatch: If you are comparing a text column to a complex object, you might need to use the 'Value()' function to extract a specific property.

Gallery Shows All Records

If the gallery shows all records instead of just the current user's, check the formula again. Look for typos, missing parentheses, or incorrect column names. Also, ensure that the data source is correctly referenced. If you are using a collection, make sure the collection is updated with the latest data before you try to filter it.

Error: 'User()' is not a valid function

This error usually indicates that you are using the 'User()' function outside of a formula within the Power Apps environment. The 'User()' function is a specific Power Apps function that returns the current user's profile information. It cannot be used in other contexts, such as in a SharePoint column formula or a Power Automate flow.

Frequently Asked Questions

Can I filter by the name of the user instead of their email?

Yes, you can. The 'User()' function returns a complex object that includes the user's name. You can access it using User().FullName. However, it is generally recommended to use the email address for filtering, as names are not always unique, whereas email addresses typically are.

What if my data source is a SharePoint list with a 'Created By' column?

SharePoint's 'Created By' column stores a lookup to the User Information list, which is a complex ID. Power Apps can compare this ID to the 'User()' function directly. The formula would look like this: Filter(SharePointList, 'Created By' = User()).

How do I handle this for Dataverse tables?

As mentioned in the steps above, Dataverse requires you to access the email property of the user object. The formula is Filter(MyDataverseTable, Owner.Email = User().Email).

Can I use this formula in a Power Automate flow?

No, the 'User()' function is specific to Power Apps. You would need to use the 'Get User Profile' action or a similar action in Power Automate to get the current user's information and then use that value in your flow's logic.

Is it possible to filter by multiple users?

Yes, you can. You can use the 'In' function to check if a value is in a list of users. For example: Filter(MyDataSource, User().Email In ["user1@example.com", "user2@example.com"]). This would return all records created by either user1 or user2.

What if I want to show records created by the current user OR records created by a specific group?

You can use the 'Or' function to combine two filter conditions. For example: Filter(MyDataSource, CreatedBy = User() Or CreatedBy = "admin@example.com"). This would return all records created by the current user or by the specific administrator.