# How to Calculate a difference between rows (not columns)

**URL:** <https://community.amazonquick.com/t/how-to-calculate-a-difference-between-rows-not-columns/36040>\
**Category:** Q&A\
**Tags:** calculations, Business-Intelligence-Engineer, quick-sight\
**Created:** [September 24, 2024, 3:05pm UTC](https://community.amazonquick.com/t/how-to-calculate-a-difference-between-rows-not-columns/36040 "2024-09-24T15:05:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlinsin](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mlinsin](https://community.amazonquick.com/u/mlinsin)\
**Post date:** [September 24, 2024, 3:05pm UTC](https://community.amazonquick.com/t/how-to-calculate-a-difference-between-rows-not-columns/36040/1 "2024-09-24T15:05:01Z")

</div>

See below the table I am working with  
This is a time series of actions and I try to understand how I can calculate the time difference between the individual rows that are returned

How can I calculate the difference between the time stamp in  
row 2 and row 1  
row 3 and row 2  
row 4 and row 3  
etc  
thanks!

|Step|Location|User|Time Stamp|Difference in Minutes|  
|Step 1|Valve A|Frank|12/22/2021 8:00|0|  
|Step 2|Valve B|Mary|12/22/2021 10:00|120|  
|Step 3|Valve C|John|12/23/2021 10:00|1440|  
|Step 4|Valve A|Bill|12/23/2021 10:31|31|  
|Step 5|Valve C|Carl|12/23/2021 10:45|14|

 ![image](https://d1wl4jav5dp96l.cloudfront.net/original/3X/6/7/6716ba4d0fab9affbfbeeb54165e910c4ed1d289.png)

---

<div class="post-metadata">

**Author:** ![robdhondt](https://d2orw1595917va.cloudfront.net/user_avatar/community.amazonquick.com/robdhondt/32/194_2.png) [@robdhondt](https://community.amazonquick.com/u/robdhondt)\
**Post date:** [September 24, 2024, 7:38pm UTC](https://community.amazonquick.com/t/how-to-calculate-a-difference-between-rows-not-columns/36040/2 "2024-09-24T19:38:51Z")

</div>

Hi @mlinsin,

Here is an example solution to your question. You can see the implementation in this Quick Sight Arena dashboard: [Link to Arena Dashboard](https://community.amazonquick.com#Arena-dashboardId=eb7921e6-55b4-4ce3-864b-9676ad69201e&authorId=CU-113).

### Steps to Solve:

1. **Create a Lag Timestamp Field** :

2. **Calculate the Time Difference** :

3. **Handle Null Values** :

### Note on Table Calculations:

These are considered **table calculations** in Quick Sight, meaning they operate on the data that is visible in your visual. For the `lag` function to work correctly, both the previous and current rows must be present in your visual. If the previous row is not displayed in your visual, the `lag` function won’t be able to retrieve its value, and the calculation won’t work as intended.

---

<div class="post-metadata">

**Author:** ![mlinsin](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mlinsin](https://community.amazonquick.com/u/mlinsin)\
**Post date:** [September 27, 2024, 3:50pm UTC](https://community.amazonquick.com/t/how-to-calculate-a-difference-between-rows-not-columns/36040/3 "2024-09-27T15:50:23Z")

</div>

thanks a lot for the hint, I was not aware of the lag function and this did the trick!!
