# Fetching data from a previous row using date

**URL:** <https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073>\
**Category:** Ask for Help\
**Created:** [January 9, 2025, 2:22am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073 "2025-01-09T02:22:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 9, 2025, 2:22am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/1 "2025-01-09T02:22:00Z")

</div>

I am new to Glide building my first app. The app is intended to collect water meter readings at water treatment facilities on a daily basis. The app needs to calculate the water flow on a daily basis, which is fetching the previous day meter reading and subtracting it from the current days reading. I built a table and screen to capture the meter reading for these facilities. The table includes the facility ID number (which is unique for each facility), the date and time, the meter reading and a field to calculate the flow. What formula in Glide can I use to fetch the previous day reading in order to calculate the flow?

---

<div class="post-metadata">

**Author:** ![MaximeBaker](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/maximebaker/32/80877_2.png) [@MaximeBaker](https://community.glideapps.com/u/MaximeBaker)\
**Post date:** [January 9, 2025, 2:36am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/2 "2025-01-09T02:36:25Z")

</div>

Is someone gonna add data by hand somewhere?

Do you want the calculation on new added row or on a daily schedule automatically?

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [January 9, 2025, 3:06am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/3 "2025-01-09T03:06:21Z")

</div>

You would use a variation of this.

> [@Diffferent formula from Row 2 onwards](https://community.glideapps.com/t/diffferent-formula-from-row-2-onwards/75370/2):
>
> Make sure you record the user’s ID/email. Use a query column, filter by user’s ID/email equals to this row \> user’s ID/email. Add a rowID column to your table. Add a lookup column on top of the query resulr, pointing to that rowID column, so you get an array of IDs for the same user. Add a find element index column, use the lookup result as the base, and look for the current row’s rowID. You will get a 0-based list of index for each user. Add a math column, with the formula being: I-1, with I b…

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 9, 2025, 12:33pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/4 "2025-01-09T12:33:49Z")

</div>

@MaximeBaker Yes, there are operators that visit the facilities on a daily basis and they will key the meter readings into the app. The calculation can go on the new row but I will be referencing it at some point to add it to a monthly report.

Thanks

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 9, 2025, 12:34pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/5 "2025-01-09T12:34:36Z")

</div>

Thanks @Jeff_Hager I will check that out.

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 9, 2025, 10:48pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/6 "2025-01-09T22:48:41Z")

</div>

@Jeff_Hager I followed your steps and I was able to create the index for the previous row. I am at this point:

- Add a query column, use the query from the 2nd step as a base, filter by index equals to this row \> previous row’s index.
- Add a single column to fetch the first “EndBal” from the query above.

I don’t believe I am following the first bullet correctly, can you clarify? I understand using the query from the 2nd step as the base, but don’t understand the rest. Also, in the second bullet, how do I fetch from the query above?

Thanks!

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [January 9, 2025, 11:15pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/7 "2025-01-09T23:15:19Z")

</div>

So if you create a query column, you can point it at the same table and set the filter in the query to return rows where the Index matches the ‘this row’ prior index (index-1).

Personally for this use case, I would just create a relation instead which will be a simpler and a run a little faster. Just set the relation to match the row index to the calculated prior row index.

Then you can use a lookup column (or single value column) to retrieve the prior row column you want from the relation.

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 13, 2025, 2:02pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/8 "2025-01-13T14:02:05Z")

</div>

@Jeff_Hager awesome! that worked, thanks so much for your help!

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 14, 2025, 2:15am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/9 "2025-01-14T02:15:02Z")

</div>

@Jeff_Hager I thought I had it solved, but as I started adding meter readings to the table for different facilities, the relation I set up failed to work properly.

I have created a simple version of my table below. The issue is that the “Index Relation” keeps referring to the first facility. In other words, the “Prior Row Index” only matches on the first facility, so it always pulls in the “Previous Meter Readings” from the first Facility.

I have the “Index Relation” set to “Prior Row Index” Matches the value in: “Index”

Do you have any suggestion on how to solve this?

Thanks very much

| Facility ID | Date | Meter Reading | Index | Prior Row Index | Index Relation | Previous Meter Reading |
| --- | --- | --- | --- | --- | --- | --- |
| NY3920886 | January 6, 2025 at 2:06 PM | 575,799 | 0 | -1 | | |
| NY3920886 | January 7, 2025 at 2:06 PM | 575,850 | 1 | 0 | NY3920886 | 575,799 |
| NY3920886 | January 8, 2025 at 1:05 PM | 575,900 | 2 | 1 | NY3920886 | 575,850 |
| NY3920886 | January 9, 2025 at 5:24 PM | 600,000 | 3 | 2 | NY3920886 | 575,900 |
| NY3920886 | January 13, 2025 at 1:10 PM | 605,000 | 4 | 3 | NY3920886 | 600,000 |
| NY3920886 | January 13, 2025 at 1:11 PM | 606,000 | 5 | 4 | NY3920886 | 605,000 |
| NY3905695 | January 13, 2025 at 1:12 PM | 800 | 0 | -1 | | |
| NY3905695 | January 13, 2025 at 1:12 PM | 850 | 1 | 0 | NY3920886 | 575,799 |
| NY3905695 | January 13, 2025 at 1:15 PM | 860 | 2 | 1 | NY3920886 | 575,850 |
| NY3920886 | January 13, 2025 at 2:44 PM | 607,000 | 6 | 5 | NY3920886 | 606,000 |
| NY3920886 | January 13, 2025 at 2:45 PM | 610,000 | 7 | 6 | NY3920886 | 607,000 |
| NY3905695 | January 13, 2025 at 2:46 PM | 900 | 3 | 2 | NY3920886 | 575,900 |
| NY3902964 | January 13, 2025 at 8:17 PM | 450 | 0 | -1 | | |
| NY3902964 | January 13, 2025 at 8:17 PM | 500 | 1 | 0 | NY3920886 | 575,799 |

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [January 14, 2025, 2:22am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/10 "2025-01-14T02:22:37Z")

</div>

> [@bmagnotta](#):
>
> The issue is that the “Index Relation” keeps referring to the first facility.

Since you have multiple facilities, you will need to use the Query and Single Value method so you can filter the query by multiple values such as facility AND prior index.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 14, 2025, 2:48am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/11 "2025-01-14T02:48:22Z")

</div>

Haven’t been following this thread too closely, but I think the below video might help:

https://www.loom.com/embed/042be3be13004355b0dedec1be67b400

[https://www.loom.com/share/042be3be13004355b0dedec1be67b400](https://www.loom.com/share/042be3be13004355b0dedec1be67b400)

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 15, 2025, 7:23pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/12 "2025-01-15T19:23:29Z")

</div>

@Jeff_Hager finally got it to work, thanks again. Now I am trying to figure out how to prevent multiple meter readings from being entered on a single day for each facility. Only want one reading per day per facility. There doesn’t appear to be an easy way to do this. I am using a form screen with text, date and number fields added to it. See screenshot. Any suggestions?

 ![Screenshot 2025-01-15 at 2.20.02 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/a/3adf817f8feebb0ef3efa92375adb177673acc46.jpeg)

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [January 16, 2025, 12:21am UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/13 "2025-01-16T00:21:04Z")

</div>

Create a query targetting the readings table, in the facilities, filter by:

- Date is within today.
- Facility ID is this row \> facility.

If the query is not empty, meaning a reading has been recorded for today, don’t allow them to add another one.

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 17, 2025, 7:10pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/14 "2025-01-17T19:10:35Z")

</div>

@ThinhDinh thanks for the suggestion. I am not sure how to implement this. I am assuming it must be done “on submit” of the form so I can message the user but there does not seem to be an option to generate a query like you are suggesting. I only see a “Query JSON” option.

Am I looking in the right place?

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [January 17, 2025, 8:16pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/15 "2025-01-17T20:16:01Z")

</div>

You are better off not allowing the user to open the form if there is already an entry for toda instead of trying to do cleanup afterwards. Only show the form button if the query is empty.

@ThinhDinh is referring to a Query column in the table of the screen that would have the form button.

---

<div class="post-metadata">

**Author:** ![bmagnotta](https://avatars.discourse-cdn.com/v4/letter/b/e19b73/32.png) [@bmagnotta](https://community.glideapps.com/u/bmagnotta)\
**Post date:** [January 23, 2025, 5:39pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/16 "2025-01-23T17:39:37Z")

</div>

@Jeff_Hager and @ThinhDinh thanks for all your help. The app is coming together nicely. I have another issue - I need to produce month-end PDF documents that are sent to the NYS health dept. I have the 3rd party tool DocsAutomator connected to the app and working. As you know I have several tables that are linked together through a common field. My issue is that I need to pull data from multiple tables to populate the PDF that I am generating. It appears the “Generate Document” action within Glide only allows you to link to one table. Do you have any suggestions on how to resolve this issue?

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [January 23, 2025, 11:49pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/17 "2025-01-23T23:49:43Z")

</div>

Been a long time since I touch DocsAutomator, but my general rule is as long as you can establish relations, you can construct JSON objects to be sent, making sure it fits with DocsAutomator’s syntax.

---

<div class="post-metadata">

**Author:** ![Rupert](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/rupert/32/44178_2.png) [@Rupert](https://community.glideapps.com/u/Rupert)\
**Post date:** [February 19, 2025, 10:01pm UTC](https://community.glideapps.com/t/fetching-data-from-a-previous-row-using-date/79073/18 "2025-02-19T22:01:26Z")

</div>

@bmagnotta Finding this a bit late, but looking up values from multiple tables can definitely work. Let me know if you need help with it!
