compare a measure to a value in a column - powerbi

Power BI noob here, still thinking like a SQL coder, so please be patient.
How can I use the user name of the person running the report to filter the report?
As a convenience for my users, I want to provide a way for them to automatically filter to only see data related to their office or region. I have a Person table that includes details like their office location. If I can filter that based on the user name of the person running the report, and join it to the rest of the data, that would work.
Unfortunately, I don't see a way to get the user name in M.
Using the USERNAME() function in DAX, I don't see a way to compare this with individual values in a column. I get an error about being unable to compare a measure to multiple values.
It seems this would be a common request, so I'm sure somebody has solved this problem. But I haven't yet found the solution.

Use a RLS in your model. You can use function USERNAME(),USEROBJECTID (), USERPRINCIPALNAME (); The last one is useful if you have table with users and their email.
https://learn.microsoft.com/en-us/power-bi/admin/service-admin-rls
check also this GuyInCube video:
https://www.youtube.com/watch?v=MxU_FYSSnYU

First, be aware of the differences between USERNAME() and USERPRINCIPALNAME(). Most likely you will want to use the later one.
You can't use neither of these in M. Imagine your model is importing the data. The M code is executed once in the context of one user, then each other user accessing the published report will reuse the already loaded and calculated model. And of course, these are DAX functions, not M.
So in DAX you can use these to compare their values to columns from your model. You didn't gave any information about your model, but lets say there is a table Sales with columns Customer and Amount:
Customer
Amount
Bill Gates#Microsoft.local
100
Steve Ballmer#Microsoft.local
110
In this case, you can write a measure like this:
My Sales = CALCULATE(SUM('Sales'[Amount]), 'Sales'[Client] = USERPRINCIPALNAME())
When Bill Gates opens the report, he will see sales amount of 100, while if Steve Ballmer opens it he will see 110.
For diagnostic purpose, you can make a measure like this and show it somewhere in your report:
Who am I = USERPRINCIPALNAME()
If your goal is to build dynamic Row-Level Security within Power BI, it has some functionality which might help you, so take a look at these articles:
Row-level security (RLS) with Power BI
Restrict data access with row-level security (RLS) for Power BI Desktop
Row-level security (RLS) guidance in Power BI Desktop
Dynamic Row Level Security with Profiles and Users in Power BI : Many-to-Many Relationship

Related

Powerbi ability to create snapshot data from a historical table of data

I have a sql database linked where I have the complete history of products and users. I want to the user to be able to select on the slicer a year and the data automatically shows active prodcuts, expired products and new products added in that year (or snapshot).
Is there a way this can be done? I am not able to find a measure to best do this for me.
I recommend creating a date dimension table first - I usually call mine Calendar. Please read this useful post by Radacad which will show you how to create one > https://radacad.com/power-bi-date-or-calendar-table-best-method-dax-or-power-query
Once it's done create relationships between your fact tables and calendar table on key dates of when your products are active or expired - I'm making a huge assumption that's what your tables store.
Your calendar table will then act as a single time/date point of truth and should be used to slice and dice your fact table.
Hope this helps!

Power BI - Merging multiple customer tables

Connection Type: Direct Query to multiple sources so limited DAX available especially in Power Query load.
Data Model Query: The Data model is not a perfect star schema but there is an attempt to separate tables into business processes and lookup tables. There are probably a few issues to discuss the current data model. I only have 1 question at this time.
My current goal is to generate a single summarised customer table to replace the current two tables that have some measure I need like the number of app customers, a number of total customers, date customer first accessed app etc.
So I cannot merge the 2 customer tables and add calculated columns and measures at the import stage as power bi does not support or allow it and sql is out as I am using direct query. My plan is to create a summarised customer table using DAX summarise function on front end visual page, that has only the app customers and then measures like the total number of customers etc. Is this best practice or is there a better way of approaching this? Understand you would ideally do in sql, or power query but in these circumstances, I think this is the best way but wanted a second view.
Is there a reason to use Direct Query over Import? If you are in Import mode, you can easily Append the two client tables together in PowerQuery.
Treb Gatte, Power BI MVP

Row level security in Power Bi

I am not able to implement row level security to my report. I have tried different methods on google and Microsoft forums but all in vain. Could you please help?
I have a dataset name "Cases" which have details of all cases logged into system with its country.
Then I have dataset name "Escalations" with details of the all the escalations along with its country.
Then there is one more table called "Country Mapping" which contains all the countries mapped with their Regions and Region Manager.
I have already prepared a report showing Global /Regional numbers But now people want to restrict the data according to its own territory.
Example :- Some want to see global data and some are responsible for a particular country. I know RLS can help me with this. I have tried to make two tables one with Username and country responsible and other one with just usernames and names.
I have made a relationship between all these tables. But the problem with the code is, like if I am the admin of the report then I need to manually add myself in the country responsible for every country. Like 146 countries .
Is there any better way to do this?

Power BI - How do I count the number of times a value appears in relation to a separate column?

I'm trying to produce a report in Power BI to count the number of groups an individual belongs to. I have a table with users, security groups they belong to, etc.
I'm looking to query the data to help track down users that don't belong to at least two security groups. In my environment every user should be in an All Staff security group plus any other unique group they belong to.
The image I've included has exactly what I'm looking for. I currently don't have that Total column. This is what I need help with. I have the Users and all of the groups they belong to.
Here's a picture of what I'm trying to achieve:
I've tried the Countrows command with a filter but I couldn't get it to filter based on the number of groups users belong to.
I can't simply count the number of times a user's name appears either because this table is a lot bigger than just my example above. This is just an extra feature I'm hoping to achieve out of my table - using the analytics of my report to help find group membership issues and resolve them.
To create the calculated column, you can use this code:
Totals = CALCULATE( COUNT(TableName[Group]), ALLEXCEPT(TableName, TableName[User]))
It groups records by 'User' and then counts number of groups per each user.
I'd recommend though to use measures instead of calculated columns. Measures are much more powerful and useful ways of reporting. To create a measure:
Totals =
SUMX(
VALUES(Data[User]),
CALCULATE(COUNT(Data[Group]))
)
Drop this measure into a matrix or a chart against the users, and you will have the desired report.

Filter data in power BI embedded

We currently host data for multiple users in our database. I'd like to implement embedded power BI into our web app. When the user logs into our system, I'd like the data source to be filtered according to the user that is logged in, so e.g. SELECT * FROM Table1 WHERE ItemID in (ItemID1, ItemID2) etc..., we aren't going to know what ItemID1, ItemID2 etc... are until after the user has logged on.
Is this possible with PowerBI embedded?
To filter data for Power BI users based on which user is logging in to the embedded web app, complete the following:
Sample:
Create a table to store the usernames for each filter "group."
You will use DAX to create a measure to identify the users from your table, and assign them to a specific user role group. Below is the DAX to use:
[USERNAME] = [Current User]
Create the measure described in point #2 in the Row-Level security settings. By creating different "groups" for the users, you are essentially dynamically-building a "filter," where you only show the users what they should be seeing--thus resulting in pseudo-filtering. For more information see the following:
https://learn.microsoft.com/en-us/power-bi/desktop-tutorial-row-level-security-onprem-ssas-tabular
http://community.powerbi.com/t5/Service/Restricting-filters-to-specific-users-in-Power-BI-report/td-p/109111
Hope this helps!