Get username function in Power Query
Get username function in Power Query with few lines of M code. For the moment, there is no native way to get the Windows username in M language. The username might be useful to filter a table or to get a file path in the ‘Documents’ folder.
The following post describes 4 ways to get the username in Power Query :
https://querypower.com/2017/04/03/4-ways-to-get-username-in-power-query/
Unfortunately, the simplest method (n°2) is not reliable because of NTFS delay as explained in the post.
So I developped a function using the same concept with some improvements.
How to get username function in Power Query
- In Power Query, add a blank query
- Then open the advanced editor and paste the code down below
- Rename the function
As the function requires no parameter, it is straight forward to use it
() =>
let
Source = Folder.Contents("C:\Users\"),
#"Expanded Attributes" = Table.ExpandRecordColumn(Source, "Attributes", {"Hidden", "Directory", "ChangeTime"}, {"Hidden", "Directory", "ChangeTime"}),
#"Filtered Directories not hidden" = Table.SelectRows(#"Expanded Attributes", each ([Directory] = true) and ([Hidden] = false)),
#"Removed Errors" = Table.RemoveRowsWithErrors(#"Filtered Directories not hidden", {"ChangeTime"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Errors", each ([Name] <> "Public")),
#"Username" = #"Filtered Rows"{0}[Name]
in
#"Username"
Limitation : it works well in the desktop but not sure it will work so good in the service depending on the way you refresh the data. For example, if you refresh data with a gateway, you might get the username of the Windows session which launched the refresh.
You may also have a look to Power BI assistant : https://thebipower.fr/index.php/power-bi-assistant/
Custom analyser
With ‘Custom analyser’ feature, Power BI Sidetools users can use external tools built by the…
DAX debugger in Power BI Sidetools
DAX debugger is meant to help Power BI report developers displaying sample data from a…
Manage your external tools easily with Power BI Sidetools
Since the Power BI team allowed to launch external tools from the Power BI desktop…
Easily export to Excel with Power BI Sidetools
While working in Power BI desktop, we often need to export to Excel some data…
Powerful text and file search in Visual Studio Code
As Power BI Sidetools creates files from your report, you are able to search through…
Compare DAX and M formulas in Visual Studio Code
Compare DAX and M formulas in Visual Studio Code is really easy with the help…
That worked like a charm! Thanks!
Love this function… Was trying to figure out how to just embed it into a single query, but not gotten very far.
I have been trying to make a “system information” query so that when someone runs a Power Query in Excel and an issue or question comes up about what is going on I can say go look at the “System Info Table”. Unfortunately all I have last refresh, with TZ, User Name,
Love to get file name/path/excel info
Especially would love “did a complete refresh of all queries occur without error”. Sometimes a results look good, but when you look at the list of queries there is an error in 1 or more.
Hello Alan,
Thank you for the feedback.
Sorry, I have no idea how to monitor the queries refresh in Excel. May be VBA ? May be use Reza Rad solution :
https://www.youtube.com/watch?v=iCumyrP3HSE
Regards
Unfortunately this gives me a user, but not the current one.
It seems to be the first one in the Users folder.
Is there maybe something I’m doing wrong (copied the code 1:1 in advanced editor in Excel 2016) or another solution?
Thank you!
Hello Andreas,
The function has not been tested in Excel. In Excel, I’m used to code in VBA :
UserName = Environ(“username”)
Hi there, the username function works when I invoke it but how do I reference it in the file path. The function is called UserName_fx and I receive an error when I put it in the file path outlined below. I don’t know M so need assistance. Thanks
The current path for my data source in the advanced editor:
Source = Csv.Document(File.Contents(“C:\Users\UserName_fx \PowerBI Files\Customer.csv”),[Delimiter=”,”, Columns=45, Encoding=65001, QuoteStyle=QuoteStyle.None]),
Hello Maureen,
You might try this :
Source = Csv.Document(File.Contents(“C:\Users\" & UserName_fx & "\PowerBI Files\Customer.csv”),[Delimiter=”,”, Columns=45, Encoding=65001, QuoteStyle=QuoteStyle.None]),
because it’s a function, you can do it like this:
Source = Csv.Document(File.Contents(“C:
\Users\” & UserName_fx() & “\PowerBI Files\Customer.csv”),[Delimiter=”,”, Columns=45, Encoding=65001, QuoteStyle=QuoteStyle.None]),