cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
yury-shafran Frequent Visitor
Frequent Visitor

Support other visuals' selection in custom slicer

Hi, I have the following task.

Table DATA has columns: Date, Type, Company, Object, Device, Actual. Table DATES has only one column Date (the table is DISTINCT(DATA[Date]) in DAX). I need to draw chart for some days (two on the picture) before selected date. So I put default slicer visual (top center) to select DATES[Date] and create measure (center) to get max value. After that I try to make custom slicer visual (right) to select required days for chart visual (bottom right) from DATA[Date] using defined measure.

testerPicture.PNG

Used code:

// ISelectionManager creation in visual constructor
this.selectionManager = options.host.createSelectionManager();

// ISelectionManager usage in visual update
const selection: ISelectionId[] = [];
// ... filling selection ...
this.selectionManager.select(selection);

Question A. Is there any simplier approach?

When I try to select specific item in default matrix visual (left) that last selection is cancelled.

Question B. How can I support selection made in other visuals (default or custom) in my custom slicer visual?

I saw Sample Slicer project but there is too much code with insufficient description.

Question C. Does "Sample Slicer" contain information that I need? Where should I watch?

 

1 ACCEPTED SOLUTION

Accepted Solutions
v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

Hi,

 

To merge filtration, you should use applyJsonFilter with "merge" option.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

13 REPLIES 13
v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

Hi,

 

To merge filtration, you should use applyJsonFilter with "merge" option.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

yury-shafran Frequent Visitor
Frequent Visitor

Re: Support other visuals' selection in custom slicer

Thank you, @v-evelk 

 

Before I had received your answer I decided to build custom chart visual and filter dates programmaticaly there. After I had completed I returned to topic problem.

 

I describe steps to achieve the goal. It may be useful to someone.

1) Install powerbi-models using following command in project folder.

 

npm install --save powerbi-models

2) Add link to pbiviz.json.

 

"externalJS": [
    "node_modules/powerbi-models/dist/models.js"
  ]

3) Add link to tsconfig.json.

 

 

"files": [
    "node_modules/powerbi-models/dist/models-noexports.d.ts"
  ]

4) Add following code to update method. (The date to be selected should be under first (zero index) category.)

 

 

 

                    const conditions: IAdvancedFilterCondition[] = [
                        {
                            operator: "GreaterThanOrEqual",
                            value: new Date(startMillis) // First date.
                        },
                        {
                            operator: "LessThanOrEqual",
                            value: new Date(endMillis) // Last date.
                        }
                    ];
                    const categories: DataViewCategoricalColumn =
                        options.dataViews[0].categorical.categories[0];
                    const target: IFilterColumnTarget = {
                        table: categories.source.queryName.substr(
                            0, categories.source.queryName.indexOf('.')),
                        column: categories.source.displayName
                    };
                    const filter: IAdvancedFilter =
                        new window['powerbi-models']
                            .AdvancedFilter(target, "And", conditions);
                    this.visualHost.applyJsonFilter(
                        filter, "general", "filter", FilterAction.merge
                    );

 

 

yury-shafran Frequent Visitor
Frequent Visitor

Re: Support other visuals' selection in custom slicer

@v-evelk, additional question. How can one support others' selection in custom matrix visual? I see 

IFilterKeyHierarchyTarget but how to fill it and how to set conditions for all levels of hierarchy? This topic tells that only one filter can be applied.
v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

I am not sure about matrix. If it uses selectionManger, you can try to use selectionManager with the second parameter as 'true' to enable multi-selection.

 

this.selectionManager.select(selector, true).then((ids: ISelectionId[]) => {
    //called when setting the selection has been completed successfully
});

 

Regarding IFilterKeyHierarchyTarget I will ask our engineers and provide info here later.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

yury-shafran Frequent Visitor
Frequent Visitor

Re: Support other visuals' selection in custom slicer

Yes, I use ISelectionManager to filter data from custom matrix visual.

Unfortunately, true in select doesn't change the behavior. My custom visuals (matrix and slicer) go to infinite selection loop. Standard line chart shows data selected by matrix for a second. Then it shows data selected by slicer for another second. After that it shows data selected by matrix again. And this process continues until I close Power BI.

With standard matrix visual all the elements work fine.

v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

Probably standard matrix uses filters too.

I am not sure that it is possible to merge filters and selections, you should use one of these approaches if you want to combine selection or filtration.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

"I see IFilterKeyHierarchyTarget but how to fill it and how to set conditions for all levels of hierarchy? This topic tells that only one filter can be applied."

 

Regarding this question, I don't have any examples of using this filter, but I can say that you can pass an array of filters into applyJsonFilter function that will be mean logical AND.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com

yury-shafran Frequent Visitor
Frequent Visitor

Re: Support other visuals' selection in custom slicer

I replace selection in custom matrix visual with filtering through applyJsonFilter. Now Power BI does not hang and I can cancel filtering by another click. But all the visuals keep refreshing repeatedly.
I can guess the following. One custom visual applies filter. So all other visuals should be refreshed. When refreshing another custom visual applies its filter. All other visuals (including first one) should be refreshed again. First custom visual applies its filter again. And this process continues.
Am I right that there is no control on filters duplication?
If my giess is correct can custom visual recognize that its filter is applied now?
If I'm wrong what is the problem?

v-evelk Established Member
Established Member

Re: Support other visuals' selection in custom slicer

I can say that it depends on a visual and how it is implemented. Since you use filtration, your update options must contain something like options.jsonFilters so, you can detect whether there are any already applied filters or not.

Also, since you switched Selection model to Filtration model I can recommend you to remove visuals from your report and add them again because there is a chance that some old filtration or selection was saved and now it breaks filtration.

 

Kind Regards,

 

Evgenii Elkin,
Software Engineer
Microsoft Power BI Custom Visuals
pbicvsupport@microsoft.com