SPL XLL Practice: Excel Interval Association
Tags: excel, vba, datascience, tutorial
Published: 2024-04-03 08:51:44

Sometimes in work, it is necessary to fill in different values based on different interval gears. For example, in the student evaluation system, there are excellent or good based on different score intervals; the shipping cost is calculated based on different prices among different weight ranges. In Excel, it is generally necessary to use multiple nested IF or lookup functions to implement, which is cumbersome and prone to errors. Here is a simple and easy-to-use method, using the Excel plugin SPL XLL. SPL XLL has rich data operation functions that can solve many complex Excel problems. In interval association, the pseg() function is usually used to return the position sequence number of the data to be searched in the intervals.

For example:

## 1. Directly write the formula for interval association

The data is as follows:

![Quantity Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uktacgp6rbewikiejlmg.png)



Calculate the price of column B based on the purchase quantity in column A. The calculation rule is that different quantity intervals correspond to different prices, as shown in the table below:

![Price Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfz2rs0gdlcxj5l45ysr.png)



For cases with few intervals, the formula can be directly written.

Write in cell B2:

```
=spl("=[15,13.75,13,12.5]([30,50,100,300,500].pseg@r(?1))",A2)
```
As shown in the figure:

![Quantity table with SPL code inserted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arive7knr8a0bhg5ey3h.png)



?1 represents the data parameter to be passed in, which refers to A2, which is 221.

First, use the pseg function to calculate which segment number A2 is located in the interval [30,50,100,300,500], and then retrieve the corresponding segment number’s price from [15,13.75,13,12.5] to return. Here 221 is located in the third interval of 100-300, the psge() function will return 3, and then retrieve the third one from the price list, which is 13.

@r represents the front opening and back closing of segmented intervals.

Return result:

![Result table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar19dhkuzfwr8hodpbff.png)



Then drag and copy B2 to each cell:

![Result table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luvbbbh919w2mb68u36m.png)



## 2. Use the association table to implement interval association

The psge() function can also be used for association with table parameters.

For example, there is the following data:

![Quantity table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrlrdd4e3o1v9bxegh14.png)


![Full quantity table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vyrip0ut8mwby7an0zk3.png)

Calculate the values of the Price column in Sheet2. The calculation rule is to use Quantity to search in Sheet1, if the quantity is greater than StartQuantity and less than or equal to EndQuantity, return the Price of this row. Write the code in B2:

```
=spl("=E(?1).segp@r(StartQuantity,?2).Price",Sheet1!A$1:C$5,A2)
```
Similarly, ?1 and ?2 represent the data parameters to be passed in, here referring to Sheet1!A$1:C$5 and A2 respectively.

The whole statement represents finding the segment number of A2 in the StartQuantity column of Sheet1, and then retrieving the price of the corresponding segment number row to return.
The result is shown in the figure:

![Result table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpjppior09x3fqjdvjk9.png)

Similarly, drag B2 and copy to each row:

![Result table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e67jkqm3zeqnhzvlhsib.png)



Using SPL XLL for interval association can avoid racking your brain with multiple IF statements. The code is intuitive, easy to understand, and very user-friendly.

Of course, SPL XLL also has many useful features, such as set operations, group calculations, batch processing, and so on. Using it to assist Excel can double work efficiency in seconds.

SPL XLL download address: [esProc Desktop Download](http://www.scudata.com/download-Desktop/)
Plugin Installation Method: [SPL XLL Installation and Configuration](https://c.scudata.com/article/1652061135502)
Reference cases: [Desktop and Excel Data Processing Cases](https://c.scudata.com/article/1651916536524)