<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Fix Filtering | Custom Visual in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Fix-Filtering-Custom-Visual/m-p/4615398#M59882</link>
    <description>&lt;P&gt;Dear Community,&lt;BR /&gt;&lt;BR /&gt;I'm developing a little custom visual that consists of &lt;STRONG&gt;two dropdowns&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Problem 1:&lt;/STRONG&gt; Currently my dropdowns are &lt;STRONG&gt;successfully populated&lt;/STRONG&gt; with data and I can &lt;STRONG&gt;select values&lt;/STRONG&gt;. I want to add &lt;STRONG&gt;filtering functionality&lt;/STRONG&gt; to the dropdowns.&lt;BR /&gt;As you can see in &lt;STRONG&gt;code&lt;/STRONG&gt;, I have tried to implement it myself but I &lt;STRONG&gt;can't make it work&lt;/STRONG&gt; as intended. Any &lt;STRONG&gt;selections&lt;/STRONG&gt; in the &lt;STRONG&gt;first&lt;/STRONG&gt; dropdown &lt;STRONG&gt;don't do anything&lt;/STRONG&gt;. Selections in the &lt;STRONG&gt;second&lt;/STRONG&gt; dropdown &lt;STRONG&gt;filter&lt;/STRONG&gt; data in the page to selected value but &lt;STRONG&gt;don't affect&lt;/STRONG&gt; avalible options in the &lt;STRONG&gt;first&lt;/STRONG&gt; dropdown.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Problem 2: &lt;/STRONG&gt;&lt;SPAN&gt;I want to create little &lt;/SPAN&gt;&lt;STRONG&gt;eraser icons&lt;/STRONG&gt;&lt;SPAN&gt; near both dropdowns. &lt;/SPAN&gt;&lt;STRONG&gt;Clicking&lt;/STRONG&gt;&lt;SPAN&gt; them &lt;/SPAN&gt;&lt;STRONG&gt;clears selection&lt;/STRONG&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;STRONG&gt;removes filters&lt;/STRONG&gt;&lt;SPAN&gt; in corresponding dropdown.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Logic&lt;/STRONG&gt; is implemented in &lt;STRONG&gt;visual.ts&lt;/STRONG&gt; : &lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import powerbi from "powerbi-visuals-api";
import "./../style/visual.less";

import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import IVisual = powerbi.extensibility.visual.IVisual;
import DataView = powerbi.DataView;

import * as d3 from "d3";

import { BasicFilter, IFilterColumnTarget } from "powerbi-models";
import IFilter = powerbi.IFilter;
import FilterAction = powerbi.FilterAction;

export class AALCalculatorVisual implements IVisual {

    // Visual elements
    private svgRoot: d3.Selection&amp;lt;SVGElement, {}, HTMLElement, any&amp;gt;;
    private dropdowns: d3.Selection&amp;lt;HTMLSelectElement, {}, HTMLElement, any&amp;gt;[] = [];

    // Parameters
    private visualHost: powerbi.extensibility.visual.IVisualHost;
    private currentDataView: DataView | null = null;
    private postalCode: string = "";
    private lob: string = "";

    constructor(options: VisualConstructorOptions) {
        this.visualHost = options.host;

        this.svgRoot = d3.select(options.element).append("svg")
            .style("background", "rgb(0, 82, 164)");

        // Dropdowns (moved above input fields)
        const dropdownNames = ["Postal Code", "LOB"];
        dropdownNames.forEach((name, index) =&amp;gt; {
            this.svgRoot.append("text")
                .text(name + ":")
                .attr("x", 10)
                .attr("y", 80 + index * 40)
                .style("font-size", "16px")
                .style("fill", "white");

            const dropdown = d3.select(options.element)
                .append("select")
                .attr("class", "dropdown-box")
                .style("position", "absolute")
                .style("top", `${70 + index * 40}px`)
                .style("left", "150px")
                .style("width", "130px")
                .style("padding", "8px")
                .style("border", "1px solid rgb(0, 122, 197)")
                .style("border-radius", "6px")
                .style("background", "white")
                .style("color", "black")
                .on("change", (event) =&amp;gt; this.handleDropdownChange(index, event));

            this.dropdowns.push(dropdown);
        });
    }

    private handleDropdownChange(index: number, event: any): void {
        const value = event.target.value;
    
        if (index === 0) {
            this.postalCode = value;
        } else if (index === 1) {
            this.lob = value;
        }
    
        if (this.currentDataView) {
            this.applyFilter(this.currentDataView);
        }
    }

    public update(options: VisualUpdateOptions) {
        this.currentDataView = options.dataViews[0]; // Save the DataView to a class property
    
        this.svgRoot
            .attr("width", options.viewport.width)
            .attr("height", options.viewport.height);
    
        let dataView: DataView = this.currentDataView;
        let table = dataView.table;
    
        let postalCodeIndex = table.columns.findIndex(col =&amp;gt; col.roles["PostalCode"]);
        let lobIndex = table.columns.findIndex(col =&amp;gt; col.roles["LOB"]);
    
        if (postalCodeIndex !== -1) {
            let postalCodes = Array.from(new Set(table.rows.map(row =&amp;gt; String(row[postalCodeIndex]))));
            this.populateDropdown(this.dropdowns[0], postalCodes, this.postalCode);
        }
    
        if (lobIndex !== -1) {
            let lobs = Array.from(new Set(table.rows.map(row =&amp;gt; String(row[lobIndex]))));
            this.populateDropdown(this.dropdowns[1], lobs, this.lob);
        }

        console.log("DataView Columns: ", dataView.metadata.columns);
    }

    private populateDropdown(dropdown: d3.Selection&amp;lt;HTMLSelectElement, {}, HTMLElement, any&amp;gt;, values: string[], selectedValue: string): void {
        dropdown.selectAll("option").remove();  // Clear existing options
    
        // Preserve existing selection
        let isSelectedValueInList = values.includes(selectedValue);
    
        // Add options dynamically
        values.forEach(value =&amp;gt; {
            dropdown.append("option")
                .text(value)
                .attr("value", value)
                .attr("selected", value === selectedValue ? "selected" : null);  // Keep previous selection
        });
    
        if (!isSelectedValueInList) {
            dropdown.property("value", "");  // Reset only if the selection is invalid
        }
    }

    private applyFilter(dataView: DataView): void {
        if (!this.visualHost) {
            console.error("Visual Host is not initialized properly.");
            return;
        }
    
        const filterValues: string[] = [];
        let targets: IFilterColumnTarget[] = [];
    
        if (this.postalCode) filterValues.push(this.postalCode);
        if (this.lob) filterValues.push(this.lob);
    
        if (dataView?.metadata?.columns) {
            dataView.metadata.columns.forEach(column =&amp;gt; {
                console.log("Checking Column:", column.displayName, "Query Name:", column.queryName);
            
                if (column.displayName === "Postal Code" &amp;amp;&amp;amp; this.postalCode) {
                    targets.push({
                        table: column.queryName.split('.')[0], 
                        column: column.displayName
                    });
                }
                
                if (column.displayName === "LOB" &amp;amp;&amp;amp; this.lob) {
                    targets.push({
                        table: column.queryName.split('.')[0], 
                        column: column.displayName
                    });
                }
            });
            
        }

        console.log("Targets:", targets);
        console.log("Filter Values:", filterValues);
    
        if (targets.length &amp;gt; 0 &amp;amp;&amp;amp; filterValues.length &amp;gt; 0) {
            const filters: IFilter[] = targets.map((target, index) =&amp;gt; 
                new BasicFilter(target, "In", [filterValues[index]])  // Use index instead of shift()
            );
            
    
            filters.forEach(filter =&amp;gt; {
                this.visualHost.applyJsonFilter(filter, "general", "filter", FilterAction.merge);
            });
    
            console.log("Filters Applied: ", filters);
        } else {
            this.visualHost.applyJsonFilter(null, "general", "filter", FilterAction.merge);
            console.log("Filters Cleared.");
        }

    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my &lt;STRONG&gt;capabilities.json&lt;/STRONG&gt;:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
    "dataRoles": [
        {
            "name": "PostalCode",
            "kind": "Grouping",
            "displayName": "Postal Code"
        },
        {
            "name": "LOB",
            "kind": "Grouping",
            "displayName": "LOB"
        }
    ],
    "dataViewMappings": [
        {
            "table": {
                "rows": {
                    "select": [
                        { "for": { "in": "PostalCode" }, "dataReductionAlgorithm": { "singleCategory": {} } },
                        { "for": { "in": "LOB" }, "dataReductionAlgorithm": { "singleCategory": {} } }
                    ]
                }
            },
            "conditions": [
                {
                    "PostalCode": { "max": 1, "min": 0 },   
                    "LOB": { "max": 1, "min": 0 }
                }           
            ]
        }
    ],
    "objects": {
        "general": {
            "displayName": "General",
            "properties": {
                "filter": {
                    "type": {
                        "filter": true
                    }
                }
            }
        }
    },
    "privileges": []
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thank you very much in advance!&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Artem&lt;/P&gt;</description>
    <pubDate>Tue, 18 Mar 2025 22:13:58 GMT</pubDate>
    <dc:creator>Alkatraz</dc:creator>
    <dc:date>2025-03-18T22:13:58Z</dc:date>
    <item>
      <title>Fix Filtering | Custom Visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Fix-Filtering-Custom-Visual/m-p/4615398#M59882</link>
      <description>&lt;P&gt;Dear Community,&lt;BR /&gt;&lt;BR /&gt;I'm developing a little custom visual that consists of &lt;STRONG&gt;two dropdowns&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Problem 1:&lt;/STRONG&gt; Currently my dropdowns are &lt;STRONG&gt;successfully populated&lt;/STRONG&gt; with data and I can &lt;STRONG&gt;select values&lt;/STRONG&gt;. I want to add &lt;STRONG&gt;filtering functionality&lt;/STRONG&gt; to the dropdowns.&lt;BR /&gt;As you can see in &lt;STRONG&gt;code&lt;/STRONG&gt;, I have tried to implement it myself but I &lt;STRONG&gt;can't make it work&lt;/STRONG&gt; as intended. Any &lt;STRONG&gt;selections&lt;/STRONG&gt; in the &lt;STRONG&gt;first&lt;/STRONG&gt; dropdown &lt;STRONG&gt;don't do anything&lt;/STRONG&gt;. Selections in the &lt;STRONG&gt;second&lt;/STRONG&gt; dropdown &lt;STRONG&gt;filter&lt;/STRONG&gt; data in the page to selected value but &lt;STRONG&gt;don't affect&lt;/STRONG&gt; avalible options in the &lt;STRONG&gt;first&lt;/STRONG&gt; dropdown.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Problem 2: &lt;/STRONG&gt;&lt;SPAN&gt;I want to create little &lt;/SPAN&gt;&lt;STRONG&gt;eraser icons&lt;/STRONG&gt;&lt;SPAN&gt; near both dropdowns. &lt;/SPAN&gt;&lt;STRONG&gt;Clicking&lt;/STRONG&gt;&lt;SPAN&gt; them &lt;/SPAN&gt;&lt;STRONG&gt;clears selection&lt;/STRONG&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;STRONG&gt;removes filters&lt;/STRONG&gt;&lt;SPAN&gt; in corresponding dropdown.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Logic&lt;/STRONG&gt; is implemented in &lt;STRONG&gt;visual.ts&lt;/STRONG&gt; : &lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import powerbi from "powerbi-visuals-api";
import "./../style/visual.less";

import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import IVisual = powerbi.extensibility.visual.IVisual;
import DataView = powerbi.DataView;

import * as d3 from "d3";

import { BasicFilter, IFilterColumnTarget } from "powerbi-models";
import IFilter = powerbi.IFilter;
import FilterAction = powerbi.FilterAction;

export class AALCalculatorVisual implements IVisual {

    // Visual elements
    private svgRoot: d3.Selection&amp;lt;SVGElement, {}, HTMLElement, any&amp;gt;;
    private dropdowns: d3.Selection&amp;lt;HTMLSelectElement, {}, HTMLElement, any&amp;gt;[] = [];

    // Parameters
    private visualHost: powerbi.extensibility.visual.IVisualHost;
    private currentDataView: DataView | null = null;
    private postalCode: string = "";
    private lob: string = "";

    constructor(options: VisualConstructorOptions) {
        this.visualHost = options.host;

        this.svgRoot = d3.select(options.element).append("svg")
            .style("background", "rgb(0, 82, 164)");

        // Dropdowns (moved above input fields)
        const dropdownNames = ["Postal Code", "LOB"];
        dropdownNames.forEach((name, index) =&amp;gt; {
            this.svgRoot.append("text")
                .text(name + ":")
                .attr("x", 10)
                .attr("y", 80 + index * 40)
                .style("font-size", "16px")
                .style("fill", "white");

            const dropdown = d3.select(options.element)
                .append("select")
                .attr("class", "dropdown-box")
                .style("position", "absolute")
                .style("top", `${70 + index * 40}px`)
                .style("left", "150px")
                .style("width", "130px")
                .style("padding", "8px")
                .style("border", "1px solid rgb(0, 122, 197)")
                .style("border-radius", "6px")
                .style("background", "white")
                .style("color", "black")
                .on("change", (event) =&amp;gt; this.handleDropdownChange(index, event));

            this.dropdowns.push(dropdown);
        });
    }

    private handleDropdownChange(index: number, event: any): void {
        const value = event.target.value;
    
        if (index === 0) {
            this.postalCode = value;
        } else if (index === 1) {
            this.lob = value;
        }
    
        if (this.currentDataView) {
            this.applyFilter(this.currentDataView);
        }
    }

    public update(options: VisualUpdateOptions) {
        this.currentDataView = options.dataViews[0]; // Save the DataView to a class property
    
        this.svgRoot
            .attr("width", options.viewport.width)
            .attr("height", options.viewport.height);
    
        let dataView: DataView = this.currentDataView;
        let table = dataView.table;
    
        let postalCodeIndex = table.columns.findIndex(col =&amp;gt; col.roles["PostalCode"]);
        let lobIndex = table.columns.findIndex(col =&amp;gt; col.roles["LOB"]);
    
        if (postalCodeIndex !== -1) {
            let postalCodes = Array.from(new Set(table.rows.map(row =&amp;gt; String(row[postalCodeIndex]))));
            this.populateDropdown(this.dropdowns[0], postalCodes, this.postalCode);
        }
    
        if (lobIndex !== -1) {
            let lobs = Array.from(new Set(table.rows.map(row =&amp;gt; String(row[lobIndex]))));
            this.populateDropdown(this.dropdowns[1], lobs, this.lob);
        }

        console.log("DataView Columns: ", dataView.metadata.columns);
    }

    private populateDropdown(dropdown: d3.Selection&amp;lt;HTMLSelectElement, {}, HTMLElement, any&amp;gt;, values: string[], selectedValue: string): void {
        dropdown.selectAll("option").remove();  // Clear existing options
    
        // Preserve existing selection
        let isSelectedValueInList = values.includes(selectedValue);
    
        // Add options dynamically
        values.forEach(value =&amp;gt; {
            dropdown.append("option")
                .text(value)
                .attr("value", value)
                .attr("selected", value === selectedValue ? "selected" : null);  // Keep previous selection
        });
    
        if (!isSelectedValueInList) {
            dropdown.property("value", "");  // Reset only if the selection is invalid
        }
    }

    private applyFilter(dataView: DataView): void {
        if (!this.visualHost) {
            console.error("Visual Host is not initialized properly.");
            return;
        }
    
        const filterValues: string[] = [];
        let targets: IFilterColumnTarget[] = [];
    
        if (this.postalCode) filterValues.push(this.postalCode);
        if (this.lob) filterValues.push(this.lob);
    
        if (dataView?.metadata?.columns) {
            dataView.metadata.columns.forEach(column =&amp;gt; {
                console.log("Checking Column:", column.displayName, "Query Name:", column.queryName);
            
                if (column.displayName === "Postal Code" &amp;amp;&amp;amp; this.postalCode) {
                    targets.push({
                        table: column.queryName.split('.')[0], 
                        column: column.displayName
                    });
                }
                
                if (column.displayName === "LOB" &amp;amp;&amp;amp; this.lob) {
                    targets.push({
                        table: column.queryName.split('.')[0], 
                        column: column.displayName
                    });
                }
            });
            
        }

        console.log("Targets:", targets);
        console.log("Filter Values:", filterValues);
    
        if (targets.length &amp;gt; 0 &amp;amp;&amp;amp; filterValues.length &amp;gt; 0) {
            const filters: IFilter[] = targets.map((target, index) =&amp;gt; 
                new BasicFilter(target, "In", [filterValues[index]])  // Use index instead of shift()
            );
            
    
            filters.forEach(filter =&amp;gt; {
                this.visualHost.applyJsonFilter(filter, "general", "filter", FilterAction.merge);
            });
    
            console.log("Filters Applied: ", filters);
        } else {
            this.visualHost.applyJsonFilter(null, "general", "filter", FilterAction.merge);
            console.log("Filters Cleared.");
        }

    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my &lt;STRONG&gt;capabilities.json&lt;/STRONG&gt;:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
    "dataRoles": [
        {
            "name": "PostalCode",
            "kind": "Grouping",
            "displayName": "Postal Code"
        },
        {
            "name": "LOB",
            "kind": "Grouping",
            "displayName": "LOB"
        }
    ],
    "dataViewMappings": [
        {
            "table": {
                "rows": {
                    "select": [
                        { "for": { "in": "PostalCode" }, "dataReductionAlgorithm": { "singleCategory": {} } },
                        { "for": { "in": "LOB" }, "dataReductionAlgorithm": { "singleCategory": {} } }
                    ]
                }
            },
            "conditions": [
                {
                    "PostalCode": { "max": 1, "min": 0 },   
                    "LOB": { "max": 1, "min": 0 }
                }           
            ]
        }
    ],
    "objects": {
        "general": {
            "displayName": "General",
            "properties": {
                "filter": {
                    "type": {
                        "filter": true
                    }
                }
            }
        }
    },
    "privileges": []
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thank you very much in advance!&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Artem&lt;/P&gt;</description>
      <pubDate>Tue, 18 Mar 2025 22:13:58 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Fix-Filtering-Custom-Visual/m-p/4615398#M59882</guid>
      <dc:creator>Alkatraz</dc:creator>
      <dc:date>2025-03-18T22:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Filtering | Custom Visual</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Fix-Filtering-Custom-Visual/m-p/4615514#M59885</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1241462"&gt;@Alkatraz&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;According to your description, it seems like your requirement is more related to custom visual development. I'd like to suggest you post to the custom visual develop forum to get further support.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/bd-p/CustomVisualsDevelopmentDiscussion" target="_blank"&gt;Custom Visuals Development Discussion - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Xiaoxin Sheng&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 01:22:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Fix-Filtering-Custom-Visual/m-p/4615514#M59885</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-03-19T01:22:29Z</dc:date>
    </item>
  </channel>
</rss>

