- Databases
- Tables
- Attributes and associated revision information
- What attributes have been created in a table over a time period?
- What attributes have been modified in a table over a time period?
Basic usage
Pre-requisites
- A valid Python interpreter as defined by the Microsoft documentation.
- The MI Scripting Toolkit must be installed for the configured Python interpreter selected in Power BI’s Python Scripting options.
Security
When adding a Python script to Power BI, the script is persisted in the Power BI project. When using Power BI in a production environment, do not store credentials in plain text within the script. If possible, use methods of authentication that do not require storing sensitive information (such as using Windows Authentication viaautologon=True) or refer to the Authentication section of the user guide.
Dataset relationships
Power BI automatically attempts to define relationships between datasets based on the column names. For example, both the DataFramesdatabases and tables include a column Database.Key, which is automatically recognized as a
relationship.
In Granta MI, it is possible for some identifiers to not be unique across the whole system: for example, two tables
in different databases may share the same name or GUID. This is common occurrence in cases where databases have been
duplicated. As a result, it is sometimes necessary to build a new unique identifier by concatenating existing columns
for dataset relationship to be successfully established.
Access control
Granta MI schema definition and data held in Granta MI might be subject to access control rules. Be aware of the access control rules applicable within your organization before exporting data and publishing it through a Power BI report, to avoid breaching organizational data confidentiality policies.Configuration steps
-
Create a new
Python ScriptSource in Power BI via theGet Databutton.
-
Copy the content of the script cell below into the
Scriptinput.
- Update the connection URL and authentication method appropriately.
-
Click
OKto validate the script definition. Power BI will execute the script and report errors, if any. -
Select which DataFrames to import as datasets. Click the
Loadbutton to confirm.
- Verify that column types and dataset relationships are correctly defined.
- Build your report visualizations.
pandas.DataFrame objects from the script as datasets. If you
modify the provided code snippet, ensure that all data to be imported into Power BI is in a DataFrame, and that all
DataFrames have unique variable names.
Advanced usage
The following code snippet illustrates how to retrieve additional revision information from Granta MI. Unlike the previous code snippet, these operations are specific to a database and table. TheMI Training database and
MaterialUniverse table are used as examples.
Record revision information and data revision information for all records in the table can be used to determine:
- How many records have been created in a table in a set time period?
- What records have been updated?
- What attribute values have been updated in a set time period?
- What attributes are most populated in the table?
- Whether an attribute is being used?
Performance considerations
It is important to note that Power BI evaluates Python script queries multiple times:- First to validate the source when it is first defined
- Then to display the preview of DataFrames that can be loaded
- Then, when refreshing the report data, the Python script is evaluated again for each DataFrame loaded as a query
Factors impacting report performance
There are a number of factors to take into account when choosing a strategy to get data into Power BI from Python scripts using the MI Scripting Toolkit:- Time cost associated with an operation: Some operations (here defined as a sequence of steps that results in a DataFrame) require a longer time to process. For example, exporting data for multiple attributes for all records in a table is likely to be lengthy. On the other hand, the first code snippet included in this example is likely to run quickly for most users.
- Database and table size: Some MI Scripting Toolkit operations scale with the number of records, for example exporting data for records.
- Number of different datasets to extract: When loading DataFrames from a Python script, Power BI duplicates the
script in each resulting query. Upon refreshing the project’s data, all queries are re-run. The more DataFrames are
loaded from a script, the more times the script is re-run. For example, if the snippet above is added to a project
and both the
example_records_revisionandexample_records_data_revisionDataFrames are loaded as queries, then refreshing the project will run the script twice, each time running the two operations. If a third operation is added to the script and the computed DataFrame is loaded as a new query, then refreshing the project will run the script three times, each performing the three operations, etc.
Other concerns
- Content of the report: Depending on the type of reporting performed in Power BI, it might not be necessary to
retrieve data for all records. For example, if the aim of the report is to display metrics of record modifications
from the last week only, the list of relevant records can be obtained by performing a criteria search on the
lastModifiedpseudo-attribute, instead of relying on the methodTable.all_records(). The added expense of the search operation is likely to be negligeable compared to the cost associated with exporting unnecessary record data. - Frequency at which the report requires updating: the more frequently data needs to be refreshed, the less
tolerance there is for poor performance. On one end of the spectrum, if the report is an one-off operation, it can
be best to avoid using Python script sources in Power BI. The script can be run separately in Python, DataFrames
persisted to disk using
.to_csv()and loaded into Power BI using aText\CSVsource. This approach ensures the script is only run once. - Report publication: Whether the Power BI report is published to the online Power BI service, whether it needs to be shared with other users, whether the other users need to be able to refresh the data has consequences on which connectors can be used.
Mitigation options
Demonstrating how these can be implemented is beyond the scope of this example, but below are options to explore:- Using a distinct Python source query per operation: Because Power BI runs the Python script for each extracted DataFrame, it can be more efficient to have separate scripts, each responsible for generating a single DataFrame. There will be some repetition, such as creating a session, and identifying the correct database and table, but the overall performance can be improved, as lengthy operations are not repeated.
- Writing data to files: In some circumstances, using Python to write data to a file (for example via
DataFrame.to_csv()) and loading the file in Power BI might be more efficient. However this affects data can be refreshed, as in this scenario Power BI cannot re-run the script. - Disabling parallel refresh of queries: Power BI settings include the ability to disable
Parallel loading of tables. Doing so does not prevent unnecessary script re-runs, but it prevents the concurrent execution of many scripts, which could degrade performance for all queries by overloading the Granta MI server.