from pycelonis import get_celonis
from pycelonis.root_cause_analysis import root_cause_analysis
celonis = get_celonis()
dm = celonis.datamodels.find('c4cb1efc-fa6a-4704-bc51-b3b365c5e796')
kpis = {'late_payment': """
CASE WHEN PU_COUNT(P2P_case, "Activity"."ACTIVITY_EN", "Activity"."ACTIVITY_EN" = 'Clear Invoice')=0
AND
DATEDIFF(dd, PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Create Purchase Order Item'), TODAY()) > 0
AND
DATEDIFF(dd, PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Create Purchase Order Item'), TODAY()) <= 60
THEN NULL
WHEN PU_COUNT(P2P_case, "Activity"."ACTIVITY_EN", "Activity"."ACTIVITY_EN" = 'Clear Invoice')>0
AND
DATEDIFF(dd, PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Create Purchase Order Item'),
PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Clear Invoice')) > 30
THEN
1
WHEN PU_COUNT(P2P_case, "Activity"."ACTIVITY_EN", "Activity"."ACTIVITY_EN" = 'Clear Invoice')>0
AND
DATEDIFF(dd, PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Create Purchase Order Item'),
PU_MAX("P2P_case", ROUND_DAY("Activity"."EVENTTIME"), "Activity"."ACTIVITY_EN" =
'Clear Invoice')) <30
THEN 0
ELSE NULL
END
"""}
from pycelonis.celonis_api.pql.pql import PQL, PQLColumn
kpi_name = "late_payment"
dm.get_data_frame(PQL(PQLColumn(kpispkpi_name], kpi_name)))
# Define parameters
SELECTED_TABLES=q'P2P_case']
CELONIS_FILTER=None
CHUNK_SIZE=20
# Create root cause analysis object
RCA = root_cause_analysis.RCA(kpis=kpis, datamodel=dm, selected_tables=SELECTED_TABLES, celonis_filter=CELONIS_FILTER, chunk_size=CHUNK_SIZE)
# Run RCA
rca_result = RCA.apply()