Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Filtering an Embedded Power BI Report via HTML inputs

Hi. I have a React web application with a power bi report embedded in it. Is there a way to filter data in a report programatically by using an html input text box in the web application?

  • Sahir_Maharaj's avatar
    Sahir_Maharaj
    3 years ago

    Here is a code example I have written in React:

     

    import React, { useState, useEffect } from 'react';
    import * as pbi from 'powerbi-client';
    
    const Report = () => {
      const [report, setReport] = useState(null);
      const [inputValue, setInputValue] = useState('');
    
      useEffect(() => {
        const embedConfiguration = {
          type: 'report',
          id: '<REPORT_ID>',
          embedUrl: '<REPORT_EMBED_URL>',
          accessToken: '<ACCESS_TOKEN>',
          tokenType: pbi.models.TokenType.Aad
        };
    
        const powerbi = new pbi.Service(
          pbi.factories.hpmFactory,
          pbi.factories.wpmpFactory,
          pbi.factories.routerFactory
        );
    
        const reportContainer = document.getElementById('reportContainer');
        const report = powerbi.embed(reportContainer, embedConfiguration);
        setReport(report);
      }, []);
    
      const handleInputChange = (event) => {
        setInputValue(event.target.value);
      };
    
      const applyFilter = () => {
        if (report) {
          report.getFilters()
            .then((filters) => {
              const newFilters = [{
                ...filters,
                $schema: "http://powerbi.com/product/schema#basic",
                target: {
                  table: "<TABLE_NAME>",
                  column: "<COLUMN_NAME>"
                },
                operator: "In",
                values: [inputValue]
              }];
    
              report.setFilters(newFilters)
                .catch((error) => console.error(error));
            })
            .catch((error) => console.error(error));
        }
      };
    
      return (
        <div>
          <input type="text" value={inputValue} onChange={handleInputChange} />
          <button onClick={applyFilter}>Filter</button>
          <div id="reportContainer" />
        </div>
      );
    };
    
    export default Report;

18 Replies

  • Hello Anonymous,

     

    Yes, you can programmatically filter data in a Power BI report that is embedded in a React web application. You can use the Power BI JavaScript API to interact with the embedded report and apply filters. Here is an outline of the steps:

    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      3. Listen to the "change" event of the input text box and retrieve its value.

    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      Here is a code example I have written in React:

       

      import React, { useState, useEffect } from 'react';
      import * as pbi from 'powerbi-client';
      
      const Report = () => {
        const [report, setReport] = useState(null);
        const [inputValue, setInputValue] = useState('');
      
        useEffect(() => {
          const embedConfiguration = {
            type: 'report',
            id: '<REPORT_ID>',
            embedUrl: '<REPORT_EMBED_URL>',
            accessToken: '<ACCESS_TOKEN>',
            tokenType: pbi.models.TokenType.Aad
          };
      
          const powerbi = new pbi.Service(
            pbi.factories.hpmFactory,
            pbi.factories.wpmpFactory,
            pbi.factories.routerFactory
          );
      
          const reportContainer = document.getElementById('reportContainer');
          const report = powerbi.embed(reportContainer, embedConfiguration);
          setReport(report);
        }, []);
      
        const handleInputChange = (event) => {
          setInputValue(event.target.value);
        };
      
        const applyFilter = () => {
          if (report) {
            report.getFilters()
              .then((filters) => {
                const newFilters = [{
                  ...filters,
                  $schema: "http://powerbi.com/product/schema#basic",
                  target: {
                    table: "<TABLE_NAME>",
                    column: "<COLUMN_NAME>"
                  },
                  operator: "In",
                  values: [inputValue]
                }];
      
                report.setFilters(newFilters)
                  .catch((error) => console.error(error));
              })
              .catch((error) => console.error(error));
          }
        };
      
        return (
          <div>
            <input type="text" value={inputValue} onChange={handleInputChange} />
            <button onClick={applyFilter}>Filter</button>
            <div id="reportContainer" />
          </div>
        );
      };
      
      export default Report;
    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      1. Embed the Power BI report in your React application using the Power BI JavaScript API.

  • Another thing to keep in mind is that Power BI offers both client-side and server-side embedding options. When embedding a report in a React web application, you can choose to either embed the report directly on a client-side component, or you can embed the report on a server-side component and have it rendered in the client-side component. Each option has its own advantages and disadvantages, and the choice will depend on your specific use case and requirements.

  • If you are interested in server-side embedding, you can make use of the Power BI .NET Client Library, which provides a convenient way to interact with the Power BI REST API from .NET applications.

    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      This can be especially useful for filtering reports programmatically as it provides a more secure and scalable solution compared to client-side embedding.

    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      In any case, I would recommend thoroughly testing the solution in a development environment before deploying it to production, to ensure that it meets your requirements and is working as expected.

  • If you have any other questions or if there is anything else I can assist with, please let me know.

  • It's also a good idea to monitor the performance of the report to ensure that it remains fast and responsive even as the volume of data grows over time.

    • Sahir_Maharaj's avatar
      Sahir_Maharaj
      Super User

      Power BI provides several performance monitoring and optimization tools that can help you identify and resolve performance issues.