Power Query - Filter Data by logged in User
Wyn Hopkins
Solving data challenges for companies and people. Power BI | Excel | Fabric | Solver Budgeting & Consolidation. Part of the Amazing team at Access Analytic Microsoft MVP ??6 Million+ YouTube views
Warning... This article is a bit techy....
Probably quite a narrow audience for this post but it's something that cropped up at a training session and was another interesting option of what Power Query can do. Plus I can see how this could be quite attractive in simple scenarios where you'd like to automatically refresh and filter the source data based on the logged in user.
I can't remember where I originally saw this solution but I also googled it and found it here https://querypower.com/2017/04/03/4-ways-to-get-username-in-power-query/
I've not used this approach in real life (let me know if you have) so just beware of reliability....
The screenshot shows I have a blue table of data (Table1) and after applying a filter for the logged in user I end up with the green table (Filtered Output).
Here's the code to create the Parameter for Current User
let
Users = Folder.Contents ("C:/Users"),
#"Filtered Rows" = Table.SelectRows(Users, let latest = List.Max(Users[Date accessed]) in each [Date accessed] = latest),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Name"}),
List = #"Removed Other Columns"[Name],
#"Current User" = List{0}
in
#"Current User"
Here's the code for the Filtered Output table
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"User", type text}, {"Department", type text}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([User] = CurrentUser))
in
#"Filtered Rows"
Until Excel has the option to lock down the query window and prevent people editing the query (vote here) this isn't a secure way of preventing people from seeing other data but could be an efficiency / file size thing.
Let me know if anyone has used this in real life and come across any issues.
Wyn
EXCEL MVP
Data Engineer at Yonderland (AS Adventure / Juttu / Bever / Cotswold Outdoor / Snow+Rock / Runners Need)
6 个月Does this work in the power bi service?
Data Reporting Analyst Associate at LifeMap
3 年Bummer. This doesn't work for me. And the info provided in the resulting table doesn't help narrow it down. Something in my system causes "Public" or "Default User" to be the most recent user sometimes. I think I would need some additional command to access or modify the current user in order for it to always be on top of the sorted table. Any ideas?
Operating Partner at Limerston Capital
5 年That is just what i was looking for. I use several different machines in different location to work on my pbix file . Files are all on Sharepoint so have exact same path apart from the user bit C:Users\Mike or similar. Using your code i can pull out the bit after Users\ and build the full path. Perfect.
Freelance Power Platform Trainer | MVP | MCT | Microsoft Power Up Program Content Creator and Presenter | Solution Architect | Accountant
7 年Yes it is quite niche, but there is a place for everything. As you appear to straddle both excel and power bi what are your thoughts on our ‘global’ reliance on excel. I.e. the thousands of businesses running with excel silos they can’t manage without?
Northern Ireland based Power BI specialist, who thrives on helping her customers with their data journey. ?? #PowerBI
7 年Excellent! Thanks