Automatically update Power BI Desktop in Python

Power BI is convenient, isn't it?

If you make it once, it feels good that monthly analysis is done automatically. Clarity that allows you to freely arrange graphs and understand the desired data at a glance. Filters and hint functions that allow you to deepen your analysis just by operating the mouse. Relationships between tables that can be connected intuitively. Cooperation with various data sources. After all, I'm surprised that it's free.

I also use it to analyze hundreds of millions of data. It's fun to use the hint function to show what was happening at that time. However, there is one problem.

It's annoying to update! !! Even though the data is preprocessed, it takes tens of minutes to read the data. It's stressful when you want to get it out. The PC becomes heavy. Besides, if you try to update on a PC with low specifications, it will fall due to lack of memory. It seems that you can automatically update with unbranded Power BI instead of Power BI Desktop, but it will be charged and account management is difficult.

So, this time I made a program in Python that automates Power BI Desk Top.

Environment / configuration

I use the Windows 10 Pro Task Scheduler to run Python programs on a regular basis. I use Pywinauto to open the pbix file in the GUI, click the "Update" button and then save it. By the way, the data source is obtained by connecting to MySQL on the server.

code

auto_pbi.py


import os
import sys
import time

from pywinauto import Desktop, Application, keyboard

def main(workbook):
    exe = 'PBIDesktop.exe'

    #Open file
    os.system('start "" "{0}"'.format(workbook))
    app = Application(backend='uia').connect(path=exe)
    time.sleep(60)
    try:
        #Specify Window
        win = app.window(title_re = '.*Power BI Desktop')
        win.set_focus()
        #Click Home> Update
        win.home.wait("visible")
        win.home.click_input()
        win.update.wait("visible")
        win.update.click_input()
        win.Cancel.wait_not("visible",timeout=6000)
        #Save
        keyboard.send_keys("^s")
        time.sleep(120)
    except Exception as e:
        print(e)
    finally:
        app.kill()


if __name__ == '__main__':
    try:
        file_path = sys.argv[1]
    except (IndexError):
        print('Please specify the file.')
        sys.exit()

    main(file_path)

The above code is specified in auto_pbi.bat and it is run in the task scheduler.

auto_pbi.bat


cd specified folder
python.exe auto_pbi.py report.pbix
exit

For Python, .py files, and pbix files, it is recommended to write them with absolute paths. I have put the report and py files in the folder, so I moved the folder on the first line.

important point

Run task scheduler

I tried to run this program on a remote PC but it didn't work. If I "run" the task scheduler myself, it works, but if I disconnect the remote connection and try to run it with a trigger, it doesn't work. This is because with a remote PC, the desktop screen is locked when the remote desktop is disconnected. I get the following error:

there is no active desktop required for moving mouse cursor!

To take this measure, you need to set the desktop screen so that it does not lock out when you turn off the remote desktop. https://pywinauto.readthedocs.io/en/latest/remote_execution.html

I took the method of using the following command when turning off the remote desktop. Please change the place where \ # 80 is according to the environment. Open Task Manager> User on the remote PC, right-click the heading of each column and check "Session" to display the remote session ID. Enter the following command from the command prompt to disconnect the remote desktop session.

TSCON RDP-Tcp#80 /dest:console

Sleep processing

If I didn't wait for a while after launching pbi, an error occurred because Window did not launch. Also, I thought that I should be careful unexpectedly, the sleep processing after saving with Ctrl + S. I thought I could complete the save process in about 30 seconds, but I couldn't. I think it's better to keep it longer than you think.

Other

If you can copy the file after updating and send it, or if you get an error, send an email, etc., if you can base it on this, you can save the troublesome processing such as daily big data update and send a comfortable Power BI life. I think you can get it!

Recommended Posts

Automatically update Power BI Desktop in Python
Power BI visualization of Salesforce data entirely in Python
Get the desktop path in Python
Automatically format Python code in Vim
Get the desktop path in Python
Python update (2.6-> 2.7)
Automatically generate Python Docstring Comment in Emacs
Principal component analysis with Power BI + Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Automatically register function arguments to argparse in Python
Try auto to automatically price Enums in Python 3.6
Automatically create word and excel reports in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Geographic information visualization of R and Python that can be expressed in Power BI
Use Cursur that closes automatically with sqlite3 in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python