Hello @ramoshe, I think you are really close! My initial thought to resolve this will require a few different functions.
This will create a fairly complex ifelse statement at the end, but my thought is we will need to figure out how many months away from the Activation Date each month value is, pull in the sales value for the activation date month on each row using a dynamic lag function, and check on every date month column if that number is greater than 0.
Create a field for the Sales Date in Months:
sales_date_month = truncDate(‘MM’, {sales_date})
Create a field to get the number of months from activation month:
monthsFromActivation = dateDiff(truncDate('MM', {activation_date}), {sales_date_month}, "MM")
Now we will write a lag function to pull in the sales for the activation_date month:
activationDateSales = lag
(
sum({sales}),
[{sales_date_month} DESC],
{monthsFromActivation},
[{product}, {sales_date_month}]
)
Here is where there might be an issue. There is a chance that lag function will create a syntax error, if that happens we may be able to find a workaround, but it might also mean it is time to add a “New Product” tag into the dataset custom SQL when you ingest it. Also, you may need to switch [{sales_date_month} DESC] to ASC but I think DESC is correct. I’m also not sure if this partition [{product}, {sales_date_month}] is necessary but it might be.
Now craft the ifelse:
newProductSales = ifelse(
{sales_date_month} > truncDate('MM', {activation_date}) AND {activationDateSales} > 0, 0,
{sales_date_month} > truncDate('MM', {activation_date}) AND {activationDateSales} = 0, {sales},
{sales_date_month} = truncDate('MM', {activation_date}) AND {sales} > 0, 0,
isNULL({sales}), 0,
0
)
newProductSales should give you the result, or get you close to what you are hoping to achieve. I will mark this as the solution, but if you have questions about any errors, let me know!