# Modify row value with data of previous record

Hello everyone,

I’ve got a dataset, like this:

CAPITAL BUYING PRICE QTY SELLING PRICE REVENUE
1.000 € 10 € 100 12 € 200 €
1.000 € 13 € 77 15 € 154 €
1.000 € 12 € 83 10 € -166 €
1.000 € 20 € 50 22 € 100 €

I need to calculate the amount I can buy given the capital and the buying price.
The problem is that the amount need to be modified each row, because the previous revenue has to be added to the initial capital. The before example will become something like this:

CAPITAL BUYING PRICE QTY SELLING PRICE REVENUE
1.000 € 10 € 100 12 € 200 €
1.200 € 13 € 92 15 € 184 €
1.384 € 12 € 115 10 € -230 €
1.154 € 20 € 58 22 € 116 €

Maybe I need to itereate the first dataset or is there a faster way to achieve this?

Why is QTY different in the 1st and 2nd tables? What’s the logic to obtain QTY in the 2nd table?

the first row, with 1000 capital and 10€ price, the bought qty is 100. Selling these qty grant +200€ that will increase the capital for the next record and so on

In this case, the only numbers that matter for this calculation are the initial capital and the buying and selling prices. Everything else is calculated from them.

The calculation can be arranged using iterations and the Shared memory. Shared memory is used to pass the new capital after each transaction (row) to the next transaction.

See the example below:
cumulative-expression.morph (9.1 KB)
Sample data.xlsx (8.8 KB)

Notes

• The last transaction revenue is different by 2 euros because the quantity is calculated using floor() rather than round().
• After making changes in the example do full Reload & Run to reset the initial value of capital.
• The example is not suitable for simultaneous parallel execution.

Wonderful solution!

Shared memory is an useful feature, great job EM Team to improve EM and keep it one of the best tools for IT!

it happens that, randomly, EM shows this error.

I did some test: 681 iterations, it runs smoothly but after a while this error start to be invoked so I need to close/open EM.

Hi @AndreaM . Does this issue occur when you use one instance of EasyMorph Desktop only? Could it be that for example there is also EasyMorph Launcher that is simultaneously active and is executing some shared memory-involving task? Or, maybe your `repo.sqlite` database file is used by both Server and Desktop or maybe you access it using network drive, etc?

PS just to be sure, what EasyMorph version are you using?

We use 5.1.2.5 version.
I closed every instance of EM Desktop and this is the first project where we use Shared Memory actions.

I can confirm that the problem depends on the occupation of the server: while I was testing my project with the Shared Memory actions, another user contacted me that something went wrong with another software that launch “ems-cmd.exe run”: this app run the ems-cmd to generate few PDF, 1 for each call, and yesterday it happened that the number of PDF wasn’t correct (something like 14 PDF instead of 16). I don’t have logs about this app, but after I stopped the Shared Memory test, the PDF generator runned perfectly.

To clarify things, are you performing Shared Memory tests on the Server using a dedicated server Task or are you performing them using EasyMorph Desktop (as the Desktop screenshot implies) while both Server and Desktop use the same data connector repository (the `repo.sqlite` file), or are you using Desktop with Server Link configured and Shared Connectors set up in ‘Server space’ mode (like in the screenshot below)?

I’m running a Morph project with an EM Desktop (installed on the same server of the EM Server version).
Desktop use the “file repository” option and the server use the same file.

Maybe I’ve to change Desktop Connector to use “Server space: Default”?

Yes, that’s exactly the scenario to cause a database file lock. EasyMorph uses sqlite file-based ‘database’ that is extremely light-weight and reliable, but unfortunately not suitable for concurrent usage by more than one process at the same time.

If you indeed switch your data connector manager to use Server space (like in the screenshot I’ve posted) this will reorient Desktop to use Server space for shared memory too and eliminate locking issues.

Ok, I’ll try to change it, but it show me this error:

You have to configure Link to Server, as described here: