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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
v-vaaga
Frequent Visitor

Can we add report level filter in power bi visuals from code (JavaScript)

Can we add report level filter in power bi visuals from code (JavaScript)

6 REPLIES 6
Eric_Zhang
Employee
Employee


@v-vaaga wrote:

Can we add report level filter in power bi visuals from code (JavaScript)


@v-vaaga

Yes.

You can check the JavaScript wiki Filters.

 

Here's a simple demo to practise.

<html>

 <script src="https://microsoft.github.io/PowerBI-JavaScript/demo/bower_components/powerbi-client/dist/powerbi.js"></script>
 <script src="https://microsoft.github.io/PowerBI-JavaScript/demo/bower_components/jquery/dist/jquery.js"></script>  
  
<script type="text/javascript">
window.onload = function () { 

  var  Filter = {
   $schema: "http://powerbi.com/product/schema#advanced",
  target: {
    table: "table Name",
    column: "column Name"
  },
  logicalOperator: "OR",
  conditions: [
    {
      operator: "Contains",
      value: "CN"
    },
    {
      operator: "Contains",
      value: "CO"
    }
  ]
}
 

var embedConfiguration = {
    type: 'report',
    accessToken: 'eyJ..YourTokenHere..w',
    id: 'report ID',
    embedUrl: 'yourEmbedUrlHere',

}; 
 

var $reportContainer = $('#reportContainer');
 
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
 
report.on('loaded', event => {
  report.getFilters()
    .then(filters => {
      filters.push(Filter);
      return report.setFilters(filters);
    });
});

}
</script>

<div id="reportContainer" powerbi-settings-nav-content-pane-enabled="true"   powerbi-settings-filter-pane-enabled="true"></div>

</html>

Hi @Eric_Zhang,

 

I tried your solution but when my application try to load report, it shows "The Content is not available", But when I load report using  @Html.PowerBIReportFor(m => m.Report, new { id = "pbi-report", style = "height:85vh", powerbi_access_token = Model.AccessToken }) with filter, It getting loadded.

Is I am doing something wrong?

Pleae help


@bhanupowerbi wrote:

Hi @Eric_Zhang,

 

I tried your solution but when my application try to load report, it shows "The Content is not available@", But when I load report using  @Html.PowerBIReportFor(m => m.Report, new { id = "pbi-report", style = "height:85vh", powerbi_access_token = Model.AccessToken }) with filter, It getting loadded.

Is I am doing something wrong?

Pleae help


 

"The Content is not available" is usually due to an invalid token. What do you get from the network traffic in a dev tool(F12 in Chrome).

Thanks. This approach works fine if we are embedding PowerBI visuals into our html or aspx page. 

But my requirement is to make it work in online PowerBI (msit.powerbi.com/groups/Some-Report-GUID). I have my custom visual and its corresponding JS. So there I need to add report level filter from code (JavaScript) i.e., without dragging and dropping that filter column.

@v-vaaga

I'm not aware of any way, where do you put the code?

Greg_Deckler
Super User
Super User

Not to my knowledge.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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