Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
Hello, so I am trying to add a filter after the report loads but it gives me this error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Add')
I got the code I am using from the offical documentation from Here , why is it not recognising the Add property?
Here is my embed code for clarification, any help is much appreciated:
<script type="text/javascript">
$(document).ready(async function () {
var accessToken = "@Model.EmbedToken.Token";
// Read embed URL from Model
var embedUrl = "@Html.Raw(Model.EmbedUrl)";
// Read report Id from Model
var embedReportId = "@Model.Id";
var embedUserName = "@Model.Username";
// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;
// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
const userIdFilter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "reports users",
column: "UserName"
},
operator: "In",
values: [embedUserName],
filterType: models.FilterType.BasicFilter,
requireSingleSelection: true
};
var config = {
type: 'report',
tokenType: models.TokenType.Embed,
accessToken: accessToken,
embedUrl: embedUrl,
id: embedReportId,
permissions: models.Permissions.All,
settings: {
filterPaneEnabled: false,
navContentPaneEnabled: true,
}
};
// Get a reference to the embedded report HTML element
var reportContainer = $('#embedContainer')[0];
if ("@Model.Username" != "") {
$("#RLS").prop('checked', true);
$("#RLSdiv").show();
}
else {
$("#RLS").prop('checked', false);
$("#RLSdiv").hide();
}
if ("@Model.IsEffectiveIdentityRequired.GetValueOrDefault()" == "True") {
$("#noRLSdiv").hide();
$("#RLS").removeAttr("disabled");
$("#RLS").change(function () {
if ($(this).is(":checked")) {
$("#RLSdiv").show(300);
} else {
$("#RLSdiv").hide(200);
}
});
}
else {
$("#noRLSdiv").show();
}
// Embed the report and display it within the div container.
var report = powerbi.embed(reportContainer, config);
report.on('loaded', event => {
//if (embedUserName == "tester")
runUpdate();
});
async function runUpdate() {
console.log("run update?");
await report.updateFilters(models.FiltersOperations.Add, userIdFilter);
}
});
</script>
Hi @SoulsSama,
I think this should be related to the parameter that you used in updatefilters function. If you are not familiar with filter structures, you can try to use getfilter function to get the sample data with default filter structures.
Regards,
Xiaoxin Sheng
Hi @v-shex-msft
First of all thank you for your suggestion, I tried getFilters function and logged the response, it is working but I don't see how will this solve my problem in here? I have a custom filter and it works normally when I apply it directly inside my config object, the problem is that I want to add the filter when the report is ( on loaded ), in the documentation Add function is written and I just copied the syntax as it is, but the error says that it's undefined, which means that Add property does not exist for FilterOperations method, I hope this clarify my problem, any more tips and recommendation would be much appreciated.
Here is a screenshot for the code line that I'm trying to implement:
Best Regards,
User | Count |
---|---|
13 | |
5 | |
2 | |
1 | |
1 |
User | Count |
---|---|
24 | |
3 | |
3 | |
2 | |
2 |