Data Model - Advanced¶
In this tutorial, you will dive deeper into more advanced topics of data models, which are required to prepare your process data model for further analyses. More specifically, you will learn:
- How to create relationships between data model tables via foreign keys
- How to set up a process configuration in your data model
- How to import a name mapping file into your data model
- How to specify different data model reload options
Prerequisites¶
To follow this tutorial, you should have created a data model and should have uploaded data into it. As we continue working with the SAP Purchase-to-Pay (P2P) tables from the Data Upload tutorial, it is recommended to complete the Data Upload tutorial first. Further, it is recommended to complete the Data Export tutorial to have a basic understanding how data is retrieved from a data model via PQL.
Tutorial¶
1. Import PyCelonis and connect to Celonis API¶
from pycelonis import get_celonis
celonis = get_celonis(permissions=False)
[2023-10-24 13:27:00,097] INFO: No `base_url` given. Using environment variable 'CELONIS_URL' [2023-10-24 13:27:00,106] INFO: No `api_token` given. Using environment variable 'CELONIS_API_TOKEN'
[2023-10-24 13:27:00,284] WARNING: KeyType is not set. Defaulted to 'APP_KEY'.
[2023-10-24 13:27:00,287] INFO: Initial connect successful! PyCelonis Version: 2.5.0
2. Find data model tables¶
Let's start by locating the data model and the corresponding Purchase-to-Pay (P2P) tables, which we created in the Data Upload tutorial:
data_pool = celonis.data_integration.get_data_pools().find("PyCelonis Tutorial Data Pool")
data_pool
DataPool(id='9af14578-0fdf-4f76-ad0e-e529181f30bc', name='PyCelonis Tutorial Data Pool')
data_model = data_pool.get_data_models().find("PyCelonis Tutorial Data Model")
data_model
DataModel(id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', name='PyCelonis Tutorial Data Model', pool_id='9af14578-0fdf-4f76-ad0e-e529181f30bc')
tables = data_model.get_tables()
tables
[ DataModelTable(id='a1c1d712-5c1d-4099-b92a-3e6378519b07', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', name='ACTIVITIES', alias='ACTIVITIES', data_pool_id='9af14578-0fdf-4f76-ad0e-e529181f30bc'), DataModelTable(id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', name='EKPO', alias='EKPO', data_pool_id='9af14578-0fdf-4f76-ad0e-e529181f30bc'), DataModelTable(id='59bc9c0f-6291-4373-a555-d15db6d5509b', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', name='EKKO', alias='EKKO', data_pool_id='9af14578-0fdf-4f76-ad0e-e529181f30bc'), DataModelTable(id='9adb4284-6974-4d84-8559-01402bb70a74', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', name='LFA1', alias='LFA1', data_pool_id='9af14578-0fdf-4f76-ad0e-e529181f30bc') ]
activities = tables.find("ACTIVITIES")
activities_columns = activities.get_columns()
ekpo = tables.find("EKPO")
ekpo_columns = ekpo.get_columns()
ekko = tables.find("EKKO")
ekko_columns = ekko.get_columns()
lfa1 = tables.find("LFA1")
lfa1_columns = lfa1.get_columns()
3. Create relationships between data model tables via foreign keys¶
As known from the Data Export tutorial, it is possible to combine columns from different data model tables inside a PQL query. These columns will be aggregated into a single result table using PQL's implicit join functionality. However, in order for this to work, we need to specify how the tables in a data model are connected. This is achieved by creating foreign key relationships between pairs of tables.
3.1 The Celonis data model¶
Tables in Celonis are organized in a snowflake schema with 1:N relationships between tables. Hereby, the activity table serves as the central fact table, around which all other tables are organized. The activity table also serves as the base table when creating the single result table during PQL's implicit grouping. Other tables, such as the case table or master data tables, are then merged via a left-outer join with the N-table on the left and the 1-table on the right side.
3.2 Create foreign key relationships¶
To create a new relationship between two tables which can be used to perform implicit joins, we have to call the create_foreign_key()
method inside the data model. The method takes as input arguments:
Name | Type | Description | Default |
---|---|---|---|
source_table_id |
str |
ID of the source table (i.e. 1-table; right table in implicit join) | Required |
target_table_id |
str |
ID of the target table (i.e. N-table; left table in implicit join) | Required |
columns |
List[Tuple[str,str]] |
List of tuples in the format ("sourceColumn", "targetColumn") that specifies the foreign keys (i.e. over which columns the are tables connected) |
Required |
Let's start by creating a relationship between our activity table ACTIVITIES
and our case table EKPO
(i.e. Purchase Order Items). Hereby, EKPO
(1-table) is the source and ACTIVITIES
(N-table) is the target. The tables are connected via the foreign key _CASE_KEY
. During an implicit join, the 1-table EKPO
(right side) is then connected to the N-table ACTIVITIES
(left side) via a left-outer join:
ekpo_activities_fk = data_model.create_foreign_key(
source_table_id=ekpo.id,
target_table_id=activities.id,
columns=[("_CASE_KEY", "_CASE_KEY")]
)
ekpo_activities_fk
[2023-10-24 13:27:00,410] INFO: Successfully created foreign key with id '5003cc01-4ceb-450c-8116-882ecc4a17da'
ForeignKey(id='5003cc01-4ceb-450c-8116-882ecc4a17da', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64', target_table_id='a1c1d712-5c1d-4099-b92a-3e6378519b07')
Next, we create a relationship between EKKO
(i.e. Purchase Order Header) and EKPO
(i.e. Purchase Order Items). Hereby, EKKO
(1-table) is the source and EKPO
(N-table) is the target. The tables are connected via the foreign keys EBELN
and MANDT
. During an implicit join, the 1-table EKKO
(right side) is then connected to the N-table EKPO
via a left-outer join:
ekko_ekpo_fk = data_model.create_foreign_key(
source_table_id=ekko.id,
target_table_id=ekpo.id,
columns=[("EBELN", "EBELN"), ("MANDT", "MANDT")]
)
ekko_ekpo_fk
[2023-10-24 13:27:00,428] INFO: Successfully created foreign key with id 'ecc50172-e72d-4cd2-ad9b-8c2576560ab4'
ForeignKey(id='ecc50172-e72d-4cd2-ad9b-8c2576560ab4', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='59bc9c0f-6291-4373-a555-d15db6d5509b', target_table_id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64')
Lastly, we create a relationship between EKKO
(i.e. Purchase Order Header) and LFA1
(i.e. Vendor Master Data). Hereby, LFA1
(1-table) is the source and EKKO
(N-table) is the target. The tables are connected via the foreign keys LIFNR
and MANDT
. During an implicit join, the 1-table LFA1
(right side) is then connected to the N-table EKKO
(left side) via a left-outer join:
lfa1_ekko_kf = data_model.create_foreign_key(
source_table_id=lfa1.id,
target_table_id=ekko.id,
columns=[("LIFNR", "LIFNR"), ("MANDT", "MANDT")]
)
lfa1_ekko_kf
[2023-10-24 13:27:00,447] INFO: Successfully created foreign key with id '9cfc9fc8-80ee-4dc0-98da-1b83d0a73d91'
ForeignKey(id='9cfc9fc8-80ee-4dc0-98da-1b83d0a73d91', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='9adb4284-6974-4d84-8559-01402bb70a74', target_table_id='59bc9c0f-6291-4373-a555-d15db6d5509b')
We can verify that the newly-created foreign key relationships exist by calling the get_foreign_keys()
method from the data model:
data_model.get_foreign_keys()
[ ForeignKey(id='5003cc01-4ceb-450c-8116-882ecc4a17da', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64', target_table_id='a1c1d712-5c1d-4099-b92a-3e6378519b07'), ForeignKey(id='ecc50172-e72d-4cd2-ad9b-8c2576560ab4', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='59bc9c0f-6291-4373-a555-d15db6d5509b', target_table_id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64'), ForeignKey(id='9cfc9fc8-80ee-4dc0-98da-1b83d0a73d91', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', source_table_id='9adb4284-6974-4d84-8559-01402bb70a74', target_table_id='59bc9c0f-6291-4373-a555-d15db6d5509b') ]
Important:
In order for the table relationships to be effective, we have to reload the data model. Otherwise, we will receive a No common table
error when querying across columns in multiple tables.
data_model.reload()
[2023-10-24 13:27:00,485] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31' [2023-10-24 13:27:00,486] INFO: Wait for execution of data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
3.3 Querying across multiple data model tables¶
Let's retrieve the PQL query from the Data Export tutorial, which we used to get data from the EMS. In this example, we could only query columns from a single table, i.e. ACTIVITIES
, as the tables in the data model were not connected to each other:
from pycelonis.config import Config
Config.DEFAULT_DATA_MODEL = data_model
import pycelonis.pql as pql
df = pql.DataFrame({
"_CASE_KEY": activities_columns.find("_CASE_KEY"),
"ACTIVITY_EN": activities_columns.find("ACTIVITY_EN"),
"EVENTTIME": activities_columns.find("EVENTTIME"),
"_SORTING": activities_columns.find("_SORTING"),
})
df = df[df._CASE_KEY == "800000000006800001"].sort_values(by=["EVENTTIME", "_SORTING"])
result_df = df.to_pandas(limit=3)
result_df
[2023-10-24 13:27:00,601] INFO: Successfully created data export with id '44669b65-a14b-4ad4-947c-09fc637d0374' [2023-10-24 13:27:00,602] INFO: Wait for execution of data export with id '44669b65-a14b-4ad4-947c-09fc637d0374'
[2023-10-24 13:27:00,633] INFO: Export result chunks for data export with id '44669b65-a14b-4ad4-947c-09fc637d0374'
_CASE_KEY | ACTIVITY_EN | EVENTTIME | _SORTING | |
---|---|---|---|---|
Index | ||||
0 | 800000000006800001 | Create Purchase Requisition Item | 2008-12-31 07:44:05 | 0.0 |
1 | 800000000006800001 | Create Purchase Order Item | 2009-01-02 07:44:05 | 10.0 |
2 | 800000000006800001 | Print and Send Purchase Order | 2009-01-05 07:44:05 | NaN |
Since the tables in the data model are now connected via foreign key relationships, we can also add columns from other tables to the result table. Let's add the material text from the purchase order item table EKPO
, the document type from the purchase order header table EKKO
, and the vendor name from the vendor master data table LFA1
:
df["Material Text (MAKT_MAKTX)"] = ekpo_columns.find("Material Text (MAKT_MAKTX)")
df["Document Type Text (EKKO_BSART)"] = ekko_columns.find("Document Type Text (EKKO_BSART)")
df["NAME1"] = lfa1_columns.find("NAME1")
result_df = df.to_pandas(limit=3)
result_df
[2023-10-24 13:27:00,735] INFO: Successfully created data export with id '097a09c8-2907-48e6-b7a4-41c313324ed3' [2023-10-24 13:27:00,736] INFO: Wait for execution of data export with id '097a09c8-2907-48e6-b7a4-41c313324ed3'
[2023-10-24 13:27:00,772] INFO: Export result chunks for data export with id '097a09c8-2907-48e6-b7a4-41c313324ed3'
_CASE_KEY | ACTIVITY_EN | EVENTTIME | _SORTING | Material Text (MAKT_MAKTX) | Document Type Text (EKKO_BSART) | NAME1 | |
---|---|---|---|---|---|---|---|
Index | |||||||
0 | 800000000006800001 | Create Purchase Requisition Item | 2008-12-31 07:44:05 | 0.0 | Shafting assembly | Electronic commerce | eSupplier, Inc |
1 | 800000000006800001 | Create Purchase Order Item | 2009-01-02 07:44:05 | 10.0 | Shafting assembly | Electronic commerce | eSupplier, Inc |
2 | 800000000006800001 | Print and Send Purchase Order | 2009-01-05 07:44:05 | NaN | Shafting assembly | Electronic commerce | eSupplier, Inc |
4. Set up process configuration¶
After having defined the foreign key relationships, the next step in preparing the Celonis data model for further analyses is to set up a process configuration. This involves specifying:
- Which tables in our data model are the activity and case table
- Which columns of our activity table denote the Case ID, Activity Name, Timestamp, and Sorting columns
This can be done by calling the create_process_configuration()
method from our data model:
process_configuration = data_model.create_process_configuration(
activity_table_id=activities.id,
case_id_column="_CASE_KEY",
activity_column="ACTIVITY_EN",
timestamp_column="EVENTTIME",
sorting_column="_SORTING",
case_table_id=ekpo.id
)
process_configuration
[2023-10-24 13:27:00,819] INFO: Successfully created process configuration with id '425faf5f-4d5d-4f2d-be85-0a32974032b7'
ProcessConfiguration(id='425faf5f-4d5d-4f2d-be85-0a32974032b7', data_model_id='bed857a0-6bbc-40de-9d62-bd6f8b5a6b31', activity_table_id='a1c1d712-5c1d-4099-b92a-3e6378519b07', case_table_id='4e4fcb4e-7ca3-4d50-b2f8-2af30684ab64', case_id_column='_CASE_KEY', activity_column='ACTIVITY_EN', timestamp_column='EVENTTIME', sorting_column='_SORTING')
In order for the changes to be effective, we need to reload the data model again:
data_model.reload()
[2023-10-24 13:27:00,853] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31' [2023-10-24 13:27:00,854] INFO: Wait for execution of data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
5. Data Model Reload Advanced¶
5.1 Partial data model reload¶
A last topic in setting up our data model is to decide, how the tables should be loaded from the data pool into our data model. We need to specify whether all tables should be reloaded or only selected tables:
data_model.reload()
[2023-10-24 13:27:00,932] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31' [2023-10-24 13:27:00,933] INFO: Wait for execution of data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
data_model.partial_reload(data_model_table_ids=[lfa1.id, ekko.id])
[2023-10-24 13:27:01,021] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31' [2023-10-24 13:27:01,022] INFO: Wait for execution of data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
5.2. Data model reload without wait¶
Lastly, we can specify whether we want to wait for the data model reload (wait
). If wait=True
, the method waits until the reload is successfully completed and raises an error if the reload fails. If wait=False
, the method does not wait for the reload and does not raise an error in case of a failed reload:
data_model.reload(wait=False)
[2023-10-24 13:27:01,130] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
data_model.partial_reload(wait=False, data_model_table_ids=[ekko.id])
[2023-10-24 13:27:10,332] INFO: Successfully triggered data model reload for data model with id 'bed857a0-6bbc-40de-9d62-bd6f8b5a6b31'
Conclusion¶
Congratulations! You have successfully learned how to prepare your process data model for further analyses. In the next tutorial Data Upload & Export Advanced, you will dive deeper into advanced topics of data pushs/exports, such as chunking, different export/import types, and specifying custom column configurations.