Forum Discussion
set value field in report powerbi by custom visual
- 8 years ago
This code snippet is going to address the issue:
let selectionIds = []; selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 1) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 1) .createSelectionId() ); selectionIds.forEach((selectionId) => { this.selectionManager.select(selectionId, true) });Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
This code snippet is going to address the issue:
let selectionIds = [];
selectionIds.push(host.createSelectionIdBuilder()
.withCategory(categorical.categories[0], 0)
.createSelectionId()
);
selectionIds.push(host.createSelectionIdBuilder()
.withCategory(categorical.categories[1], 0)
.createSelectionId()
);
selectionIds.push(host.createSelectionIdBuilder()
.withCategory(categorical.categories[0], 1)
.createSelectionId()
);
selectionIds.push(host.createSelectionIdBuilder()
.withCategory(categorical.categories[1], 1)
.createSelectionId()
);
selectionIds.forEach((selectionId) => {
this.selectionManager.select(selectionId, true)
});Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
I tried this snippet under Power BI API 2.1, it works fine for the first row, then it continues to work only for that row, so I can deselect it, select again, etc. And another control (e.g. Donut chart) displays selected value highlighted while I would expect all other values filtered out and only selected one displayed. Then if I select another row, Donut chart displays "Can't display the visual" error.
Even if I deselect that another row and select the 1st row back, I still get that error.
Under API 1.13 this snippet has no effect.
So what I want to know is how can I filter out unselected rows, not just highlight selected ones, so my slicer to work like HierarchySlicer or Sample Slicer.
Is the only way to achieve this is to use Interactivity Service?
Here is how I did this. First I added a check box for each of options.dataViews[0].categorical.categories[0].values both to control and this.checkboxes list so that i in snippet below represents row index and then when user checks or clears any checkbox, I clear selection and add each row index to it.
private onCheckBoxChange() {
this.selectionManager.clear();
for (let i = 0; i < this.checkBoxes.length; i++) {
const checkBox = this.checkBoxes[i];
if (checkBox.checked) {
for (let category of this.updateOptions.dataViews[0].categorical.categories) {
const selectionId = this.selectionIdBuidler
.withCategory(category, i)
.createSelectionId();
this.selectionManager.select(selectionId, true);
}
}
}
}selectionManager and selectionIdBuilder are created in constructor
constructor(options: VisualConstructorOptions) {
this.selectionManager = options.host.createSelectionManager();
this.selectionIdBuidler = options.host.createSelectionIdBuilder();
...
}and the checkboxes in update method
public update(options: VisualUpdateOptions) {
...
const values = options.dataViews[0].categorical.categories[0].values;
for (let i in values) {
const value = values[i];
const checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.addEventListener("change", () => this.onCheckBoxChange());
this.checkBoxes.push(checkBox);
...
}
...
}
- v-viig7 years agoCommunity Champion
Do you want to imlement a Slicer to filter data out?
If so, please use JSON Filter.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
- seregindv7 years agoFrequent Visitor
I just want to implement the same behavior as SampleSlicer has in the bottom part: show list of possible values, where some of them can be checked and all unchecked filtered out.
Is JSON filter suitable for this task? I though it is primarily intended to be used for range filters and I need to use something different that allows to select discrete values..
- v-viig7 years agoCommunity Champion
Yes, JSON Filter can be used to filter data. In your case you shoould use BasicFilter.
You can find details in powerbi-models documentation.
const basicFilter: models.IBasicFilter = { target: { table: "Products", column: "Version" }, operator: "In", values: [ 1, 2, 3, 4 ] };
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals