Power BI & Paginated Reports - Export & Email Automation
After publishing a Power BI report, users may wish to email filtered versions of the report to different users (including 3rd party as well). For instance, in a scenario where a company needs to send a report to hundreds of clients, each with different filters applied. The traditional method of achieving this involves manually changing filter values in the Power BI report, saving the report as a PDF and then sharing it via email. However, this approach can be extremely time-consuming and inefficient, especially when you need to send the report on a daily or weekly basis to multiple users!
Following are the available options that one can utilize to automate the above process:
1. Power BI Report Screenshot Automation
2. Exporting Power BI Paginated Reports
To automate the process of emailing reports to different users, Python can be used to configure an outgoing SMTP mail server. This will enable the automatic mailing of generated PDF reports to the respective users.
The whole process can be integrated using a single python script which uses Selenium web automation to export reports from Power BI Service and SMTP server to send out emails.
1. Power BI Service
2. Python with Selenium package
3. Excel input-feed containing filter names, filter values & corresponding email addresses
Power BI Report Screenshot Automation
This method uses Selenium web automation to capture screenshots of Power BI report pages and save them in a PDF file. Each report page published on the Power BI service has a unique URL and can be pre-filtered using query string parameters. Query string parameters are added to the report page URL and used to define multiple filter values.
To change filters using query string parameters, use the following syntax:
DASHBOARD_URL?filter= <table>/ <field> <operator> 'value'
Let's break down what each component of this syntax means:
DASHBOARD_URL - The URL of the published dashboard you want to filter.
<table> - The name of the table which contains the column on which you want to apply filters.
<field> - The name of the column you want to use for filtering.
<operator> - The operator you want to use to filter the dashboard. For example, ‘eq’ for equals, ‘ne’ for not equals, etc.
‘value’ - The filtering value you want to apply.
For example, if you want to filter a dashboard by the name of a customer, you might use the following syntax:
Multiple filters on different fields can be applied by adding ‘and’ another field in the same format. For example,
https://app.powerbi.com/xxxxxxxx?filter=Customers/Country eq ‘India’ & dimdate/Year eq 2022
For more details, please refer to Power BI Screenshot Automation (neenopal.com)
Exporting Power BI Paginated Reports
A major limitation of the above Screenshot approach is if the report contains a table or matrix visual, the screenshot will only capture the number of rows visible within the visual’s view; if the table contains more rows, users won’t be able to view all of them in the saved PDF file. To overcome this limitation, one can create Paginated Reports.
Paginated reports in Power BI allow users to create and publish pixel-perfect, printable reports which can be formatted well to fit in a page. Paginated reports are created using Power BI Report Builder tool and can be published to Power BI service. A Paginated Report can have multiple charts and tables with precise formatting and layout and can be exported into different formats including PDF, XLSX, PPT and DOCX, etc. If the report has a long table, the whole table is exported even if it spans to multiple pages. Paginated reports have Report Parameters which are used to filter the report data. The report parameters can be added to the URL by using predefined URL access parameters.
For changing filters using parameters, consider following URL syntax:
powerbiservicereporturl - The Power BI service URL of your paginated report. For example- https://app.powerbi.com/groups/me/rdlreports/xxxxxxxx
rp or rdl stands for report parameters or report commands respectively.
Report Parameters (rp:) - These are used to pass the report parameter values, For example- rp:Country=India
Report Commands (rdl:) - These are used to configure the look and feel of the paginated report and have multiple options.
Options which can be used for the current purpose are:
|Export Format||rdl: format = Value||This command will load the report and automatically start exporting it in the specified format. Formats available- PDF, PPTX, MHTML, IMAGE, EXCELOPENXML, WORDOPENXML, CSV, ACCESSIBLEPDF, XML|
|PDF- Margin Options||rdl: MarginBottom = decimal(in) rdl: MarginLeft = decimal(in) rdl: MarginRight = decimal(in) rdl: MarginTop = decimal(in)||These commands will set the page margins.|
|PDF- Page Size||rdl: PageHeight = decimal(in) rdl: PageWidth = decimal(in)||These commands will set the page size, For example for A4 size- rdl: PageHeight = 11.69 rdl: PageWidth = 8.27|
|PDF- Start Page or End Page||rdl:StartPage = integer rdl: EndPage = integer||These commands will set the start or end page for the report|
URL parameters in paginated reports in Power BI - Power BI | Microsoft Learn
Python Script Flow steps:
1. Opens Web Browser using Selenium
2. Signs-in to Power BI Service
3. Reads Excel file containing:
- a. Screenshot Automation: Email address, filter names & filter values.
- b. Paginated Report: Email address, parameter name, parameter value.
4. Iterates over each entry in excel and implement following steps:
a. For Screenshot Automation:
- 1. For each report page, create a custom URL by adding filters as query string parameters to the base URL.
- 2. Open each report URL in the browser window, capture a screenshot using selenium and save the image.
- 3. Once screenshots for all pages are saved, create a PDF file containing all images.
- 4. Email the generated PDF to the corresponding email address.
b. For Paginated Report:
- 1. Create a custom URL by adding parameter values(rp) and export format(rdl) as PDF to the base URL.
- 2. Open the URL in the browser window and the report is automatically downloaded to the specified directory.
- 3. Email the generated report to the corresponding email address.
The user may want to send the generated reports via email on a weekly or monthly basis. To achieve this, the whole automation flow can be scheduled on a fixed frequency and time using Windows Task Scheduler.
1. Steps for setting up a scheduled job using Task Scheduler:
2. Open Task Scheduler and click on “Create a new Task”.
3. In General Tab, enter job Name and Description
4. To set a trigger to set frequency and execute job on a fixed schedule:
- a. Go to Triggers Tab
- b. Click New
- c. Select Frequency (One Time, Daily, Weekly, Monthly)
- d. Set up Time
- e. Click OK
5. To set up Action to execute the Automation Script:
- a. Go to Actions Tab
- b. Click New
- c. Enter the path of python installation (python.exe) of your system in the Program/Script box
- e. Enter the path of the automation python script file in the Add Arguments field.
- f. Enter the path of the working directory in the Start in Field.
- g. Click OK
Note*- If more emails need to be added or if any filters need to be changed in the Excel input feed then the Excel file needs to be updated before the Scheduled time. In that scenario, it’s better to manually trigger the script.
Data Scientist @Avi Gupta
Leave a reply