Let's suppose I've got a simple table with two columns: user, value
Is it possible to use the function USERNAME within a code in order to filter the information that corresponds only to the user that is logged? Something like this:
Select username, value
from table
where username = USERNAME()
And show only the information that every user should see.
Regards
Yes, it's possible. You can use the USERNAME or USERPRINCIPALNAME as a filter in CALCULATE or as part of a Row Level Security filter.
Related
I need to create a table based on a lookup. The problem is that in the CALCULATETABLE I cannot use username() function.
CALCULATETABLE (
LOOKUPVALUE ( 'User Table'[SOC], 'User Table'[username], USERNAME () ),
'User Table'[username] = USERNAME ()
)
Does anyone have any idea how I can proceed?
What I would like to have is a table with the list of companies for the logged-in user.
The tables in your model are computed only when the data is originally loaded or refreshed. As a result, you cannot use dynamic variables like slicer selections or usernames in their construction since those would require the tables to be recomputed each time they change.
What you can do is create visuals with measures that are filtered by username and/or use usernames as part of Row Level Security filtering.
The best option in your situation is the use of dynamic security, so that the logged in user will have access to only a subset of companies wish are related to him.
With this approch no calculated table needed.
Visit : https://www.kasperonbi.com/dynamic-security-made-easy-with-ssas-2016-and-power-bi/
Thanks for the answers,
I already use the row level security to filter data but I would like to make available (for specific request) also another table containing this information.
I could create a second table and filter also it with row level security.
Thanks
I show a model of sales that can be aggregated by different fields through a form. Products, clients, categories, etc.
view_by_choice = filter_opts.cleaned_data["view_by_choice"]
sales = sales.values(view_by_choice).annotate(........).order_by(......)
In the same form I have a string input where the user can filter the results. By "product code" for example.
input_code = filter_opts.cleaned_data["filter_code"]
sales = sales.filter(prod_code__icontains=input_code)
What I want to do is filter the queryset "sales" by the input_code, defining the field dynamically from the view_by_choice variable.
Something like:
sales = sales.filter(VARIABLE__icontains=input_code)
Is it possible to do this? Thanks in advance.
You can make use of dictionary unpacking [PEP-448] here:
sales = sales.filter(
**{'{}__icontains'.format(view_by_choice): input_code}
)
Given that view_by_choice for example contains 'foo', we thus first make a dictionary { 'foo__icontains': input_code }, and then we unpack that as named parameter with the two consecutive asterisks (**).
That being said, I strongly advice you to do some validation on the view_by_choice: ensure that the number of valid options is limited. Otherwise a user might inject malicious field names, lookups, etc. to exploit data from your database that should remain hidden.
For example if you model has a ForeignKey named owner to the User model, he/she could use owner__email, and thus start trying to find out what emails are in the database by generating a large number of queries and each time looking what values that query returned.
I have a column named company ID that contains various comapnies with differnet employes in them, I need a DAX Query which will give data A/Q to the Company id, suppose if there are 3 employes inside a company and each of them have a companyid 1 then I they should able to see the reports of each other but they cannot be able to see the reports of comanyid 2 and 3, how this can be achived?
I know I can do this by creating different Roles for each companyid, but how can this be achived if I want this to be built in one particular role???
What you are asking is to implement Dynamic Row Level Security.
Model:
User Table: Table that contains user detail along with the field on which we will apply security(here email field).
Company Table: Table containing company data .
User Company Bridge: Bridge table that contains permission details, for example user x is member of company y and z.
Company Data Table: Measures or transaction information of company that is to be filtered.
Defining RLS(Row Level Security):
In Modelling -> Manage Roles, create a new role on Email of User Table by this DAX query which returns the email id of logged in user.
[Email] = userprincipalname()
Finalizing:
Go to PowerBI Service -> Dataset -> Security and add users to the roles created.
To test the implementation:
Go to Modelling tab of pbix file.
Click on View As Roles.
Check other User checkbox and put an email ID and also check Profile
checkbox.
Now you can see data filtered.
In this manner it becomes easy to maintain roles and security by just modifying the bridge table that stores all permission details.
look at the following scenario:
I have an User model and an Address model that belongs to user.
In the user index, I need to show along with user's info how many addresses does the user have, but it's generating N+1 queries as everytime I call count it executes an additional query for that user id.
How can I do that? I read about select_related but I'm trying to make it in the reverse order...
In SQL it could be translated to:
SELECT user.*,
(SELECT count(*) FROM address WHERE address.user_id = user.id) AS address_count
FROM user
Is there a way to get the above SQL with django QuerySet?
You can annotate the number of addresses, you haven't shown your models but you can use the following on your queryset
.annotate(address_count=Count('address'))
User.objects.all().annotate(address_count=Count('address')) # Im guessing you want this
This would provide an address_count property on for each result
Docs for count
I am using the open_graph Explorer tool to test this out and I need to be able to get the first_name, last_name and installed from 1000 users. At the moment I group up the users in grouped of 50 for a single query and batch them into up to 20 queries in a batch request.
A single query looks as follows
?ids=100003825998801,100003825998802,547884299,100003825998803,100003825998804,100003825998805,100003825998806&fields=first_name,last_name,installed
Now, the question is, when you use multiple ids in a single query like I did, facebook seems to "force" certain fields on you, large fields, such as "metadata" and "fields". Is it possible to remove these fields from the result set of the query?
Also, any advice on how to make this more efficient would also be great.
Thank.
Have you considered using FQL instead …?
SELECT first_name, last_name, is_app_user FROM user
WHERE uid IN (100003825998801,100003825998802,547884299,100003825998803,
100003825998804,100003825998805,100003825998806)
– that gives you just the info you want, you only have to watch out for the fact that the installed field is named is_app_user in FQL.