Hi all,
I am getting stuck with a quite basic task: I want to calculate the 12-month running sum for a few metrics, starting from data aggregated daily. I ran some tests using windowSum but I cannot crack the case.
The original data is more or less in this format:
Date | User | Transaction | Amount |
---|---|---|---|
2024-01-01 | abc | Buy | 100 |
2024-01-01 | abc | Sell | 200 |
2024-01-02 | abc | Buy | 50 |
2024-01-04 | abc | Sell | 20 |
2024-01-01 | def | Buy | 20 |
2024-01-04 | def | Sell | 10 |
2024-01-05 | def | Buy | 10 |
2024-01-09 | def | Sell | 50 |
So daily aggregated data by user and transaction type.
I want to get the running sum so that at 2024-07-04 it will return the total for the user starting from 2024-07-04. Something like this:
Date | User | Transaction | Amount |
---|---|---|---|
2024-01-01 | abc | Buy | 100 |
2024-01-01 | abc | Sell | 200 |
2024-01-02 | abc | Buy | 150 |
2024-01-04 | abc | Sell | 220 |
2024-01-01 | def | Buy | 20 |
2024-01-04 | def | Sell | 10 |
2024-01-05 | def | Buy | 30 |
2024-01-09 | def | Sell | 60 |
Going back 12 months per each user.
As you can see, in my data there are days in which a user has no transactions, so what I need to base my calculation is the date. In SQL I would use a simple SUM (OVER) windows function.
In this instance I thought that I could use windowSum, like this:
windowSum(sumIf({Amount}, {Transaction} = "Buy"),[truncDate("MM", {Date}) ASC],12,0)
But this won’t do the trick.
One reason, I guess, is that I would need something like addDateTime
to get the relevant dates, but I cannot figure out how to get there.
I also looked into runningSum
but still, cannot figure out how to make the calculation work for the last 12 months. Also considering that I do not want calendar months, i.e. if I am at the 15th of July 2024 then I want to start calculating from the 15th of July 2023, and not just truncate the date at month level, then going from full July 2023 to 15th of July 2024.
The older posts and documentation didn’t help me much (mostly refer to starting from the same level of time granularity), so I’d like to ask if anyone can point out to the right direction here.
Thanks!