Community perspective: Add a timestamp to processes utilizing Anaplan API and Python

Lehtohen
edited December 2023 in Blog

Anaplan is a critical tool for different business processes, and it is used to handle a variety of data. One key attribute for data quality is the timeliness and reliability of the data. It is a known best practice to use Data Hub for centralized data management and then import data to different spoke models. Even though the data flow between models is working smoothly, one issue I have faced with multiple customers is that it is not visible for the end users when the data was previously updated, especially if the end users are working only in the NUX side. 

With the power of the Anaplan API, we can make it visible for the end users when an action was last run and thus improve the quality of the data that is an integral part of decision making. 

In this article I will show how it is possible to store a timestamp for when a dataset was last updated using the Anaplan API and Python. This will include a few key points when dealing with the Anaplan API, including authentication and using the API to write directly to a cell. 

Importing required libraries and setting parameters

In order to run the script, a few libraries need to be imported: 

  • Requests allows sending HTTP requests easily
  • Date is used to get the current timestamp
  • Datetime is used to manipulate dates and times, in this case it is used to get the timestamp in required format
  • Json is used to convert a Python object to a JSON string

In this example we are using the basic authentication method. I prefer using some base64 encoder for encoding the password for scripts. The user_pw would be an encoded format of “email:password”. For more efficient use of the API, I would recommend using the CA Certificate. 

As input we need to also give the workspace id, model id and module id. The line item id is optional as it is also possible to use the line item name. 

Lehtohen_1-1668950711116.png

Authentication and timestamp with Python

Next step is to provide authentication. The piece of script below will return an authentication token.

Lehtohen_3-1668950765078.png

Writing to a cell with Python

Next we are updating module data by writing directly to a cell. This will not trigger any import action, which means that it will not lock the model from other users. 

For more information about the required fields and request body, I suggest checking out the documentation in the Apiary

This will write the current date and timestamp to the defined cells in Anaplan in text format. 

This script can be scheduled to run automatically in the background, and update the current time on a set interval for example 15 minutes. 

Lehtohen_4-1668950791053.png

Adding timestamp to a process

Once we have the date and timestamp in our module, we can use these in our import actions. In Data Hub my export modules could reference these fields so that when imported to other models the latest timestamp would remain. Note that the line item, which holds the timestamp does not require any dimensions, so that it does not consume any extra space. 

Lehtohen_5-1668950824117.png

If on the other hand you are running processes inside a model, you would need to also store the timestamp in that model. In this case you would need to create an import view from the module. I have used a “dummy list” as a dimension in order to get the import action run more smoothly. 

Lehtohen_6-1668950824073.png

Now my demo process would contain two steps:

  1. Import data to a module
  2. Import timestamp to a line item in the module

It is likely that your target module contains other dimensions, which are not applicable for this import action. 

Lehtohen_7-1668950824095.png

The end result in a module would look something like this: 

Lehtohen_8-1668950823920.pngAs this function is writing directly to a cell, there is no import action required, which would temporarily block the model from other users. 

If you would like to also store historic information for when actions have been run, I recommend checking out this community article for ideas: Community perspective: Monitor activity in your models using an action log.

I see great opportunities with the Anaplan API, and I have many API tricks in my sleeve. Feel free to leave a comment or message me on LinkedIn if you are interested to hear more.