Forum Discussion
asm495
3 years agoRegular Visitor
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...
- 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 IncludeThe logic is to:
- 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.
- Those four combinations are:
- ( Country, Territory )
- ( Country, * )
- ( *, Territory )
- ( *, * )
- 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
OwenAuger
3 years agoSuper User
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:
- 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.
- Those four combinations are:
- ( Country, Territory )
- ( Country, * )
- ( *, Territory )
- ( *, * )
- 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
asm495
3 years agoRegular Visitor
Thank you so much. This was the perfect solution and did exactly what I needed! Very much appreciated.