The trick to getting the DAX function Username() to return the user’s User Principal Name (UPN) in Power BI is to configure Row-Level Security (RLS) on the dataset within the Power BI service. If you haven’t enabled RLS, you may see some odd results.
Last week I posted a video about using Row-Level security in Power BI, and in the comments of the video I received a question regarding how to use Username() within a DAX expression.
Brian indicated he couldn’t find any examples of how to dynamically filter based on a username in DAX, even though he was told that it could be done.
To validate that Username() does indeed work, I added a DAX measure to my model:
User = username()
Within Power BI Desktop, this returned my Windows account — GUYINACUBE\asaxton.
However, when I published the Power BI Desktop model to the Power BI service, I saw a GUID returned as the value.
This is because in order for Username() to return a value, RLS needs to be enabled on the model. So, I created a role (any role will work) on the model for another user. After that was done, Username() returned the UPN of the user using the report. Note that you may need to refresh the report page to see the result.
I can then switch to a different user, and see their UPN.
Now you can go and make use of the Username() function within DAX calculations to further explore data.
Here is the RLS video that I referenced above. It doesn’t specifically show the use of Username(), but it is a good overview of RLS in general.