Row Level Security (RLS) in Power BI restricts data access to specific users based on the values in a table, typically a user table. You can implement this by creating a DAX filter that matches the current user's name to a column in your data source, ensuring users only see the rows relevant to them without changing the underlying data model or requiring IT to manually filter data for each person. This is achieved by creating a role in the Power BI Desktop service and defining a DAX filter expression that references the USERNAME() function.

What You'll Need

  • Access to a Power BI Desktop file containing the data model you wish to secure.
  • A dataset within that model that contains a list of users and their associated permissions.A basic understanding of DAX (Data Analysis Expressions) syntax, specifically the USERNAME() function and FILTER() function.
  • A Power BI Pro license (or Premium Per User) for the users who will view the report, as RLS is a feature of the Pro service tier.
  • An understanding of how your data is modeled; RLS applies at the table and column level, so it relies heavily on the relationships between tables.

Step-by-Step

  1. Prepare your data model. Ensure your data model contains a table that links users to the data they should access. For example, you might have a table named 'Users' with columns for 'UserName' and 'Department'. This table should have a one-to-many relationship with the fact table containing your actual sales or transaction data. The relationship is critical because the RLS filter will be applied to the fact table based on the user's department.
  2. Create a DAX measure or calculate a calculated column (optional but common). While RLS is usually defined using a filter on a table, sometimes it's easier to define a measure that returns the current user's department. This measure can then be used in the RLS filter. Create a measure named 'CurrentUserDepartment' using the following DAX formula: SELECTEDVALUE('Users'[Department], "All"). This measure will return the value of the Department column for the row in the Users table that matches the current user.
  3. Open the Power BI Desktop file and navigate to the Model view. Click on the 'Model' icon in the left-hand navigation pane. This view shows the tables and the relationships between them. In this view, you will see the tables you created in the first step and the measure you created in the second step.
  4. Create a new role. In the 'Model' view, look at the 'Manage Roles' button in the ribbon. Click this button to open the 'Manage Roles' dialog box. In the dialog box, click the 'New' button. Give the role a descriptive name, such as 'Sales Department' or 'East Region'. You can create multiple separate roles, each with a different set of permissions.
  5. Define the RLS filter for the role. With the new role selected in the 'Manage Roles' dialog box, look for the 'Table Filters' section. Click on the table that contains the data you want to restrict access to. In our example, this would be the 'Sales' table. This will open a row-level filter expression editor. In this editor, you will define the DAX logic that determines which rows are visible to users assigned to this role.
  6. Write the DAX filter expression. Enter the following DAX expression into the row-level filter editor for the 'Sales' table: AND(RELATED('Users'[Department]) = "Sales", USERPRINCIPALNAME() = USERNAME()). This expression works by checking two things: first, it uses the RELATED function to bring in the Department from the Users table, and then it checks if that Department matches the "Sales" string. The second part of the expression, USERPRINCIPALNAME() = USERNAME(), is a security best practice. It ensures that the user who is currently logged in is indeed the one who owns the row. This prevents a user from impersonating another user.
  7. Save the model and the report. Once you have defined the filter, click the 'OK' button to close the 'Manage Roles' dialog box. Then, save your Power BI Desktop file and publish it to the Power BI service (app.powerbi.com).
  8. Assign users to the role in the Power BI service. In the Power BI service, open your published report. Navigate to the 'Manage Roles' option in the toolbar. This will open the 'Manage Roles' dialog box for the online version of the report. You will see a list of the roles you defined in Power BI Desktop. Click the 'Edit' button next to the role you want to assign users to. In the 'Users' section of the dialog box, type the email addresses of the users you want to give access to this specific role. You can type multiple email addresses, separated by semicolons.
  9. Verify the RLS implementation. To test if your RLS is working correctly, you need to sign out of Power BI and sign in as a user who has been assigned to a specific role. In the Power BI service, click your profile picture in the top-right corner of the screen and select 'Sign out'. Then, sign in with the email address of a user you assigned to a role. Open the report and navigate to the visualizations. The data should now be filtered based on the DAX expression you defined.

Common Mistakes

Not Using USERPRINCIPALNAME() = USERNAME()

One of the most common mistakes is omitting the USERPRINCIPALNAME() = USERNAME() check in the DAX filter expression. Without this check, you are essentially allowing any user in the specified role to impersonate any other user in that same role. This is a significant security risk. The USERPRINCIPALNAME() function returns the full user principal name (UPN) of the user who is currently logged in. The USERNAME() function returns the username, which might be shorter and less specific. By checking that these two values are equal, you ensure that the user is not trying to access data belonging to another user in the same role.

Incorrect DAX Syntax or Table References

Another common issue is incorrect DAX syntax or referencing the wrong table. For example, if you try to use RELATED('Users'[Department]) but the relationship between the 'Sales' table and the 'Users' table is not active, the RELATED function will return an error. You must ensure that the relationship is both correct and active. You can check this by looking at the relationship lines in the Model view. If a relationship is not active, you can right-click on it and select 'Activate' from the context menu. Additionally, ensure that the column names and table names in your DAX expression match exactly the names in your data model.

Forgetting to Publish Changes

It is very common to define the RLS roles in Power BI Desktop and then forget to publish the updated model to the Power BI service. If you do not publish the changes, the users in the Power BI service will still be using the old model without the new security filters. Always remember to publish your changes to the service after you have finished configuring RLS.

RLS is not working with DirectQuery

Row Level Security does not function correctly with DirectQuery unless the source database supports it. If your data source is set to DirectQuery, you must ensure that the underlying database (such as SQL Server or Azure SQL) also supports RLS and that the connection string is configured correctly to pass the security context. If the database does not support RLS, you will need to use Import mode or implement security at the database level.

Tips

Use a 'Users' Table

The most effective way to implement RLS is to use a dedicated 'Users' table in your data model. This table should contain a list of all the users who have access to the report, along with the roles or permissions they should have. This makes it easy to manage user access and to add new users without having to modify the DAX filter expressions.

Use a 'Roles' Table

In addition to a 'Users' table, you can also use a 'Roles' table. This table can contain a list of all the roles you have defined in Power BI, along with the specific rows of data that each role should be able to access. This approach can be more complex to set up, but it can be more scalable and easier to manage for large organizations.

Use the 'All' Option

In the DAX measure you created in Step 2, you used the 'SELECTEDVALUE' function with a default value of 'All'. This is a good practice because it allows you to test the report without RLS. When you are in Power BI Desktop, you can switch the user to 'All' and see all the data. When you publish the report to the service and assign users to roles, the filter will automatically apply.

Use the 'USERNAME()' Function

The USERNAME() function is a powerful tool for RLS. It allows you to dynamically filter data based on the user who is currently logged in. This is useful for scenarios where you have a single role but need to filter the data for each user individually.

Use the 'USERPRINCIPALNAME()' Function

The USERPRINCIPALNAME() function is more secure than the USERNAME() function. It returns the full user principal name (UPN) of the user who is currently logged in. This can help to prevent impersonation attacks.

Use the 'RELATED' Function

The RELATED function is essential for RLS when you have a star schema. It allows you to bring in data from a related table without having to explicitly filter on the foreign key. This can make your DAX expressions cleaner and easier to read.

Troubleshooting

Users are seeing more data than expected

If users are seeing more data than they should, it is likely because the RLS filter is not being applied correctly. Check the following:

  • Verify that the users have been assigned to the correct role in the Power BI service.
  • Check the DAX filter expression in the 'Manage Roles' dialog box to ensure that it is correct.
  • Ensure that the relationship between the tables is active.
  • Check the data model to ensure that the tables and columns are named correctly.
  • Make sure that the report is using the correct data source.

Users are seeing an error when they try to open the report

If users are seeing an error when they try to open the report, it is likely because the RLS filter is causing a problem. Check the following:

  • Check the DAX filter expression to ensure that it is valid.
  • Ensure that the tables and columns referenced in the DAX expression exist in the data model.
  • Check the data type of the columns to ensure that they are compatible.
  • Ensure that the users have the correct permissions to access the report.

The report is not updating with the latest data

If the report is not updating with the latest data, it is likely because the RLS filter is preventing the refresh from happening. Check the following:

  • Ensure that the users have the correct permissions to refresh the data.
  • Check the data source credentials to ensure that they are correct.
  • Ensure that the data source is not blocking the refresh.

I am using DirectQuery and RLS is not working

If you are using DirectQuery and RLS is not working, it is likely because the underlying database does not support RLS. Check the documentation for your data source to see if it supports RLS. If it does not support RLS, you will need to use Import mode or implement security at the database level.

Frequently Asked Questions

How do I handle users who are not in the 'Users' table?

If a user is not in the 'Users' table, they will not be able to access the report at all. If you want to give them access, you must add them to the 'Users' table and assign them to a role. If you want them to see all the data, you can create a role that has no filter, but this is generally not recommended due to security concerns.

Can I use RLS across multiple tables?

Yes, you can use RLS across multiple tables. You simply need to define the filter for each table that you want to secure. However, it is important to ensure that the filters are consistent across all tables. For example, if you filter the 'Sales' table by Department, you should also filter the 'Products' table by Department.

Does RLS work with Power BI Report Server?

Yes, RLS works with Power BI Report Server. The implementation is very similar to the implementation in the Power BI service. You define the roles in Power BI Desktop and then assign users to the roles in the Report Server portal.

Can I use RLS with a live connection?

No, you cannot use RLS with a live connection. RLS requires the use of Import mode. If you are using a live connection, you will not be able to use RLS.

How do I test RLS in Power BI Desktop?

You can test RLS in Power BI Desktop by using the 'Edit as User' feature. Click on the 'Model' icon in the left-hand navigation pane and then click on the 'Manage Roles' button. Select the role you want to test and then click on the 'Edit' button. This will open the 'Edit as User' dialog box. In this dialog box, you can enter the email address of a user who has been assigned to that role. Power BI Desktop will then filter the data based on the RLS filter expression for that user.

What is the difference between USERNAME() and USERPRINCIPALNAME()?

The USERNAME() function returns the username, which might be shorter and less specific. The USERPRINCIPALNAME() function returns the full user principal name (UPN) of the user who is currently logged in. The UPN is typically in the format of an email address. The USERPRINCIPALNAME() function is more secure because it is more difficult for a user to impersonate another user.