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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
KamleshExusia
Frequent Visitor

How to dynamically bind server name and database name in Power BI embedded report for different user

I have a user name logging into the Web Application.

 

I am using HTML, CSS, JavaScript and ASP.NET webform to run the Web Application with Power BI embedded reports.

 

One user is in US so he will have access to US SQL server and second user is EU so he weill have access to EU SQL server.

 

Workspace will have one Power BI embedded report.

 

And based on application user it will access different data source i.e. US / EU SQL server to show respective report data.

 

My questiion is whether this is possible or not?

 

If yes,

 

then 

 

How to code to to pass Server Name and Database name based on the user logged in to Power BI embedded config to show Power BI Embedded report with respective report data i.e. US/EU SQL server data.

 

Tried to test the application flow using following things, but the Power BI Report is not getting loaded in HTML div element. Its giving "Power BI Loading logo" in the embed div and then showing message as "Power BI content is not avaialble"

Application Flow is:

 

  1. Power BI workspace has One Report and Two Datasets

  2. Following is the Token generation code in .NET

    // Generate an embed token and populate embed variables
                using (var client = new PowerBIClient(new Uri(Configurations.ApiUrl), Authentication.GetTokenCredentials()))
                //using (var client = new PowerBIClient(new Uri(Configurations.ApiUrl), Authentication.m_tokenCredentials))
                {
                    var report = client.Reports.GetReportInGroup(new Guid(Configurations.WorkspaceId), new Guid(ddlReport.SelectedValue));
                                                               
                    var rls = new EffectiveIdentity(username: appLoginUserName, new List<string> { userDatasetId.ToString() });
                    
                    // Effective Identity
                    var rolesList = new List<string>();
                    rolesList.Add("Tenant");
                    rls.Roles = rolesList;
    
                    // Create list of datasets
                    var v2DatasetID = new List<Guid>();
                    v2DatasetID.Add(userDatasetId);
    
                    // Create list of Effective Identities
                    var v2rls = new List<EffectiveIdentity>();
                    v2rls.Add(rls);
    
                    // Create a request for getting Embed token 
                    // This method works only with new Power BI V2 workspace experience
                    var tokenRequest = new GenerateTokenRequestV2(
    
                        reports: new List<GenerateTokenRequestV2Report>() { new GenerateTokenRequestV2Report(report.Id) },
    
                        datasets: v2DatasetID.Select(datasetId => new GenerateTokenRequestV2Dataset(datasetId.ToString())).ToList(),
    
                        identities: v2rls,
    
                        targetWorkspaces: null
    
    
                    );
    
                    // Generate Embed token
                    var getToken = client.EmbedToken.GenerateToken(tokenRequest);
                    
    
                    // Populate embed variables (to be passed client-side)
                    //embedToken = tokenResponse.Token;
                    embedToken = getToken.ToString();
                    embedUrl = report.EmbedUrl;
                    reportId = report.Id;
    
                }

     

  3. Following is the Power BI embed configuration with dynamic dataset: 
<script>
            // Read embed token
            var embedToken = "<% =this.embedToken %>";

            // Read embed URL
            var embedUrl = "<% = this.embedUrl %>";

            // Read report Id
            var reportId = "<% = this.reportId %>";

            // Read dataset Id
            var userDatasetId = "<% = this.userDatasetId %>";

            // Get models (models contains enums)
            var models = window['powerbi-client'].models;

            // Embed configuration is used to describe what and how to embed
            // This object is used when calling powerbi.embed
            // It can also includes settings and options such as filters
            var config = {
                type: 'report',
                tokenType: models.TokenType.Embed,
                accessToken: embedToken,
                embedUrl: embedUrl,
                id: reportId,   
                datasetId: userDatasetId, // The dataset id that you want the report to use 
                settings: {
                    filterPaneEnabled: true,
                    navContentPaneEnabled: true,
                    extensions: [
                        {
                            command: {
                                name: "cmdShowValue",
                                title: "Show Value in MessageBox",
                                selector: {
                                    $schema: "http://powerbi.com/product/schema#visualSelector",
                                    visualName: "VisualContainer7" // Sales and Avg Price by Month visual
                                },
                                extend: {
                                    visualContextMenu: {
                                        title: "Show Value in MessageBox"
                                    }
                                }
                            }
                        }
                    ]

                }
            };

            // Embed the report within the div element
            var report = powerbi.embed(embedDiv, config);
            
        </script>
2 REPLIES 2
v-lionel-msft
Community Support
Community Support

Hi @KamleshExusia ,

 

Please refer to the link.

"When a report is connected to a dataset, you can use dynamic binding. The connection between the report and the dataset, is known as binding. When the binding is determined at the point of embedding, as opposed to being predetermined earlier, the binding is known as dynamic binding.

When embedding a Power BI report using dynamic binding, you can connect the same report to different datasets depending on the user's credentials.

This means that you can use one report to display different information, depending on the dataset it's connected to. For example, a report showing retail sale values can be connected to different retailer datasets, and produce different results, depending on the dataset of the retailer it's connected to."

Connect a Power BI report to a dataset using dynamic binding - Power BI | Microsoft Docs

 

 

Best regards,
Lionel Chen

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

Thank you @v-lionel-msft for your reply.

 

I am trying this option (https://docs.microsoft.com/en-us/javascript/api/overview/powerbi/bind-report-datasets) and will update this topic once done.

 

Just one concern around having One Report and Two Data Sets.

 

Will token generation work if my report is in Workspace1 and datasets are in Workspace 2?

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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