Power Query - Filter Data by logged in User

Power Query - Filter Data by logged in User

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


Antony Klipper

Data Engineer at Yonderland (AS Adventure / Juttu / Bever / Cotswold Outdoor / Snow+Rock / Runners Need)

6 个月

Does this work in the power bi service?

回复
Joseph Delli Gatti

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?

Mike Asplin

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.

Rory Neary

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?

Mary Fealty

Northern Ireland based Power BI specialist, who thrives on helping her customers with their data journey. ?? #PowerBI

7 年

Excellent! Thanks

要查看或添加评论,请登录

Wyn Hopkins的更多文章

  • The Best Dependent Drop Down Technique

    The Best Dependent Drop Down Technique

    Driven by a gauntlet laid down by friend and fellow MVP Mark Proctor on our Unpivot podcast recently I revisited my…

    1 条评论
  • Should you use Measures for Conditional Formatting?

    Should you use Measures for Conditional Formatting?

    Streamline Your Power BI Conditional Formatting with Measures I've always found the built-in conditional formatting in…

    6 条评论
  • Consolidate and Hyperlink to Excel files on SharePoint

    Consolidate and Hyperlink to Excel files on SharePoint

    In this video, we're diving into the world of Excel and Power BI, focusing on creating hyperlinks for easy referencing…

    4 条评论
  • The greatest multi-level Excel drop-down list ever!

    The greatest multi-level Excel drop-down list ever!

    One-Off dependent drop down lists in data validation are relatively straightforward: here's a technique using XLOOKUP…

    5 条评论
  • Default Your Slicer to the Current Month in Power BI

    Default Your Slicer to the Current Month in Power BI

    First Published November 2022 on our YouTube Channel Join 65,000+ subscribers to stay up to date with new videos…

    4 条评论
  • Power BI Licensing Explained

    Power BI Licensing Explained

    What are the differences between: Power BI Free (soon to be renamed Fabric Free) Pro: $10 USD pp/pm PPU (Premium Per…

    2 条评论
  • Power BI May 2023 Release

    Power BI May 2023 Release

    The May version of Power BI desktop is out This is a very quick post highlighting my 2 favourite picks: 1. Azure Maps…

    5 条评论
  • Simpler DAX ? = Power Query

    Simpler DAX ? = Power Query

    If you are struggling to write a complicated DAX formula then the answer may be to step back and do a little Power…

    2 条评论
  • Power BI Explained

    Power BI Explained

    A simple explanation of Power BI I hope you find this useful. Power BI continues to go from strength to strength and…

  • SUMX explained.

    SUMX explained.

    SUMX is a DAX function that can be used in an Excel Data Model (aka Power Pivot) to create a temporary column…

    6 条评论

社区洞察

其他会员也浏览了