Forum Discussion

asm495's avatar
asm495
Regular Visitor
3 years ago
Solved

Row Level Security - Multiple Filter Criteria

I am trying to use RLS to filter some data according to a table which stores users territory information. The territory information is a table ("UserTerritories") consisting of both a Country and a T...
  • OwenAuger's avatar
    3 years ago

    Hi asm495 

     

    There are different ways of writing the condition.

    I would recommend something like this (to be placed as a RLS filter on the Account table):

     

    VAR CurrentUser =
        USERPRINCIPALNAME ()
    
    VAR CurrentCountryTerritory =
        -- Construct 4 row table containing the 4 combinations:
        -- ( Country, Territory ), ( Country, * ), ( *, Territory ), ( *, * )
        VAR CurrentCountry = 
            SELECTCOLUMNS ( { "*", Account[Country] }, "@Country",[Value] )
        VAR CurrentTerritory =
            SELECTCOLUMNS ( { "*", Account[Territory] }, "@Territory",[Value] )
        RETURN
            CROSSJOIN ( CurrentCountry, CurrentTerritory )
    
    
    VAR Include =
        -- This returns TRUE only if at least one of the combinations in the variable CurrentCountryTerritory
        -- appears in the 'UserTerritories' table for the current user
        CALCULATE (
            NOT ISEMPTY ( 'UserTerritories' ),
            TREATAS ( CurrentCountryTerritory, 'UserTerritories'[Country], 'UserTerritories'[Territory] ),
            'UserTerritories'[Username] = CurrentUser
        )
    
    RETURN
        Include

    The logic is to:

    1. For each row of Account table, there are four possible Country/Territory combinations that could exist in 'UserTerritories' that would allow that row of Account to be vislble.
    2. Those four combinations are:
      1. ( Country, Territory )
      2. ( Country, * )
      3. ( *, Territory )
      4. ( *, * )
    3. If any of those is present in 'UserTerritories' when filtered on the current user, then return TRUE, i.e. the current row of Account will be visible.

    Does this work for you?

     

    Regards,

    Owen