Forum Discussion

Twister8's avatar
Twister8
Helper II
10 years ago

USERNAME() function DAX

Hello everyone,

 

I'm new in Power BI and I have a doubt:

 

I would like use the Username() function DAX, to filter some datas in Dashboard, like RLS, for example:

 

User DOMAIN\Twister8 can see Country = China

another users can see all Countries

 

IF( Username() = "DOMAIN\Twister8", Country = "China", Country)

 

I wanna use the Username() function, because the anothers users doesnt has Pro License.

 

In my test using the RLS conditions like:

TEST - China [Country] = "China"

 

When I share the Dashboard with anothers users, the message about requeried Pro License its showed.

So, my question is:

Its possible use alternative, Username() functions, to restrict access on Data for users without use Pro License.?

 

 

6 Replies

  • v-haibl-msft's avatar
    v-haibl-msft
    Microsoft Employee

    Twister8

     

    I tried to use alternative as you said but have no success. I’ll try to explain my ideas as below though it is proved to be impossible.

    I first create another table to store the users which need to be restricted access on Data.

     

    Then create a measure to get the countries which should be shown for this login user. If the user is Twinster8, then China will be returned. If the user is others, then “” will be returned.

    CountryShow = 
    VAR Country =
        LOOKUPVALUE ( Table2[Country], Table2[Name], USERNAME () )
    RETURN
    ( IF ( ISBLANK ( Country ), "", Country ) )

     

    At last, try to create a calculated table to show the data of specified countries. I though it should be OK now but a "not supported" warning message returned.

    ("CUSTOMDATA and USERNAME functions are not supported in calculated columns. These functions may only be used in Measures or in the AllowedRowsExpression.")

    A related idea can be found here.

    FilterTable = 
    CALCULATETABLE (
        Table1,
        FILTER ( Table1, SEARCH ( [CountryShow], Table1[Country],, 0 ) > 0 )
    )

     

    So in my opinion, the only way to do it now is to use RLS with Pro License.

     

    Best Regards,

    Herbert

      • v-haibl-msft's avatar
        v-haibl-msft
        Microsoft Employee

        Twister8

         

        Let’s try to use this function in measure as below. Not sure if it is the result you wanted.

        Assuming we have a simple table like the following one.

         

        We can create another new table which specific someone can only see the specified country.

         

        We can create a measure with following formula to show the total sales according to the login user of Power BI service.

        TotalSales = 
        VAR CountryForUser =
            LOOKUPVALUE ( Table2[Country], Table2[Name], USERNAME () )
        RETURN
            (
                IF (
                    CountryForUser <> BLANK (),
                    CALCULATE (
                        SUM ( Table1[Sales] ),
                        FILTER ( Table1, Table1[Country] = CountryForUser )
                    ),
                    CALCULATE ( SUM ( Table1[Sales] ) )
                )
        )

        When I login to Service with the specified user in Table2, I can only see the total sales of China.

         

        After I share the dashboard with another user, he can see the total sales of China and USA.

         

        Best Regards,

        Herbert

    • Hance's avatar
      Hance
      Frequent Visitor

      bad guys Dev team want more money and require pro license from all users who use this report