【Query Builder：Expression】Refer to columns in the previous row of query results

I used the Expression which refers to a column in the current row of the query result with the past article [【Query Builder: Expression】Refer to columns in the current row of query results](https://qiita.com/Miki_Yokohata/items/9cc6d14abd58403834c0) .
This time, let's display the cumulative value using the syntax that refers to the columns of the **Previous Row**.

Launch the Query Builder from the [Sharperlight](https://sharperlight.com/) application menu.
Select `Detailed Report` for Mode, `SAP Business One` for Product, `OEC Computers Australia` for Company and `Outgoing Payments` for Table.

Select **\<ALL>** for the mandatory filter `Document Number` and **V10000** for the `Costumer/Vendor Code`.
Next, drag and drop the `Canceled` field from the Selection list to the Filter area. And specify **N** for the value.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/b553d6f7-f1f0-af0e-244c-de14640d7823.png)
With this, it can extract the documents for the vendor V10000 and they have not been canceled.

Select the following fields to output.
- Customer/Vendor Name
- Posting Date
- Document Total
All can be selected from the Selection list by dragging and dropping or double-clicking.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/4eb4b1b1-4f9b-d4f6-3581-aa3d16084fc5.png)

Now let's get to the point.
Right-click in the output area and select **Add Expression**.
Create a logic with it.
The logic calculates the cumulative amount of the Document Total column.
That is, the value of Document Total column in the first row are displayed as cumulative amount as is. The second line is the cumulative amount of line 1 + the value of Document Total column of line 2, the third line is the cumulative amount of line 2 + the value of Document Total column of line 3, and so on.

First, we need to determine if it is the first row.
Find the appropriate function in the list. Type **Start** in the `Find` text box. Under the `Conditional` folder you will find the **Boolean flag to identify the Start of the Report / StartOfReport**. Double-click on it to transfer it to the editor. This function `StartOfReport` returns **True** for the first row of the query result and **False** for the other rows.

Use the IIF syntax and write as follows:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/20116760-8daf-4996-7f73-db8091434471.png)
Click the OK button once to save.
Let's change the **Description** of the added expression to `Cumulative Amount` and the **Name** to `CumulativeAmt`.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/5bce24ec-1238-0a66-22bf-cb41d7305051.png)

Double-click to open the expression again and complete the IIF statement.
When True, i.e. in the first row, the value of Document Total column should be used as is, so select **Document Total {%DocTot}** in the `Current Row` folder from the function list.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/4d68ef5a-bc99-9109-a243-dc154bdf871e.png)
If False, that is, from the second line onwards, the formula shoud be the cumulative amount of the previous row + the value of Document Total column on the current row, so choose the **Cumulative Amount {%Previous:CumulativeAmt}** of the `Previous Row` folder from the function list first.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/f4b34d9d-cfb3-d81f-b56b-f3d804778d81.png)
Then add **+** sign.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/74a45cf4-fdff-17d7-fc14-32c45a593b46.png)
Finally select **Document Total {%DocTot}** in the `Current Row` folder from the function list.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/e7ded71b-3f3e-f738-26f7-6ce281636cb9.png)
Save with the `OK` button and check the result with the `Preview` button.
It should calculate the cumulative amount well.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/2b9f5bbb-3d78-4897-f650-826486d7d4bb.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2530698/59562f47-48e7-0bb8-ccf7-8a9482054a96.png)
This is just an example and not practical, but I hope this helps your work.