cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
WSwanson Frequent Visitor
Frequent Visitor

Restricting Query by Username DAX Command in Direct Query

We have a Direct Query dataset in which we are trying to limit a table specifically by the domain name of the user pulling the report. This is being done in the Advanced Editor screen in the Query Editor using the Report Server solution.

The current query (when hard-coded) looks like this, and works fine:

 

let
Source = Sql.Database("Server", "Database",
[Query = "Select Id, DomainName From Database.dbo.Table Where DomainName = 'Hard Coded Domain'"])

 

in
Source

 

We are trying to make the 'Hard Coded Domain' piece be dynamic with the following code:

 

let
LoadingUser = USERNAME(),
Source = Sql.Database("SRVDB", "BI",
[Query = "Select EmployeeId, EmployeeName, DomainName From BI.Base.Employee Where DomainName = '"&LoadingUser&"'"])

 

in
Source

 

However, this produces the error: "Expression.Error: The import USERNAME matches no exports. Did you miss a module reference". Then it says "Expression.Error: The name USERNAME wasn't recognized. Make sure it's spelled correctly"

 

Has anyone ever seen this error before, and if so, is it even possible to use the USERNAME command to limit a query in Advanced Editor?

1 ACCEPTED SOLUTION

Accepted Solutions
Microsoft v-jiascu-msft
Microsoft

Re: Restricting Query by Username DAX Command in Direct Query

Hi @WSwanson,

 

USERNAME() is a DAX function. So you can't use it in the Query Editor that uses Power Query. Please vote on this idea. The workaround could be found here. I tested it with R code. It worked. You can try it like bellow.

let
    RScript = R.Execute("output <- read.table(text=system2(""whoami"", stdout=TRUE))"),
    output = RScript{[Name="output"]}[Value],

    Source = Sql.Database("SqlServerName", "contosoretaildw", [Query="select * from DimCustomer where lastname = '&output&'"])
in
    Source

Best Regards,

Dale

 

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Microsoft v-jiascu-msft
Microsoft

Re: Restricting Query by Username DAX Command in Direct Query

Hi @WSwanson,

 

USERNAME() is a DAX function. So you can't use it in the Query Editor that uses Power Query. Please vote on this idea. The workaround could be found here. I tested it with R code. It worked. You can try it like bellow.

let
    RScript = R.Execute("output <- read.table(text=system2(""whoami"", stdout=TRUE))"),
    output = RScript{[Name="output"]}[Value],

    Source = Sql.Database("SqlServerName", "contosoretaildw", [Query="select * from DimCustomer where lastname = '&output&'"])
in
    Source

Best Regards,

Dale

 

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Microsoft v-jiascu-msft
Microsoft

Re: Restricting Query by Username DAX Command in Direct Query

Hi @WSwanson,

 

Could you please mark the proper answer as solution or share the solution if it's convenient for you? That will be a big help to the others.

 

Best Regards!
Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Exclusive LIVE Community Event #3 – Ask Arun Anything

Exclusive LIVE Community Event #3 – Ask Arun Anything

Join us in the third Triple A event!

Meet the 2020 Season 1 Power BI Super Users!

Meet the 2020 Season 1 Power BI Super Users!

It’s the start of a new Super User season! Learn all about the new Super Users and brand-new tiered recognition system.

Super User Challenge: Can You Solve These?

Super User Challenge: Can You Solve These?

We're celebrating the start of the New Super User season with our first ever Super User 'Can You Solve These?' challenge.

Power BI Desktop Update - February 2020

Power BI Desktop Update - February 2020

We are super excited for our update this month, as we are releasing two of our top community requests!

Power Platform Online Conference

Power Platform Online Conference

Join us for the first ever Power Platform Online Conference!

Top Solution Authors