Skip to content
Advertisement

DATE_DIFF() in BigQuery to calculate time between rows

I would like to calculate the time delay between several customer purchases. However, each purchase is saved in an individual row. The data set looks similar to the following:

Per customer, I would like to end up with a table that calculates the time-difference (in days) between a certain purchase and the purchase that came before. Basically, I would like to know the time delay (latency) between a customers first and second purchase, second and third purchase, and so on. The result should look like the following:

I am struggling to add the purchase_latency calculation to my current query, as the calculation would require me to do a calculation over rows. Any ideas how I could add this to my current query?:

Advertisement

Answer

Did you try row navigation functions like LAG?

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement