You can achieve this in two ways - define filters when embedding the report or with Row-Level Security.
When embedding Power BI elements, you are initializing a configuration. One of the properties of this object are the filters to be pre-applied on the report, when loaded. Currently these are the supported filter types:
export enum FilterType {
Advanced = 0,
Basic = 1,
Unknown = 2,
IncludeExclude = 3,
RelativeDate = 4,
TopN = 5,
Tuple = 6
}
These types corresponds to the filters, that you can apply in the report when shown in a browser or in Power BI Desktop. For more information what properties each filter type has and how to use it, see Filters page in the documentation, but for example a basic filter needs to define the table and column, on which the filter will be applied, comparison operator, and of course values, e.g.:
const basicFilter: pbi.models.IBasicFilter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Count"
},
operator: "In",
values: [1,2,3,4],
filterType: 1 // pbi.models.FilterType.BasicFilter
}
or
const basicFilter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Count"
},
operator: "In",
values: [1,2,3,4],
filterType: 1 // pbi.models.FilterType.BasicFilter
}
Then set this filter in the configuration prior embedding:
var config = {
type: embedType,
accessToken: accessToken,
tokenType: tokenType,
embedUrl: embedUrl,
id: embedId,
dashboardId: dashboardId,
permissions: permissions,
settings: {
filterPaneEnabled: false,
navContentPaneEnabled: true
},
filters: [basicFilter]
};
Note, that in this case you should hide the filter pane, by setting filterPaneEnabled: false
, otherwise the user will see the pre-applied filter in the filters pane and will be able to change it or remove it! But if you hide the filters pane, this will limit the users options to analyze the data by only using the slisers and other options provided in the report itself. So using RLS may be a better idea, but it requires a dedicated capacity for your workspace (i.e. buying Power BI Premium or Power BI Embedded).
To use RLS you must define one or more roles in your report. For each role a DAX expression will be defined to filter the data. Then for each of your users, you can decide which role or roles to give and the report will show only the data accessible to these roles. Then use the AAD token that you get, to call GenerateTokenInGroup REST API and pass the roles that you want your user to have in the request body:
{
"accessLevel": "View",
"identities": [
{
"username": "john@contoso.com",
"roles": [
"sales",
"marketing"
],
"datasets": [
"cfafbeb1-8037-4d0c-896e-a46fb27ff229"
]
}
]
}
Then use the above acquired token to embed the report and don't forget to change the value of tokenType
property in the configuration to be 1 (embed) instead of 0 (AAD), if needed.
This way you can leave the filters pane visible, because the security policies will be applied under the hood by the engine.