Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago

Power BI Embedded - Filters

Hello All,

                Greetings, I had been created an Alert Data report with two parameters such as Start Date and End Date, the scenario of the report is that the user will first select the Start Date and End Date, and according to that date range the report will be displayed for that particular data. The report was working fine and I had published it to the Power BI Service too and there also the parameters are perfectly working.

 
              I had embedded that report into an Asp.Net MVC application using one of the Power BI developer samples [App Owns Data] in GitHub. The report had been embedded and working fine. The issue is, the parameters are not working in the Power BI Embedded. In the web app I had created 2 list boxes for the Start Date and End Date and a button, the report will be displayed once the user selects the dates from the list boxes and clicks the button. I have used the javascript to set the filters for the parameters but the filters are not reflected in the report.
 
Requesting for a solution or suggestion to accomplish this scenario. I have attached the code for reference.
 
const filter1 =
            {
                $schema: "http://powerbi.com/product/schema#advanced",
                target: 
                {
                    table: "Alert",
                    column: "Date"
                },
                logicalOperator: "And",
                conditions: 
                [
                    {
                        operator: "GreaterThanOrEqual",
                        value: sd
                    },
                    {
                        operator: "LessThanOrEqual",
                        value: ed
                    }
                ],
            };

            var reportContainer = $('#reportContainer')[0];

            var report = powerbi.embed(reportContainer, config);

            //report.on('loaded', event => { report.getFilters().then(filters => { filters.push(filter1); return report.setFilters(filters); }) });

            report.setFilters([filter1]);

6 Replies

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi Anonymous,

     

    I would suggest you try the online demo here. You also can download the source code. It could be like below.

    // Build the filter you want to use. For more information, See Constructing
    // Filters in https://github.com/Microsoft/PowerBI-JavaScript/wiki/Filters.
    const filter = {
      $schema: "http://powerbi.com/product/schema#basic",
      target: {
        table: "Store",
        column: "Chain"
      },
      operator: "In",
      values: ["Fashions Direct"]
    };
    
    // Get a reference to the embedded report HTML element
    var embedContainer = $('#embedContainer')[0];
    
    // Get a reference to the embedded report.
    report = powerbi.get(embedContainer);
    
    // Retrieve the page collection and get the visuals for the first page.
    report.getPages()
      .then(function (pages) {
    
        // Retrieve active page.
        var activePage = pages.find(function(page) {
          return page.isActive
        });
    
        activePage.getVisuals()
          .then(function (visuals) {
    
            // Retrieve the wanted visual.
            var visual = visuals.find(function(visual) {
              return visual.name == "VisualContainer3";
            });
    
            // Set the filter for the visual.
            // Pay attention that setFilters receives an array.
            visual.setFilters([filter])
                .catch(function (errors) {
                    Log.log(errors);
                });
            })
            .catch(function (errors) {
                Log.log(errors);
            });
      })
      .catch(function (errors) {
          Log.log(errors);
      });

    Best Regards,

    Dale

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hai v-jiascu-msft thank you for your response, I had already tried the online demo but yet I couldn't able to find the bug.

       

      Kind Regards,

      Geetha

      • v-jiascu-msft's avatar
        v-jiascu-msft
        Microsoft Employee

        Hi Geetha,

         

        What is the type of the filter? If it's a visual level filter, we should get the report first and then get the page and the visual. At last we can set the filter. 

        // Build the filter you want to use. For more information, See Constructing
        // Filters in https://github.com/Microsoft/PowerBI-JavaScript/wiki/Filters.
        const filter = {
          $schema: "http://powerbi.com/product/schema#basic",
          target: {
            table: "Store",
            column: "Chain"
          },
          operator: "In",
          values: ["Fashions Direct"]
        };
        
        // Get a reference to the embedded report HTML element
        var embedContainer = $('#embedContainer')[0];
        
        // Get a reference to the embedded report.
        report = powerbi.get(embedContainer);
        
        // Retrieve the page collection and get the visuals for the first page.
        report.getPages()
          .then(function (pages) {
        
            // Retrieve active page.
            var activePage = pages.find(function(page) {
              return page.isActive
            });
        
            activePage.getVisuals()
              .then(function (visuals) {
        
                // Retrieve the wanted visual.
                var visual = visuals.find(function(visual) {
                  return visual.name == "VisualContainer3";
                });
        
                // Set the filter for the visual.
                // Pay attention that setFilters receives an array.
                visual.setFilters([filter])
                    .catch(function (errors) {
                        Log.log(errors);
                    });
                })
                .catch(function (errors) {
                    Log.log(errors);
                });
          })
          .catch(function (errors) {
              Log.log(errors);
          });

        Best Regards,

        Dale

  • Anonymous's avatar
    Anonymous
    Not applicable

    Any solutions to this question...

     

    Regards,

    Geetha