Tag: PowerShell

  • Business Ops Update: Bugfix

    Business Ops Update: Bugfix

    Howdy folks!

    We have just published a bugfix release for Business Ops; Version 2.0.2 is now available for download. Please see the instructions below for details.

    Instructions

    If you have installed Business Ops 2.0 or 2.0.1, you should use the “Edit External Tools” menu to remove the External Tools you have installed, then close and uninstall Business Ops.

    Then, just download and install the latest version of Business Ops, and reinstall your favorite External Tools. That’s it!

    What Happened?

    In previous versions of Business Ops, its installation path was C:\Users\<Username>\AppData\Local\Programs\pbi-tips-business-ops\, which could be accessed by any process running on the machine. This struck us as somewhat insecure, so we decided to beef up the security. When we released Business Ops 2.0, we changed the installation path to C:\Program Files\PowerBI.tips Business Ops\, which requires elevated permissions to modify any files inside. This change increased the security of Business Ops, but those of you who are familiar with how file paths work can probably guess how it also had some other, unintended consequences. For those who aren’t, here’s what happened:

    The installation path in previous versions did not have any spaces in it, and everything was working fine. But when we changed the installation path to one that includes spaces, several of the included External Tools in Business Ops stopped working. This wasn’t immediately obvious because it only affected the handful of External Tools which launch a PowerShell script by calling its absolute path. Once we identified the cause of the issue, the fix was pretty straightforward, and we got everything working again.

    Thanks for reading!

    James

    If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials.  Subscribe to our YouTube Channel.  Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.

    Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:

    Check out the new Merch!

    Hasta La Vista Data
    Go Ahead Make My Data
    PBIX Hat
  • Copy Power BI Desktop Server:Port Connection String to Clipboard

    Copy Power BI Desktop Server:Port Connection String to Clipboard

    Howdy, folks!

    A few months ago, I was writing and running various PowerShell scripts to manipulate the connected data models in my Power BI Desktop files. During model development, I was constantly having to open DAX Studio to copy the Server:Port connection string, and thinking, “there’s got to be a faster way to do this.”

    So, I developed and released a simple External Tool for Power BI Desktop, which copies the Server:Port connection string for the currently-connected data model directly to the clipboard.

    I’m a strong believer in modular design, so when I build something, I try to make it do one thing, and do it well. I believe this External Tool for Power BI Desktop is a great example of that philosophy in action.

    This external tool is now in the Business Ops tool from PowerBI.tips.

    Enjoy!

    James

    If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials.  Subscribe to our YouTube Channel.  Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.

    Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:

    Check out the new Merch!

    Hasta La Vista Data
    Go Ahead Make My Data
    PBIX Hat


  • Hot Swap Report Connections – External Tools

    Hot Swap Report Connections – External Tools

    Latest Version Download:

    Download the latest version of Hot Swap Connections using Business Ops 

    Hot Swap Connections

    Splitting models from reports has great advantages, but can make it harder to edit. When editing a model it is useful to see how you it will effect the reports. Using live connections would mean republishing the model back to the cloud and then refreshing the connection for every change you wish to test. In addition, you would probably want to make test workspaces to not overwrite a live production model while developing.

    Now there is an external tool that can help solve these issues. The tool has two functions. The first will allow you to switch from a live connection to directly connecting to an open Power BI report. This will allow “Local Development” so that it can be done on your machine without needing to republish. Changes can be seen instantly and time spent on testing can be dramatically decreased.
    The second will removed any connections to allow to reconnect to a shared dataset or AAS model.

    Instructions to Install

    Please install using the official installer here:
    https://powerbi.tips/2020/08/one-tool-to-install-them-all/

    Using the Hot Swap Connections Tool

    After installing the tool, click external tools the Hot Swap Connections to launch.

    Connect Tab

    This tool will remove any live connections from the selected report and connect it directly to the Power BI report it was launched from. This will only remove live connections so you cannot accidentally delete entire models.

    You can choose between Overwrite and connect or Copy and connect. Selecting Overwrite will directly edit that file by removing the connections and replacing with a live connection to the current file. Selecting copy will leave your file intact and create a copy in the same directory with the suffix defined in the settings tab.
    It will then open the report that is connected to the model file.

    Steps:

    • Open your Model file
    • Select the Connect tab
    • Run Hot Swap Connections
    • Choose to Overwrite or Copy
    • Select Report file to connect

    Remove Tab

    This tool will remove any live connections from the selected report and open the file. This is useful when you have made local edits and want to connect it back to a dataset or analysis services model. This will only remove live connections so you cannot accidentally delete entire models.

    You can choose between Overwrite and remove live connections or Copy and remove live connections. Selecting Overwrite will directly edit that file by removing the connections. Selecting copy will leave your file intact and create a copy in the same directory with the suffix defined in the settings tab.
    It will then open the report that has no connections.

    Steps:

    • Open any Power BI report
    • Select the Remove tab
    • Run Remove Connections
    • Choose to Overwrite or Copy
    • Select Report file to remove connections

    The script will leave all visualizations and report features intact. But, all connections will be removed. When you open the report again in power bi desktop, all visuals will appear broken:

    This is because you have removed all data from the report. Select a new data source to connect the report to. If the new source matches the names of the columns and measures used in the visuals, they will all repopulate.

    Settings Tab

    When selecting Copy and connect or Copy and remove live connections, the tool will create a copy of your report first so you do not directly edit you report file. It will place the copy in the same directory as the original and add a suffix as defined in the settings tab.

    Watch the webinar below

    Steve and Mike talk through the external tool and see it in action!

  • Split an existing Power BI file into a model and report

    Split an existing Power BI file into a model and report

    Shared datasets are a great way to share data models across the organization. This enables users to maintain one source of the truth and increase efficiency. However, models should be built separate to reports. Meaning it can be difficult to transform an existing report into a shared dataset.

    This post describes how to split a pbix file with report and model, into two separate files One for the model and one for the report. This is accomplished using a PowerShell Script. By using this technique any report can quickly be split into a data model file and live connection report file.

    Note: that this script is not officially supported by Microsoft. This code is provided as is without any guarantees. The code will alter the internal files, so please keep a backup if you are unsure of anything.

    Think like the Business, Act like I.T

    Being a great Power BI developer can often mean more than just building visually impressive reports. Focus should be given to efficiency, reusing design and data modelling where possible. In addition, there should be “one source of the truth” – different reports should not have different methods to calculate the same KPI.

    This is accomplished by the creation of Shared Datasets. Users can publish a report that contains no visuals, only a data model. Multiple reports can then be built off this model by using a live connection and use the same data model. The model contains global measures. A Global measure is written and stored in the model file. Then the Global measures are re-used by other reports via the Live Connection. This ensures all reports have the same data model, logic and refresh status.

    There are many reasons you should consider this approach, which is out of scope for this article. If you are less familiar with shared datasets, I encourage you to visit the following resources:

    Think like the Business, Act like I.T user group – global models

    Power BI datasets: A Method to the Madness article

    Planning A Power BI Enterprise Deployment whitepaper DOWNLOAD
    ( Chris Webb and Melissa Coates) – section 9

    Use Cases

    Shared datasets are great, but what if you already have a file with a model and report in one? Currently in order to split a file this would need to be manual, by either copying all the visuals over to a new report (you would also need to re-record any bookmarks, filters, layouts etc. ) or to copy all the Power Query queries (you would then need to set up relationships and re write all measures). This can be time consuming, especially on a large report or model.

    Luckily for you, this code will do all the hard work for you. Simply run the code and select a PBIX file. It will create two new files, a report and a model.

    Running the code

    Right click and select the option Run with PowerShell in the menu.

    A menu will open up. Select the power bi file that you wish to split.

    Click the button OPEN to allow the script to modify your file.

    The script will then create two copies of the file and add the suffix _model and _report. Feel free to rename these, if you desire.

    Publishing to the Service

    Model File

    Open the _model file. It’s a good idea to add some text to the report explaining this is only a model file, so others understand the purpose. Example:

    Publish this file to the desired workspace.

    Report File

    Open the _report file. The script will leave all visualizations and report features intact, but all connections will be removed. When you open the report in power bi desktop, all visuals will appear broken:

    If prompted, make sure you discard changes – this will completely detach the report from the source.


    Click Get Data and Select Power BI Datasets.

    In the pop up window, select the model report you published in the previous step. This will now restore all visuals to display again.

    Publish the report to the desired location.

    That’s it! you can now share your new model file and continue to build reports off it.

    Download the Script

    You can download the script described below

    Download the PowerShell file and save it to your local machine.

    Script

    #This script was designed by Steve Campbell and provided by PowerBI.tips
    #BE WARNED this will alter Power BI files so please make sure you know what you are doing, and always back up your files!
    #This is not supported by Microsoft and changes to future file structures could cause this code to break
    
    #--------------- Released 6/2/2020 ---------------
    #--- By Steve Campbell provided by PowerBI.tips ---
    
    
    #Choose pbix funtion
    Function Get-FileName($initialDirectory)
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
        
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.initialDirectory = $initialDirectory
        $OpenFileDialog.filter = "PBIX (*.pbix)| *.pbix"
        $OpenFileDialog.ShowDialog() | Out-Null
        $OpenFileDialog.filename
    }
    
    
    #Error check function
    function IsFileLocked([string]$filePath){
        Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
        return ($errs.Count -ne 0)
    }
    
    
    #Function to Modify files
    Function Modify-PBIX([string]$inputpath, [string[]]$filestoremove){
    
        #Make temp folder
        $temppth = $env:TEMP  + "\PBI TEMP"
        If(!(test-path $temppth))
        {New-Item -ItemType Directory -Force -Path $temppth}
    
        #Unpackage pbix
        $zipfile = ($inputpath).Substring(0,($inputpath).Length-4) + "zip"
        Rename-Item -Path $inputpath -NewName  $zipfile
                  
        #Initialise object
        $ShellApp = New-Object -COM 'Shell.Application'
        $InputZipFile = $ShellApp.NameSpace( $zipfile )
    
        #Move files to temp
        foreach ($fn in $filestoremove){ 
           $InputZipFile.Items() | ? {  ($_.Name -eq $fn) }  | % {
           $ShellApp.NameSpace($temppth).MoveHere($_)   }  
        }
        
        #Delete temp
        Remove-Item ($temppth) -Recurse
        
        #Repackage 
        Rename-Item -Path $zipfile -NewName $inputpath  
    }
    
    
    
    
    #Choose file
    try {$pathn = Get-FileName}
    catch { "Incompatible File" }
    
    
    #Check for errors
    If([string]::IsNullOrEmpty($pathn )){            
        exit } 
    
    elseif ( IsFileLocked($pathn) ){
        exit } 
    
    #Run Script
    else{    
    
        #set variables
        $modelfiles   = @( 'SecurityBindings', 'Report')
        $reportfiles   = @('Connections','DataModel',  'SecurityBindings')
        
        #Copy files
        $pathf = Get-ChildItem $pathn
        $reportname = [io.path]::GetFileNameWithoutExtension($pathn)
        $model = ($pathf).toString().Replace('.pbix', '_model.pbix')
        $report = ($pathf).toString().Replace('.pbix', '_report.pbix')    
        Copy-Item $pathn -Destination $model
        Copy-Item $pathn -Destination $report
    
        #modify files
        Modify-PBIX $model $modelfiles
        Modify-PBIX $report $reportfiles
        
    }
    
    
    

    Script Usage License

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials.  Subscribe to our YouTube Channel.  Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.

    Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:

    Check out the new Merch!

    Hasta La Vista Data
    Go Ahead Make My Data
    PBIX Hat


  • Hot Swap Power BI Report Connections

    Hot Swap Power BI Report Connections

    Let’s say you have a Power BI file connected to an Analysis Services machine. Then you want to change the data source to PowerBI.com using a Live Connection. Well, you are out of luck. Until NOW!!

    This post describes how to remove a connection from a report using a PowerShell Script. This means any source you can just delete the data source and then re-point your report.

    Note: that this script is not officially supported by Microsoft. This code is provided as is without any guarantees. The code will alter the internal files, so please keep a backup if you are unsure of anything.

    Use Cases

    Image the following diagram. Here we have a Power BI Report Connected to an analysis services model. Now, you wish to re-point the model to a Power BI dataset.

    Note: we assume the analysis services model and the Power BI dataset model have the same definition. Meaning all the columns and measures are the same.

    The below script removes connections Power BI Report.

    Download Script

    Download the Power Shell Script here

    Note: Running it on a file without a live connection will not have an effect on the file.

    Running the code

    Download the PowerShell file and save it to your local machine.

    Right click and select the option Run with PowerShell in the menu.

    A menu will open up. Select the power bi file that you wish to remove the connection from.

    Click the button OPEN to allow the script to modify your file.

    The script will leave all visualizations and report features intact. But, all connections will be removed. When you open the report again in power bi desktop, all visuals will appear broken:

    This is because you have removed all data from the report. Select a new data source to connect the report to. If the new source matches the names of the columns and measures used in the visuals, they will all repopulate.

    How the Code Works

    Internally, PBIX files contain a selection of metadata and media files. The files are zipped and packaged into a pbix file.

    The PowerShell script will alter these internal files, removing the Connections file which holds the information to the live connection.

    It then removes a security file, SecurityBindings, that is necessary to remove in order to not corrupt the file. SecurityBindings will be repopulated the next time you edit and save the file.

    Script

    #This script was designed by Steve Campbell and provided by PowerBI.tips
    #BE WARNED this will alter Power BI files so please make sure you know what you are doing, and always back up your files!
    #This is not supported by Microsoft and changes to future file structures could cause this code to break
    
    #--------------- Released 5/28/2020 ---------------
    #--- By Steve Campbell provided by PowerBI.tips ---
    
    
    #Choose pbix funtion
    Function Get-FileName($initialDirectory)
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
        
        $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $OpenFileDialog.initialDirectory = $initialDirectory
        $OpenFileDialog.filter = "PBIX (*.pbix)| *.pbix"
        $OpenFileDialog.ShowDialog() | Out-Null
        $OpenFileDialog.filename
    }
    #Error check function
    function IsFileLocked([string]$filePath){
        Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
        return ($errs.Count -ne 0)
    }
    
    
    #Choose file
    try {$pathn = Get-FileName}
    catch { "Incompatible File" }
    
    
    #Check for errors
    If([string]::IsNullOrEmpty($pathn )){            
        exit } 
    
    elseif ( IsFileLocked($pathn) ){
        exit } 
    
    #Run Script
    else{    
       
        #Unzip pbix
        [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression')
        $zipfile = $pathn.Substring(0,$pathn.Length-4) + "zip"
        Rename-Item -Path $pathn -NewName  $zipfile
    
        #Delete files
        $files   = 'Connections', 'SecurityBindings'
        $stream = New-Object IO.FileStream($zipfile, [IO.FileMode]::Open)
        $mode   = [IO.Compression.ZipArchiveMode]::Update
        $zip    = New-Object IO.Compression.ZipArchive($stream, $mode)
        ($zip.Entries | ? { $files -contains $_.Name }) | % { $_.Delete() }
    
        #Close zip
        $zip.Dispose()
        $stream.Close()
        $stream.Dispose()
    
        #Repackage and open
        Rename-Item -Path $zipfile -NewName $pathn 
        Invoke-Item $pathn 
    }

    Editing the Code

    The code runs on PowerShell. PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language.

    If you want to view or edit the code, first open PowerShell ISE. We recommended to run as administrator. Afterwards, you can paste the code below into the editor.

    Other Considerations

    The script can be useful when using two power bi files for development and deployment to the PowerBI.com service. In this use case you will have one Power BI that is the bifurcated model and one report that has just report pages. To speed up development you can Open both the Model file and the Report file. The Report file can point to a LocalHost version of the running Analysis Services.

    Here is an example of what this architecture would look like.

    Script Usage License

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials.  Subscribe to our YouTube Channel.  Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.

    Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:

    Check out the new Merch!

    Hasta La Vista Data
    Go Ahead Make My Data
    PBIX Hat