Skip to content
Advertisement

BigQuery – Picking latest not null value within 28 interval

I’m trying to add a column on this table and stuck for a little while

ID Category 1 Date Data1
A 1 2022-05-30 21
B 2 2022-05-21 15
A 2 2022-05-02 33
A 1 2022-02-11 3
B 2 2022-05-01 19
A 1 2022-05-15 null
A 1 2022-05-20 11
A 2 2022-04-20 22

to

ID Category 1 Date Data1 Picked_Data
A 1 2022-05-30 21 11
B 2 2022-05-21 15 19
A 2 2022-05-02 33 22
A 1 2022-02-11 3 some number or null
B 2 2022-05-01 19 some number or null
A 1 2022-05-15 null some number or null
A 1 2022-05-20 11 some number or null
A 2 2022-04-20 22 some number or null

The logic is to partition by Category1 and ID then pick the latest none null value within the past 28 days. If there is no data exist, it’ll be null

For the first row, ID = A and Category 1, it will pick 7th row as they are in the same category, ID and the date difference is <= 28. It skipped row 4th and 6th as the date is too far back and null value.

I’ve tried querying this by

but it’s picking incorrect rows,my guess is this query

is not picking the correct date.. could anyone give me advise/suggestion how I could twick this query?

Advertisement

Answer

Consider below approach

if applied to sample data in your question – output is

enter image description here

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