to our friends in this video we're going to see how to implement a security configuration for an organization where account employees are managed by a hierarchy of managers and we want to reduce at the minimum possible the way we Define the security configuration for users that have to access to this report by using just a list of the managers that they want to see we will see the list of the requirements where we understand that the hierarchy of the managers in a large organization maybe with hundreds or thousands of employees could bring to a number of steps in the definition of the configuration for the security in order to make it also efficient because one of the challenges when you have the security configuration is to make sure that you don't slow down the reports that you are going to create and in a large organization with a hierarchy of managers the risk is high that you get slow reports so let's start with the demo let's start taking a look at the final result we want to obtain in this model I have the solution already implemented and you see that we have the classic contoso database where we have the sales the classic sales connected to product connected to customer but in this case every customer has an account an employee account and each account can be can have should have a manager and we also have a hierarchy of managers that actually manage the entire sales organization imagine we could have thousands of employees in this organization so one of the issues we have is that we want to simplify the configuration definition and we want also to make it efficient because if we have a slow definition of the filter in the security rule that could affect every report so the goal we want to obtain is the following here I'm I don't have the security applied but we want to Define for each user that can access this report a limitation to the main managers that that user can see being able to see a manager means being able to see the entire organization below that manager and all the employees that report to those managers for example if I connect by impersonating a user for example Marco at contoso. com Marco has access to two managers and please take a look at the value that now is 12 million in the total sales amount in both of the reports and you see that we have two reports one shows the sales by brand and one shows the sales according to the management organization so just the list of the manager and the sales amount for all the employees and manager below each node so once I click marker contoso. com you will see that these numbers change I click OK and the total amount is just less than 6 million now so we have less than half of the sales why because Marco at contoso.
com has access to only if I remember well John and header now please pay attention to Heather because header has a manager Peter but Peter has also other managers below him and we don't have access to those other managers which means that also the value that is visible for Peter is actually the value of only the manager we are allowed to see for that part of the organization indeed if I stop viewing by using Market contoso. com you see that Peter has a header and James below so we didn't see the value for James once we apply the security so this is what the security is intended to be a way to fit the data no matter what in any report regardless of the dimensions indeed you have seen that also the sales by brand were affected by the effect of the security implementation this is the final result we want to obtain I now move to a model where I didn't implement the security yet so we have access to the entire model at this point but we already imported what we want to be our table to define a security so if I go in permissions this is a table that we imported let's see here we imported from um from an external configuration it could be an Excel file a SQL Server table and the minimum number of information we want to Define is the following for Marco contoso. com we Define only the two manager names in this example every manager name is unique across the entire organization we will see later an example where we implement the same model but with the difference that every manager name is unique only considering the entire path when you have a large organization you might have this problem so if you don't have a unique ID to identify the manager you identify identify the manager across the entire path to which they thought we will see this later now you notice that Margo has also a third manager that should be visible but Mickey doesn't exist actually make it is a is a typo there is a manager called Nikki with the N instead of the M if I go back I should be able to see that nikhil he is indeed here with the N but I intentionally wrote an error in the configuration because I want to see how I could manage errors in the configuration creating a report to actually display these problems because of course Marco will not see nickel would not even see Mickey because it doesn't exist but we may want to report this error in the configuration later to make sure that someone will fix it so this is the starting point but how is the existing model defined because we didn't see the existing model so the existing model is the following so the the permission table is the table that we see here is isolate is a table that doesn't have any relationship with anything at the moment and our sales table that I can see here in this Zone in this area is connected to the customer table now the customer table has a many-to-one relationship with the organization how does it work let's take a look so we have you see two columns we have account employee an employee here so let's start from the customer table the customer table has for each customer we can see this here for each customer has an account employee of course every account employee could manage different customers now every account employee where your account has a manager and where do we have this information well this information is included in the organization table the organization table we have this structure oops sorry I typed too many too many keys so the employee column is a unique column for this table so for each employee we can see uh what who is the manager that manages that employee and in case there is a hierarchy of manager so the manager of ADA has a manager and that has another manager we actually see these three levels of management defined here this is the way you have to Define this table if you have a different data source that for example defines the stable through a potential hierarchy I suggest that you take a look at the parent child pattern published on daxbattles.
com to see how we create actually the same structure we see here where we have what we call a record hierarchy which means that in a few cases we had three levels in a few cases we have a two levels in other cases we have five levels so we could have a different number of levels and this is a typical situation we have in many organization now starting from this structure we created two columns the manager ID and the depth the manager ID is simply the concatenation of all the manager names across all the levels that are visible separated by a pipe why we do this because this way we can easily obtain this number the depth of the organization which can be helpful to create measures that hide the the the levels that don't have meaningful data this could be useful but again this is part of the pattern for the parent child now if you think for a moment the organization table is connected to customers so one approach could be well we could you we could use a function that look for the name of the manager within this column because at the end of the day if we can make this sample that every manager is unique when I if I have access for example to Stephen well Stephen would give me access to this node but not to John Russell or not to John Carmen Francisco Fiona charts so we actually already have a relatively easy way to define that but what is the issue if we Define the security straight in the stable organization we will pay a big price for the performance and we also have to write a more complex rule to to to find the string but the thing is that every query should have to scan this table looking for the content inside a query so the idea to keep the existing configuration which is very simple is to expand this table so that we actually obtain a direct list of all the employee names that are visible for each account assigned to a particular user because by doing that we can actually have an easy way to define the security so that it automatically propagates to the table organization in a very efficient way in order to do that so we have to create a table that we will connect to the organization table what does it mean I expect to create a table here which create a list of all the employees I can actually filter and I will create a relationship between the this new table and the organization table for example and this should should make it easier to to apply the Theta because I will just filter a column that I create here with the username so that for each username I have exactly the list of account employees I can filter that's the idea to remove the analysis of the organization from the evaluation of the security when the query runs because we can create a table maybe a calculated table so it's automatically updated that has this information computed only once whenever we refresh the model so we move the complexity at refresh time but we execute this only once and the small price to pay a new table in the model should be relatively cheap because we would have a very high compression because we will duplicate many rows many values and many arrows how can we do that so as we said we have to create a calculated table so in order to create a calculator table we go I I prefer to go in this visualization because here I can actually see the content of the table in terms of name of the columns and the table I want to create is a table that I will call permission suspended so permissions expanded expanded so let's start with a simple copy of the existing table the existing table is permissions so we can see the result of this table is just a copy of the existing table the first thing I do I just want to rename these columns because this way I I obtain names that are more meaningful in the table I want to obtain at the end and so the the rename can be obtained by using select columns so I I just want to rename actually one column but I have to copy the user column because I want to keep the username permissions user and then the new column the column that I want to use is the assign manager list so which is which manager has been assigned to a particular user and in this case either I assigned manager ID which is the permissions manager ID okay so um I I made a small mistake probably when I Define the the goal of this table I don't actually the I don't have to create the list of the users that I can see I mean I could but this could you know expand the table more than necessary if you remember I had a column that contained the ID of the manager by using the entire path and I could use that information because that information is unique for that manager name in a point in the organization and I just had to to list all the possible values of that column because this way I reduce the number of rows that I have to transfer in this filter and I will leverage on the existing organization table so that I don't have to create a larger imagine you have 20 30 000 employees I actually can just create an intermediate table that has only the list of the manager including the the managers below the manager I can access and the expansion to the list of the employees can be made by just uh applying the filter over organization because organization already has that so my goal is that for each row in the permission table I want to create the list of the managers that are below the manager that is assigned for this for this reason I started with the assign manager ID because I want to create another column uh that I will call manage ready that has the list of the managers how can I do that well I can use the generate function generate function is an iterator and for each row of Select columns can execute another table expression with a current row context in in this case in the permissions table in the rows generated by this stable expression will be will be merged and joined let's say to the to each row of the permissions so that for example if the first row in the permission has a file managers we will get five rows with the same two columns at the beginning user and assign manager the the new column manager edit which is the list of the managers below that manager including also the manager we want to filter how can we do that well at this point we will certainly use a calculate table function because the calculate table transforms the row contest interfitter context and our goal is to get the list of the unique manager ID in the organization table so that is our goal but in order to do that we have to apply a filter and in order to apply a filter we can use uh we can use what we can actually check we can actually check whether the um the the the path that we have in managerie because remember manager ID is the column that contains the entire path if that path includes the manager ID we have in permission so we have this permission ID that can be used as a filter to search whether the path contains that name we have a function a path contains that can do exactly what we want so path contains get check and check whether the manager ID includes my permission manager ID the idea is that every ID is unique so the presence of this manager ID in any point in any point of the hierarchy is enough to make sure that this will work I just have a problem now by using the context transition I lose the access to this value I no longer have the row contest I will have the filter context the the content solution I saw the filter contest but this is unnecessary complex so it's easier if I store in a a variable assign manager ID the value of the permissions manager ID when I have sealed the row context available and then I return the result of this calculate table at this point I can use this variable here and the other thing I do I actually do I want to avoid any possible issue of circular dependency because now we are going to to create a new table then we will create a relationship I just want to make sure I don't I don't have other other filters involved and actually by doing this I have a conversation maybe that this is unnecessary but this makes it clear that I don't need any contest transition out of this calculation and let's see if this works so I don't know why we have this error but let's try to include this uh the manager ID is not available all right because at this point I renamed it let's clear it I renamed this name just to avoid the confusion between the two different names so actually I'm using the permissions manager already but because of the rename I have to use the new name and the reason why I had to use the new name is because otherwise I would have obtained two columns with the same name because you see that the column I'm adding is manager ID so let's try now this should work at this point and this is the the result we have let's take let's take a look so we have oh let's sort by username so that we have all the rows for Marco and John here and you see that Marco actually expanded all the path that include John we only see the path that exists in the organization table you see that for example we had charts we have Stephen we don't have a Fiona for example because there are no employees that report with Iona in the simple organization table that we have but otherwise we would have had the entire list all the all the all the unique combination unique nodes and unique paths that we have starting from John because John was included in our uh definition then we have if you remember Marco had header which is here and because Heather is here now eating it includes Peter but Peter is included only for the path that has header and anything below now in this case we have a small number of rows in the organization table but you can imagine that this this list could be larger if we actually had thousands of employees larger but not as large as the organization table so actually the number of rows we obtain here is more compact even though we repeat this list for each user so depending on the number of users we have to Define here this this could be long but remember when the the security feeder will be applied only one user will be filtered and so that should be relatively fast now that we have this table what should we do well at this point we have to go back in the data model in the data model view because now I can create a relationship between let's see if I can highlight this between the manager ID column and the manager ID column we actually have the same column name but also the same content because actually we copied that column here and an efficient way because the goal is that we want to filter the user so we want to filter the user by filtering the user we want to filter the list of the manager IDs that this user can see and by propagating this filter to the organization table we will also filter the employees and then the customer that had to be fitted starting from that user let's do that so I just create a relationship and in this case because we don't have a primary key on any of the two sides of the relationship we have to create a many-to-many cardinality relationship relationship this is good but what is not correct in the default provided by the power bi user interface is the cross filter Direction what we really want to do we want only permission expanded filters organization so this is the only feature we want single permission expanded filters organization because we don't want to introduce a bi-directional filter and any ambiguity in our model there is no need for that so we want this one and once I do this permission expanded features organization I have this kind of definition for the relationship so the relationship is a single filter relationship let's see if I can highlight where when I filter the user I filter all the managers for that user and this filter is propagated to the manager ID column here that will filter all the employees so far so good but I still have to do the last Mark I had to complete the last mile which is I had to define the security role so in order to do that I go back to the report view where I go in modeling and I go in manage roles I already created a role a security role manager view that is empty at the moment and the only table I have to filter is this permission expanded table the I had to go to this Dax editor because what I want to do I want to filter the column user the column user it has to be equal to username so this is the expression I have to write unfortunately unfortunately the editor doesn't have intellisense yet I save this new rule and now what happens now I can try to apply my rule so I click on view as and I man I use manager view because I want to use this security role and I also want to impersonate Marco at contoso. com so if I click OK let's see if this works and as you see the security has been applied only feature in the data that Marco can see so actually I successfully implemented the security and as you see now we have the Security apply only on permissions Commander table if I go in permissions expanded as a table view I only see the rows for Marco at the moment I didn't filter the original permissions table so if I go in the permissions table and I can actually see the entire content in theory this table is hidden but technically the user could see the content of this table is for any reason you don't want to make the stable accessible to any user one thing you could do is to add in the manage roles another filter for permissions where you say where we say only false because this way we hide the complete configuration or we just repeat the same configuration we used in permission expanded where we filter user equal to username um actually I didn't want to add this this exam the security feeder because I wanted to make it clear that what matters is the permissions expanded table so the security filter we might add to permissions doesn't have any effect on anything so probably I prefer to use false because in this because this way if the user with a direct access to the model tries to look at the permissions content you see that even though I'm filtering by market contoso.