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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
fatgreycat
Frequent Visitor

Set Relative Date Slicer value upon load Javascript API

Hi all!,

 

Im attempting to set a slicer value upon report load. According to this documentation this can be done:

 

https://github.com/Microsoft/PowerBI-JavaScript/wiki/Slicers

 

Could someone please check my code below to see if i'm missing something or help confirm that this functionality is not working?

 

 

        var filter = new models.RelativeDateFilter(
            {
            table: "D_DATE",
            column: "DATE_VALUE"
            },
            models.RelativeDateOperators.InLast,
            30,
            models.RelativeDateFilterTimeUnit.Days,
            true
        );

        //relative date
        slicerObj.push({
            selector: new models.VisualSelector("a0625ab0110ec10492e3"),
            state: filter
        });


        var config = {
            type: 'report',
            tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            pageName: summaryPage,
            embedUrl: embedUrl,
            filters: basicFilter,
            slicers: slicerObj, //THIS DOESN'T DO ANYTHING?
            id: embedReportId,
            permissions: models.Permissions.All,
            settings: {
                filterPaneEnabled: true,
                navContentPaneEnabled: true
            }
        }; 

var reportContainer = $('#reportContainer')[0];
var report = powerbi.embed(reportContainer, config);

 

1 ACCEPTED SOLUTION
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @fatgreycat,

 

There aren't any issues. I made it work with the following code finally. (the main part is as follows.)

var models = window['powerbi-client'].models;

const slicer_filter = 
    {
        selector: new models.VisualSelector("97cc6177cbc5e4a48834"),
        state: {
            filters: [
                {
                    "$schema": "http://powerbi.com/product/schema#basic",
                    "target": {
                        "table": "DimProduct",
                        "column": "ColorName"
                    },
                    "operator": "In",
                    "values": [
                        "Blue"
                    ]
                }
            ]
        }
    };

var config= {
    type: 'report',
    tokenType: tokenType == '0' ? models.TokenType.Aad : models.TokenType.Embed,
    accessToken: txtAccessToken,
    embedUrl: txtEmbedUrl,
    id: txtEmbedReportId,
    slicers: [slicer_filter],
    permissions: permissions
};

var embedContainer = $('#container')[0];
var report = powerbi.embed(embedContainer, config);

So please check out these things below.

1. The format of the filter,

2. The slicer is an array.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @fatgreycat,

 

Could you please mark the proper answers as solutions?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @fatgreycat,

 

There aren't any issues. I made it work with the following code finally. (the main part is as follows.)

var models = window['powerbi-client'].models;

const slicer_filter = 
    {
        selector: new models.VisualSelector("97cc6177cbc5e4a48834"),
        state: {
            filters: [
                {
                    "$schema": "http://powerbi.com/product/schema#basic",
                    "target": {
                        "table": "DimProduct",
                        "column": "ColorName"
                    },
                    "operator": "In",
                    "values": [
                        "Blue"
                    ]
                }
            ]
        }
    };

var config= {
    type: 'report',
    tokenType: tokenType == '0' ? models.TokenType.Aad : models.TokenType.Embed,
    accessToken: txtAccessToken,
    embedUrl: txtEmbedUrl,
    id: txtEmbedReportId,
    slicers: [slicer_filter],
    permissions: permissions
};

var embedContainer = $('#container')[0];
var report = powerbi.embed(embedContainer, config);

So please check out these things below.

1. The format of the filter,

2. The slicer is an array.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-jiascu-msft
Microsoft Employee
Microsoft Employee

Hi @fatgreycat,

 

I have consulted some experts. I will update here when I get the answer.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Update the report ID: CRI 79433674.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Dale,

 

Sorry I don't understand what you are asking me to do?

Hi @fatgreycat,

 

Sorry for the confusion. It's a reference to other teams. You don't need to do anything.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.