Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Anonymous
Not applicable

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
v-evelk
Employee
Employee

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

View solution in original post

13 REPLIES 13
v-evelk
Employee
Employee

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

Anonymous
Not applicable

@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.

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

Anonymous
Not applicable

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.

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

"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

Anonymous
Not applicable

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?

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

Anonymous
Not applicable

(I rebuilt report.)

I observe several problems with field options.jsonFilters in update method.
A) First of all I apply filters like following.

{
    "$schema": "http://powerbi.com/product/schema#advanced",
    "target": {
        "table": "Data",
        "column": "Type"
    },
    "filterType": 0,
    "logicalOperator": "And",
    "conditions": [
        {
            "operator": "Is",
            "value": "Type2"
        }
    ]
}

But on next iteration options.jsonFilters contains another type of filter.

{
    "$schema": "http://powerbi.com/product/schema#basic",
    "target": {
        "table": "Data",
        "column": "Type"
    },
    "filterType": 1,
    "operator": "In",
    "values": [
        "Type2"
    ]
}

Is it normal behavior?

B) Does field options.jsonFilters contain filters applied by this visual only?
C) Are there any methods to compare filters in view of issue A?
D) In custom matrix visual field options.jsonFilters contains applied filters. But in custom slicer visual it is always empty. Why so?

A) It is a regular situation that filter looks not exactly the same as initial when you get it from options.jsonFilters .

It happens because filter will be converted into a query inside Power BI and then will be restored form this query when you receive it.

B) options.jsonFilters should contain all applied filters as I know.

C) I am not sure

D) It may happens if your custom visual uses an old API because jsonFilters appeared only in API 2.2 but it is better to use the latest API.

 

Kind Regards,

 

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

Anonymous
Not applicable

pbiviz.json of both projects contains

"apiVersion": "2.3.0"

May filters visibility depend on either type of filtered column or kind in dataRoles (capabilities.json)? It is Text in Grouping for custom matrix visual and Date in GroupingOrMeasure for custom slicer visual.

No, I don't think so.

 

You can send the source code and the report to pbicvsupport@microsoft.com and I will try to figure out what is wrong with filtration if you wish.

 

Kind Regards,

 

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

Anonymous
Not applicable

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
                    );

 

 

Helpful resources

Announcements
March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.