Your first Envision script - Inventory Optimization Software

Your first Envision script

Home » Resources » Here

This tutorial details your first dashboard composed with a short Envision script. No prior knowledge about Envision or about programming in general is required. Through this quick tutorial, you will become more familiar with key concepts relative to Envision.

Getting started

If you do not already have one, you will need to open a Lokad account which comes with a 30-day free trial. Once you have your Lokad account, you will need to load the Envision sample dataset. This dataset represents the historical data of a small imaginary retailer, and this is the data we are going to use in this tutorial.

Once you have successfully imported the sample files in your account, click on the "Projects" tab. Once on the project page, click on the Create Envision script link at the bottom and pick a title name. You should see a new page featuring a near empty blank page. This view is called the code editor, and it allows you edit an Envision script. Cut-and-paste the code below, and then click on the green button Start Run on the right.
/// This is my first sample script!
read "/sample/Lokad_Items.tsv"
read "/sample/Lokad_Orders.tsv" as Orders
read "/sample/Lokad_PurchaseOrders.tsv" as PO

oend := max(Orders.Date)

// Top indicators about the dataset
show label "This is a sample script" a1f1 tomato
show table "Product Lines" a2b2 with sum(1)
show table "Order Lines" c2d2 with sum(Orders.1)
show table "Purchase Order Lines" e2f2 with sum(PO.1)

// Two linecharts
Week.sold := sum(Orders.NetAmount)
Week.purchased := sum(PO.NetAmount)
when date >= monday(oend) - 52 * 7 & date < monday(oend) 
  show linechart "Purchased and sold{$}" a3f4 tomato with
    Week.sold as "Sold"
	Week.purchased as "Purchased"

// Worst rotating inventory
UnitSold = sum(Orders.Quantity) when date > oend - 365
Turns = UnitSold  / max(1, StockOnHand + StockOnOrder)
show table "Slowest inventory turns" a5f8 with
  order by Turns

Once the script execution is complete, a new green line should appear below the Start Run button. The color green indicates that the operation was completed successfully. Click on this line, and this will bring you to the newly generated dashboard. Click the top button Edit Script above the dashboard to get back to the code editor. Now, you know how to navigate back and forth between the dashboard view and the code editor view.


Understanding what’s going on

At this point, depending on your familiarity with programming concepts, the script above might still feel quite cryptic. Let’s review this script line by line in order to understand what is going on.

On line 1, we have a line that starts with //. All such lines are comments. They do not play any role in the logic of the script, and are only provided for readability purposes. The first line can even have a triple dash ///. With this triple dash, this line becomes the project subtitle, and gets displayed below the project title in the project list view.

On lines 2 to 4, we have three read statements that load the data needed to create the dashboard. Each of these statements uploads one file.

On line 6, we define a variable that stores the date of the latest order made. It is useful to define it because this date will be used several times in the rest of the script.

On lines 8 to 12, we have the two first lines of the dashboard, which comprises 4 tiles in total. The topmost tile is merely a label, while the three tiles below are the simple 1x1 tables containing only a single indicator. As we will see below, tables can also be used to display actual tabular data, not just a single value at a time.

On lines 14 to 20, we define the line chart that is displayed immediately below the three small tiles. We begin by computing weekly totals for both sales and purchases on lines 15 and 16. Then, on line 17, we filter the data in order to keep only the last 52 full weeks. The logic is a tiny bit complex but it boils down to a start condition and an end condition. Line 18 begins with 2 spaces at the very beginning of the line. These spaces are important: the when defines a block, and everything that is within this block, i.e. all the lines that have the 2 extra spaces, get filtered accordingly. Finally, the line chart is defined in the lines 18 to 20. In line 18, we set the title, color and position of the chart. In lines 19 and 20, we define the two curves that are displayed. Note that both lines start with 4 spaces : 2 spaces needed by the when a bloc and 2 extra spaces for the show block.

On lines 22 to 30, we have the table that comes below the line chart. This table displays the top list of products that are rotating the least within one’s inventory. On line 23, we compute the number of units sold for each product over the last 365 days. On line 24, we compute the inventory turns, i.e. number of rotations of the inventory within a year. The max() function at the denominator is merely used to avoid divisions by zero if a products happens to have zero inventory. On lines 25 to 30, we have the table display statement itself. In line 25, we set the title and the position of the title. From line 26 to 29, we define all the columns of the table : one line per column, from right to left. Finally, in line 30, we use the order by option in order to put the lower turns at the top of the table.


Unless you are already familiar with programming, the amount of information in this tutorial probably feels a little overwhelming. However, as you can see, within just 30 lines of code it is possible to compose a non-trivial dashboard that includes KPIs, some data visualization and a prioritized action list of products that probably requires some attention in order to avoid having these products clutter your inventory. By using only 30 lines of script, we have already gone quite far.

More generally, the syntax used to compose calculation expressions is very similar to the one in Excel. For example, the total stock value taking into account both the stock-on-hand and the stock-on-order could be written as sum(PurchasePrice * (StockOnHand + StockOnOrder)). Don’t hesitate to experiment with this sample, modifying some bits of the script and immediately observing the corresponding effects on your newly generated dashboards.