Filter on first events in period
I have a requirement to filter a fact table by the first event for each user in a period.
e.g with user A B and C
1: user A - 02 jan 2024
2: user B - 04 jan 2024
3: user A - 05 jan 2024
4: user B - 01 feb 2024
5: user C - 02 feb 2024
If I filter on Jan 24, I want to return events 1 and 2 (excluding 3)
If I filter on Feb 24, I want to return events 4 and 5
If I filter on Year 24, I want to return events 1 2 and 5
My initial solution was to create a custom field on the fact table with that definition
Rank: rank() over (partition by "user" order by "event date") and filter on the field = 1
unfortunately, despite displaying the correct Rank, when it comes to filtering, the translation engine applies the filter on the field without applying the Event Date filter, meaning that it would always return event 1, 2 and 5.
Any other idea?
Hi David,
It's possible that Assaf's multi-pass solution might work. However we have internally decided that the generated SQL would be too costly to run on a dashboard refresh so we refactored our solution.
Instead of ranking the events to identify the first in the requested period, we have pre-computed the "previous event date" at DB level and changed our filters to check that the "previous date" is before the requested period's "From date".
It is functionally equivalent and cheaper to compute at refresh time.