How can we show sales data only for the logged-in user using dynamic row-level security (RLS) with the USERNAME function?
Sample Data
UserEmail
Region
SalesAmount
OrderDate
alice@example.com
North
1000
2024-01-15
bob@example.com
South
1500
2024-02-20
alice@example.com
East
700
2024-03-05
carol@example.com
West
1200
2024-01-30
bob@example.com
North
900
2024-04-10
Dashboard Components
KPI Card:Total Sales for Current User Formula: Total Sales = SUM(Sales[SalesAmount]) Shows the sum of sales filtered by the logged-in user's email.
Table:Sales Details Columns: UserEmail, Region, SalesAmount, OrderDate Shows only rows where UserEmail matches the logged-in user due to RLS.
Bar Chart:Sales by Region for Current User X-axis: Region Y-axis: Sum of SalesAmount Filtered dynamically by RLS to show only current user's data.
RLS Role Filter Expression
Sales[UserEmail] = LOWER(USERNAME())
This expression filters the Sales table rows to only those where the UserEmail matches the logged-in user's email.
Dashboard Layout
+----------------------+----------------------+
| KPI Card | Bar Chart |
| Total Sales Amount | Sales by Region |
+----------------------+----------------------+
| Sales Details Table |
+-------------------------------------------------+
Interactivity
The dynamic RLS filter automatically applies based on the logged-in user's email. No manual slicers are needed to filter data by user.
All visuals (KPI card, bar chart, and table) update automatically to show only data for the current user.
Self Check
If you log in as bob@example.com, which rows appear in the Sales Details table?
Answer: Only rows where UserEmail is bob@example.com (rows 2 and 5 from the sample data).
All visuals update to show sales totals and regions only for Bob's data.
Key Result
Dashboard showing sales data filtered dynamically by logged-in user using USERNAME-based RLS.
Practice
(1/5)
1. What is the main purpose of using USERNAME() in Dynamic Row-Level Security (RLS) in Power BI?
easy
A. To change the report theme dynamically
B. To filter data based on the logged-in user's identity
C. To calculate the total sales amount
D. To create new columns in the data model
Solution
Step 1: Understand USERNAME() function
The USERNAME() function returns the current logged-in user's email or username.
Step 2: Apply USERNAME() in Dynamic RLS
Dynamic RLS uses USERNAME() to filter data so each user sees only their allowed rows.
Final Answer:
To filter data based on the logged-in user's identity -> Option B
Quick Check:
USERNAME() filters data by user [OK]
Hint: USERNAME() filters data by current user identity [OK]
Common Mistakes:
Thinking USERNAME() creates new columns
Using USERNAME() to calculate totals
Confusing USERNAME() with theme settings
2. Which of the following is the correct syntax to create a filter rule for Dynamic RLS using USERNAME() in Power BI?
easy
A. [UserEmail] = USERNAME()
B. FILTER([UserEmail], USERNAME())
C. USERNAME() = [UserEmail]
D. CALCULATE(USERNAME(), [UserEmail])
Solution
Step 1: Understand filter rule syntax
In Dynamic RLS, the filter rule compares a column to the current user using =.
Step 2: Identify correct syntax
The correct syntax is [UserEmail] = USERNAME() to filter rows matching the logged-in user.
Final Answer:
[UserEmail] = USERNAME() -> Option A
Quick Check:
Filter rule uses column = USERNAME() [OK]
Hint: Use column = USERNAME() for filter rules [OK]
Common Mistakes:
Using USERNAME() on left side of comparison
Using FILTER() function incorrectly
Trying to CALCULATE USERNAME()
3. Given a table with a column UserEmail and a Dynamic RLS filter rule [UserEmail] = USERNAME(), what data will a user with email alice@example.com see?
medium
A. Only rows where UserEmail is not alice@example.com
B. All rows regardless of UserEmail
C. Only rows where UserEmail is alice@example.com
D. No rows at all
Solution
Step 1: Understand the filter rule effect
The rule [UserEmail] = USERNAME() filters rows to only those matching the logged-in user's email.
Step 2: Apply to user email
For user alice@example.com, only rows with UserEmail = alice@example.com are visible.
Final Answer:
Only rows where UserEmail is alice@example.com -> Option C
Quick Check:
Filter shows rows matching USERNAME() [OK]
Hint: User sees only rows matching their email [OK]
Common Mistakes:
Assuming user sees all rows
Thinking user sees rows with different emails
Believing no rows are shown
4. You wrote this RLS filter rule: FILTER(Users, Users[Email] = USERNAME()). Why does it cause an error in Power BI?
medium
A. FILTER cannot be used directly as a filter rule; it expects a boolean expression
B. USERNAME() is not allowed in RLS rules
C. The column name should be lowercase
D. FILTER requires two arguments
Solution
Step 1: Understand RLS filter rule requirements
RLS filter rules expect a boolean expression, not a table expression like FILTER().
Step 2: Identify the error cause
Using FILTER() returns a table, causing a syntax error in RLS rules.
Final Answer:
FILTER cannot be used directly as a filter rule; it expects a boolean expression -> Option A
Quick Check:
RLS rules need boolean expressions [OK]
Hint: Use boolean expressions, not FILTER(), in RLS rules [OK]
Common Mistakes:
Using FILTER() instead of boolean condition
Thinking USERNAME() is disallowed
Assuming column case matters
Believing FILTER needs two arguments
5. You want to create Dynamic RLS so that managers see data for their team members, and team members see only their own data. The table has columns UserEmail and ManagerEmail. Which DAX filter rule correctly implements this?
hard
A. USERNAME() = [UserEmail] && USERNAME() = [ManagerEmail]
B. [UserEmail] = USERNAME() && [ManagerEmail] = USERNAME()
C. FILTER(Users, [UserEmail] = USERNAME() || [ManagerEmail] = USERNAME())
D. [UserEmail] = USERNAME() || [ManagerEmail] = USERNAME()
Solution
Step 1: Understand the requirement
Managers should see rows where they are the manager, and users see their own rows.
Step 2: Choose correct logical operator
Use OR (||) to allow either condition: user is the row owner or user is the manager.