# Previous value in the database

**URL:** <https://community.fibery.io/t/previous-value-in-the-database/5162>\
**Category:** Get Help\
**Created:** [September 17, 2023, 12:39pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162 "2023-09-17T12:39:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 12:39pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/1 "2023-09-17T12:39:19Z")

</div>

Is it possible to output the previous values through the formula or automation?

Here’s a **simple** example of what I’d like to get:

 ![10255chrome](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/d/d2a70da6fa06ed5b9c93c27f84e841ecf1233309.png)

Please help me, I can’t find a solution.

You can see better that way:

 ![10257JPEGView](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/00702280c45f90cb31b455e4842db643763a1482.png)

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 17, 2023, 12:50pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/2 "2023-09-17T12:50:20Z")

</div>

Each entity has no relation to other entities in the same db unless explicitly linked, so there is no definition of ‘the previous value’.  
However , assuming I understand your example, you could use auto linking to connect entities together (based on matching name) in an n:n relation. Then you can use a formula to find which of the related items has the latest date earlier than the current entity, and find its price.

Edit: this answer was written before the second clarifying image was provided

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 17, 2023, 12:51pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/3 "2023-09-17T12:51:46Z")

</div>

By the way, what should happen with the fifth row? It has the same name and date as row 1, so which came first?  
Similarly, 6 and 2.

Perhaps you mean you only want to match if the name AND shop number is the same. If so, include this criterion in your auto relation

---

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 12:54pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/4 "2023-09-17T12:54:09Z")

</div>

> [@Chr1sG](#):
>
> By the way, what should happen with the fifth row? It has the same name and date as row 1, so which came first?

But different shops.

Two criteria are important - the “Date” and the “Shop” number.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 17, 2023, 12:56pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/5 "2023-09-17T12:56:21Z")

</div>

Ok, I didn’t know about the shops, but the principle is the same: match items based on rules, and then use a formula on the resultant collection to get the previous item’s value.

You do need to consider what should happen if there are multiple items with the same product, shop number and date. Which one should be thought of as being earlier and which should be later?

---

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 12:58pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/6 "2023-09-17T12:58:23Z")

</div>

> [@Chr1sG](#):
>
> Ok, I didn’t know about the shops, but the principle is the same: match items based on rules, and then use a formula on the resultant collection to get the previous item’s value.

I’ll try to implement it now

---

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 12:59pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/7 "2023-09-17T12:59:27Z")

</div>

> [@Chr1sG](#):
>
> Which one should be thought of as being earlier and which should be later?

based on the date

---

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 1:03pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/8 "2023-09-17T13:03:01Z")

</div>

![10260chrome](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/1/1325cb9ee0c053d25d30402c25d05467bbc14692.png)

> [@Chr1sG](#):
>
> and then use a formula on the resultant collection to get the previous item’s value.

> [@Chr1sG](#):
>
> Then you can use a formula to find which of the related items has the latest date earlier than the current entity, and find its price.

@Chr1sG Please can you tell me what the exact formula is?

---

<div class="post-metadata">

**Author:** ![Mikhail](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/mikhail/32/7493_2.png) [@Mikhail](https://community.fibery.io/u/Mikhail)\
**Post date:** [September 17, 2023, 1:31pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/9 "2023-09-17T13:31:58Z")

</div>

@Chr1sG Did you mean like this DDD.Sort(Date).First().Price ?

 ![10265chrome](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/0db61a00fc16273dd1782c0e05f9b893ca6081a1.png)

Is it possible to select an element of the array? I can select not the first element but the second?

DDD.Sort(Date).[0].Price  
DDD.Sort(Date).[1].Price  
DDD.Sort(Date).[2].Price

DDD.Sort(Date).Second().Price - doesn’t work

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 17, 2023, 5:07pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/10 "2023-09-17T17:07:25Z")

</div>

The auto-relation (which you’ve done correctly) will link to any entity with the same name and same shop, so for any entity, this will include future dated items and also itself.  
So the formula you need will be something like this:

`DDD.Filter(Date < [This Data].Date).Sort(Date).Last().Price`

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 17, 2023, 5:08pm UTC](https://community.fibery.io/t/previous-value-in-the-database/5162/11 "2023-09-17T17:08:34Z")

</div>

Also

> [@Mikhail](#):
>
> > [@Chr1sG](#):
> >
> > Which one should be thought of as being earlier and which should be later?
> 
> based on the date

but I wrote

> [@Chr1sG](#):
>
> You do need to consider what should happen if there are multiple items with the same product, shop number and **date**.
