Forum Discussion

apaksoy's avatar
apaksoy
Frequent Visitor
1 year ago
Solved

How to keep DAX Studio connection credentials between Power BI and Excel unchanged?

I have been able to establish a connection between power bi desktop (v2.138.1452.0 64-bit) and excel (v2501 of microsoft 365 insider beta channel) using dax studio (v3.2.1) as shown in the attached screenshot. The query I created on the excel side depends on an Analysis Services Database connection. I launch dax studio either from within power bi desktop or excel and it works.

 

 

However, the number within the database name (such as the 50589 of localhost:50589) and the database ID (such as 1a88654c-2a123-1234-b4b0-91617abcde2f) for the server connected by dax studio changes whenever I launch a new dax studio session after restarting excel or power bi. Then, I have to go and edit the source for the query on the excel side with a new database number and ID. I have tried saving one of the dax studio sessions as a .dax file and opening it after launching dax studio either standalone (after opening power bi desktop) or from within power bi or excel, but it does not help.

 

Is it possible to have a constant database name and ID for the connection established by dax studio in between power bi and excel, at least whenever the same power bi file is concerned? If not, what could be a practical alternative to my updating the connection details each and every time on the excel side while continuing to use the same excel power query and have it refreshed conveniently whenever needed?

 

P.S. This may not be a common problem among most users of both power bi and excel, but it is for me because microsoft appears to require a work email for the native connection between power bi and excel to work and I don't work anywhere. The same issue prevents me from trying a potential solution via a microsoft app called “On-Premises Data Gateway”.

  • Hi again apaksoy 

    No problem, and thanks for clarifying things 🙂

    My example assumed a live Analysis Services connection to a local model in Power BI Desktop. However, you are actually importing using Power Query's Analysis Services connector.

     

    Method 1:

    Assuming you have ensured that the port number is fixed to a known value, change the "Database ID" step of your query from:

     

     

    #"Database ID" = Source{[Name="8ddb886a-cd8a-4299-80c4-5b5d6eef422a"]}[Data],

     

    to

     

    #"Database ID" = Source{0}[Data],

     

     

    This relies on the fact that a local model appears as a server with a single Analysis Services database, which we can identify by index (zero) rather than name. 

     

    Method 2:

    You can use methods similar to those described here to locate the port number by examining files saved on the local drive that are generated by Power BI Desktop. Each instance of Power BI Desktop creates a folder structure which contains files including msmdsrv.port.txt which contains the port number as text.

     

    This method is a bit riskier as it relies on identifying the correct path of the file and selecting the correct folder.

     

    Here is an example I got working on my machine (XLSX attached also).

    • GetPowerBIPort returns the port number.
    • PowerBIModel contains the initial steps to connect to the model.

     

    // ============================================
    // GetPowerBIPort
    // ============================================
    // The below code assumes that the ports are contained in files
    // C:\Users\%user%\Microsoft\Power BI Desktop Store App\AnalysisServicesWorkspaces\
    let
        IgnoreFolder = {"Default","Default User","Public","All Users"},
        UsersSubfolderPath = "\Microsoft\Power BI Desktop Store App\AnalysisServicesWorkspaces",
        UserFolders = Table.SelectRows (Folder.Contents("C:\Users"), each [Attributes][Kind]="Folder" and not List.Contains(IgnoreFolder,[Name]) ),
        // Assume current user is the one with latest [Date accessed] folder in C:\Users
        CurrentUserFolder = Table.MaxN(
          UserFolders,
            each [Date accessed],
            1
        ),
        CurrentUser = CurrentUserFolder[Name]{0},                        
    //Read the contents of file msmdsrv.port.txt from subfolder of AnalysisServicesWorkspaces
        Folders = 
        Table.SelectRows(
        Folder.Contents("C:\Users\" & CurrentUser & UsersSubfolderPath),
        each Text.StartsWith([Name],"AnalysisServicesWorkspace")),
        AddFiles = Table.AddColumn(Folders, "Port", each Table.SelectRows([Content]{[Name="Data"]}[Content], each [Name]="msmdsrv.port.txt")),
        FilesBinary = Table.ExpandTableColumn(AddFiles, "Port", {"Content", "Date accessed"}, {"Port.Content", "Port.Date accessed"}),
        #"Removed Other Columns" = Table.SelectColumns(FilesBinary,{"Port.Content", "Port.Date accessed"}),
        LatestFile = Table.Max(#"Removed Other Columns",each [Port.Date accessed] ),
        PortNumber = Lines.FromBinary(LatestFile[Port.Content],null,null,1200){0}
    in
        PortNumber
    
    // ============================================
    // PowerBIModel
    // ============================================
    let
        CurrentPort = GetPowerBIPort,
        Source = AnalysisServices.Databases("localhost:" & CurrentPort, [TypedMeasureColumns=true, Implementation="2.0"]),
        Database = Source{0}[Data],
        Model1 = Database{[Id="Model"]}[Data],
        Model2 = Model1{[Id="Model"]}[Data]
    in
        Model2

     

     

     

4 Replies

  • Hi apaksoy 

    If you are wanting to connect Excel to a local model open in Power BI Desktop, the challenge is that port number (the number after localhost:) and the database name change in a random fashion each time Power BI Desktop is launched (as you've described).

     

    Here is what you can do to work around this:

    1. Open the Power BI model by starting Power BI Desktop from the command line with the /diagnosticsport: command line option, which allows you to specify a port number. From memory, this port number must be 4 digits (or possibly less) but not 5 digits.
    For my installation of Power BI Desktop for an example PBIX file, after launching CMD in the Power BI Desktop installation folder, the command looks like this:

     

     

    pbidesktop /diagnosticsport:1680 C:\Temp\GameData.pbix

     

     

    2. Connect Excel to localhost:XXXX (e.g. localhost:1680 in my example). In the course of connecting, you will be forced to choose the database (as you're aware):

     

    3. In Excel, you have created the connection, modify the connection string found under Data > Queries & Connections > Connections > choose connection > Properties > Definition by removing the text

     

     

    Initial Catalog=xxxxxxxx-xxxx-xxxxxxxx-xxxxxxxxxxxx;

     

     

    Original:

     

    Modified:

     

    4. Now, as long as the model is opened each time by launching Power BI Desktop with the /diagnosticsport: option specifying the same port number, Excel will connect without problem.

     

    Does this work for you?

     

    • apaksoy's avatar
      apaksoy
      Frequent Visitor

      Thx very much OwenAuger.  Your answer was quite specific and helpful. Thank you for explaining it clearly.

       

      However, while the port number is maintained with the procedure you suggest, whenever I relaunch power bi and dax studio, I get an error message in excel saying (in the local language) that the user does not have access to the specified database or the database does not exist when I try to refresh the query.

       

      Then, I need to enter the new database ID created by dax studio using the query editor on the excel side. This happens even though I recreated the connection from scratch and modified the connection string within the connection properties as you suggested. Not sure if it makes any difference but the connection string in my case is slightly different than the one in your screenshots both before and after the modification as shown in the screenshot below (after modification).

       

       

      Also below is the code in m formula language that underlies the query I run on the excel side:

      let
          Source = AnalysisServices.Databases("localhost:4900", [TypedMeasureColumns=true, Implementation="2.0"]),
          #"Database ID" = Source{[Name="8ddb886a-cd8a-4299-80c4-5b5d6eef422a"]}[Data],
          Model1 = #"Database ID"{[Id="Model"]}[Data],
          Model2 = Model1{[Id="Model"]}[Data],
          #"Added Items" = Cube.Transform(Model2,
              {
                  {Cube.AddAndExpandDimensionColumn, "[Fon Listesi]", {"[Fon Listesi].[Count].[Count]", "[Fon Listesi].[Fon Adı].[Fon Adı]", "[Fon Listesi].[Fon Kodu].[Fon Kodu]"}, {"Fon Listesi.Count", "Fon Listesi.Fon Adı", "Fon Listesi.Fon Kodu"}}
              }),
          #"Removed Columns" = Table.RemoveColumns(#"Added Items",{"Fon Listesi.Count"}),
          #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Fon Listesi.Fon Adı", "Fon Adı"}, {"Fon Listesi.Fon Kodu", "Fon Kodu"}}),
          #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Fon Kodu", "Fon Adı"})
      in
          #"Reordered Columns"

       

      • OwenAuger's avatar
        OwenAuger
        Icon for Super User rankSuper User

        Hi again apaksoy 

        No problem, and thanks for clarifying things 🙂

        My example assumed a live Analysis Services connection to a local model in Power BI Desktop. However, you are actually importing using Power Query's Analysis Services connector.

         

        Method 1:

        Assuming you have ensured that the port number is fixed to a known value, change the "Database ID" step of your query from:

         

         

        #"Database ID" = Source{[Name="8ddb886a-cd8a-4299-80c4-5b5d6eef422a"]}[Data],

         

        to

         

        #"Database ID" = Source{0}[Data],

         

         

        This relies on the fact that a local model appears as a server with a single Analysis Services database, which we can identify by index (zero) rather than name. 

         

        Method 2:

        You can use methods similar to those described here to locate the port number by examining files saved on the local drive that are generated by Power BI Desktop. Each instance of Power BI Desktop creates a folder structure which contains files including msmdsrv.port.txt which contains the port number as text.

         

        This method is a bit riskier as it relies on identifying the correct path of the file and selecting the correct folder.

         

        Here is an example I got working on my machine (XLSX attached also).

        • GetPowerBIPort returns the port number.
        • PowerBIModel contains the initial steps to connect to the model.

         

        // ============================================
        // GetPowerBIPort
        // ============================================
        // The below code assumes that the ports are contained in files
        // C:\Users\%user%\Microsoft\Power BI Desktop Store App\AnalysisServicesWorkspaces\
        let
            IgnoreFolder = {"Default","Default User","Public","All Users"},
            UsersSubfolderPath = "\Microsoft\Power BI Desktop Store App\AnalysisServicesWorkspaces",
            UserFolders = Table.SelectRows (Folder.Contents("C:\Users"), each [Attributes][Kind]="Folder" and not List.Contains(IgnoreFolder,[Name]) ),
            // Assume current user is the one with latest [Date accessed] folder in C:\Users
            CurrentUserFolder = Table.MaxN(
              UserFolders,
                each [Date accessed],
                1
            ),
            CurrentUser = CurrentUserFolder[Name]{0},                        
        //Read the contents of file msmdsrv.port.txt from subfolder of AnalysisServicesWorkspaces
            Folders = 
            Table.SelectRows(
            Folder.Contents("C:\Users\" & CurrentUser & UsersSubfolderPath),
            each Text.StartsWith([Name],"AnalysisServicesWorkspace")),
            AddFiles = Table.AddColumn(Folders, "Port", each Table.SelectRows([Content]{[Name="Data"]}[Content], each [Name]="msmdsrv.port.txt")),
            FilesBinary = Table.ExpandTableColumn(AddFiles, "Port", {"Content", "Date accessed"}, {"Port.Content", "Port.Date accessed"}),
            #"Removed Other Columns" = Table.SelectColumns(FilesBinary,{"Port.Content", "Port.Date accessed"}),
            LatestFile = Table.Max(#"Removed Other Columns",each [Port.Date accessed] ),
            PortNumber = Lines.FromBinary(LatestFile[Port.Content],null,null,1200){0}
        in
            PortNumber
        
        // ============================================
        // PowerBIModel
        // ============================================
        let
            CurrentPort = GetPowerBIPort,
            Source = AnalysisServices.Databases("localhost:" & CurrentPort, [TypedMeasureColumns=true, Implementation="2.0"]),
            Database = Source{0}[Data],
            Model1 = Database{[Id="Model"]}[Data],
            Model2 = Model1{[Id="Model"]}[Data]
        in
            Model2