Python: Dynamic Power BI PDFs with REST API

TABLE OF CONTENTS

INTRODUCTION

Sometimes you have a requirement to export large volumes of static reports to users who may or may not have Power BI licenses. One way of accomplishing this is through email subscriptions. The issue being that a large population (say, 50+ users) would each need their own subscription managed per report! No good (think, 50 users per 10 reports is a nightmare to manage).

Another non-optimal option is to provision per user licenses, but this can be tricky for the mid-size company. 100 users at $12/month can be costly for single report consumption (i.e., $14k+ annually). Double that if you use fancy features like paginated reporting.

We will review a solution where a report is iteratively filtered by 'employee' and sent automatically to a manager, where notes can be added, and the final write up is forwarded to the intended audience. Hold on, this will be fun!

Note: the respective GitHub repo with all code can be found here: GitHub, Python Dynamic Power BI PDFS with REST API.

PREREQUISITES

Programming Tools

Environment Settings

Microsoft License Requirements:

Microsoft Fabric Admin Settings:

Azure Entra Application

To use the Power BI REST API, an Azure Entra application is required. This application allows you to authenticate and authorize access to the Power BI service. The good news is that it's generally free with a Microsoft subscription and easy to set up.

Steps to Set Up an Azure Entra Application:

  1. Register an Azure Entra Application:
  1. Add API Permissions:
  1. Add a Client Secret:

Please note the following for the script: Display Name, Application (client) ID, Object ID, Directory (tenant) ID

ADD USERS

SMTP Settings

Setting up a SMTP server is slightly outside of my scope; I can tell you a quality tech can provision this quickly 😊 However, there are important settings that will need to be adjusted so your script will work appropriately.

SMTP Server

The following is required, please get this from you 'tech': Server Address, Port, User Name, Password. Best practice is to use a Service Account for the user.

Enable the SMTP Client Authentication settings:

Adding an 'App' password to the Service Account

Additional Information

Familiarize yourself with everything in these Microsoft Docs:

PROCESS & SCRIPTS

Required Python Packages

File Parameters

Parameters: PBI Config

The following breaks down where each parameter is sourced.

Parameters: PS_Start_Automation

Parameters: AutomatedReports

The only parameters required are your filters. You will see that I used an 'employees' table to loop through the list. Also, your filter expression will need to be updated. I have a column in the semantic model Employee [Employee Last First Name]. This is the column I'm filtering on each loop. Note that you will need to update this based on your semantic model. Make sure to maintain the correct syntax per this Microsoft doc (Filter a report using query string parameters in the URL).

Main Scripts

Effectively, there are three main scripts:

Working in tandem, the scripts should be housed in a shared folder. Once the PS_Start_Automation is updated for this location, the AutomatedReports.py script will connect to the PBIconfig.json file automatically. Again, if the 'save_locally' parameter is true, a 'PDF_Reports' folder will be created, and the generated PDFs will be saved here.

Full scripts and code can be found here: GitHub, Main Scripts

Supporting Scripts

A Jupiter notebook has been added so users can walk through the process of testing scripts and establishing their environments. A quick breakdown is below.

Full scripts and code can be found here: GitHub, Supporting Scripts or GitHub, SupportingScripts.ipynb

Scheduling with Task Scheduler

As mentioned earlier, the scripts are tied and will work in concert when the PowerShell script is launched. However, task scheduler is used to kick off the PowerShell job at certain intervals. Below walks you through the process of setting this up.

Task Scheduler Setup

  1. Open Task Scheduler by typing Task Scheduler in the Windows search bar and selecting the application.
  2. Click on Create Task… in the right-hand pane.
  3. In the General tab, provide a name for your task (e.g., Run Python Script Daily) and configure the task to run with the highest privileges if needed.
  4. In the Triggers tab, create a new trigger:
  1. In the Actions tab, create a new action:
  1. In the Conditions and Settings tabs, adjust any additional settings as needed.
  2. Click OK to create the task.

CONCLUSION

This guide should guide you through the full process of implementing your own 'automated Power BI PDF download and publisher' deployment. This script was built with the scope that all reports are sent to a single user so they could forward the reports with notes. However, I may create a part two where scripts are sent to the appropriate recipients. Stay tuned!

Again, the respective GitHub repo with all code can be found here: GitHub, Python Dynamic Power BI PDFS with REST API.

About the Author:William Rodriguez is the founder and principal consultant of Analytical Ants, an analytics company focused on procuring insights via data engineering, data science, and data analysis. William has led multi-million-dollar construction projects, acted as financial controller managing million-dollar monthly check productions, and built and deployed BI infrastructures at companies large ($30B Revenue) and 'small' ($60M Revenue). Academically, he has his master's in business, two undergraduate business degrees, seven active Microsoft certifications, and over 70 accredited SQLBI training hours. William loves spending time with his amazing family and knows that he would be nowhere without Christ.

Reach out to Analytical Ants LLC for all your data/analytical needs. We specialize in #DataEngineering #DataScience, and #DataAnalysis.

We're Social!

Email us or follow us on LinkedIn, GitHub, or X.