cube-js / cube

📊 Cube — Universal semantic layer platform for AI, BI, spreadsheets, and embedded analytics
https://cube.dev
Other
17.96k stars 1.78k forks source link

How to calculate rolling sum within a calendar year #8975

Open SabinaHisamova opened 1 day ago

SabinaHisamova commented 1 day ago

At the moment, rollingWindow leading and trailing parameters supports values in the following format: (-?\d+) (minute|hour|day|week|month|year) which can be used to define the window size to the right and left of the offset.

I need to calculate the accumulated sum of parameter (income_per_mth) within a year, i.e. start calculating the accumulated parameter again with the onset of January of the following year. The granularity of the data is the month.

I tried to formulate this logic as follows:

accum_param : {
    title: 'accum_param_year',      
      sql: `income_per_mth`,        
      type: `sum`,  
      rolling_window: {
        offset: 'end',
        trailing: '1 year'
      },
}

But it doest start over the calculation in January of the next year. It works with a granularity of a month, i.e. it counts the accumulated parameter within 12 months, and not within a certain year. For example, for March 2024, the calculation will start from March 2023, but I need it to start from January 2024.

Please suggest how I can solve my problem.