Skip to main content
Announcements
Join us on Sept. 17 to hear how our new GenAI Assistant empowers data professionals: REGISTER
cancel
Showing results for 
Search instead for 
Did you mean: 
melperso21
Contributor II
Contributor II

Rango de meses anteriores con la ayuda del set analysis

Hola Comunidad, necesito su ayuda, estoy aplicando un código con set analysis, con el objetivo de escoger una fecha y luego seleccionar el mismo mes de hace un año atras, para esto estoy usando el siguiente código: count({$<event.montFDate={"$(=monthname(max(event.montFDateP),-12))"}>}distinct(order_id))

Ahora necesito aplicar una misma lógica, pero lograr obtener el total del mes de hace un año atras y los dos meses anteriores, es decir, si escojo ahorita el mes de enero del 2024, esta lógica logre tener la suma de enero/2023, diciembre/2022 y noviembre del 2022

Espero que mi duda quede clara y poder tener ayuda de ustedes.

 

Saludos.

Labels (3)
5 Replies
pravinboniface
Creator II
Creator II

Please try this:

count({$<event.montFDate={">=$(=addmonths(max(event.montFDate),-14))<=$(=addmonths(max(event.montFDate),-12))"}>}distinct order_id)
melperso21
Contributor II
Contributor II
Author

Hola @pravinboniface 

Probe, pero me da valor 0

pravinboniface
Creator II
Creator II

Can you please share the format of event.montFDate?  I think there may be a date format mismatch with what I tried.  I tested with data looking as follows

load * inline [
event.montFDate,order_id
1/1/2023,1
2/1/2023,2
];

melperso21
Contributor II
Contributor II
Author

Hola @pravinboniface 

El formato que uso es el siguiente:

melperso21_0-1709901691910.png

 

Saludos.

 

pravinboniface
Creator II
Creator II

@melperso21 If you have a date in the format 'MMM YYYY', the approach will not work even if we cast the dates to be in that format.

For example, we could code something like this where we format the date as 'MMM YYYY'

count({$<event.montFDate={">=$(=date(addmonths(max(date#(event.montFDate,'MMM YYYY')),-14),'MMM YYYY'))<=$(=date(addmonths(max(date#(event.montFDate,'MMM YYYY')),-12),'MMM YYYY'))"}>}distinct order_id)

But this will resolve to

count({<event.montFDate={">=Dec 2021<=Feb 2022"}>}distinct order_id)

This will not work because the greater than and less than will treat the date in alphanumerical order.

The best way to do this is to first convert event.montFDate to a date using date(date#([event.montFDate],'MMM YYYY'),'YYYYMMDD')

Then apply the original expression.

count({$<event.montFDate={">=$(=addmonths(max(event.montFDate),-14))<=$(=addmonths(max(event.montFDate),-12))"}>}distinct order_id)

 

I hope this helps