Year-to-Date comparisons

Answered
Florian asked on November 27, 2025

Hello dear flexmonster team!

 

I was wondering how to achieve a year-to-date comparison. I would like to see the sum of a value in the previous 3 years up until a certain - or at least today's - date.

 

ie I would like to compare the total revenue of 2023, 2024 and 2025 but only ever up to November 27 (or preferably any date I choose) for each year.

 

kind regards,

Florian

2 answers

Public
Nadia Khodakivska Nadia Khodakivska Flexmonster November 28, 2025

Hello Florian!

Thank you for contacting us.

The idea is to preprocess the data by adding a new field that will contain the day number in the year.

For instance:

// date is converted into its ordinal number within the year 
function getDayNumber(date) {
const start = new Date(date.getFullYear(), 0, 0);
const diff = date - start;
const oneDay = 1000 * 60 * 60 * 24;
const dayOfYear = Math.floor(diff / oneDay);
return dayOfYear;
}

// preprocessing each record by adding a new "dayOfYear" value
function addDayOfYear(records) {
return records.map((r) => {
const d = new Date(r.date);
const dayOfYear = getDayNumber(d);
return {
...r,
dayOfYear,
};
});
}

function getData() {
let data = [
{ date: "2022-01-18", revenue: 1600 },
{ date: "2022-05-09", revenue: 2100 },
{ date: "2022-09-27", revenue: 2900 },
{ date: "2022-11-29", revenue: 3400 },
{ date: "2023-01-15", revenue: 1200 },
{ date: "2023-03-10", revenue: 900 },
{ date: "2023-11-05", revenue: 3000 },
{ date: "2023-12-20", revenue: 5000 },
{ date: "2024-02-01", revenue: 1500 },
{ date: "2024-04-17", revenue: 1100 },
{ date: "2024-10-30", revenue: 2700 },
{ date: "2024-12-11", revenue: 4200 },
{ date: "2025-01-12", revenue: 1800 },
{ date: "2025-03-22", revenue: 1400 },
{ date: "2025-11-20", revenue: 2500 },
{ date: "2025-12-29", revenue: 5100 },
]
return addDayOfYear(data);
}

Then, you can add this field to the report filters area and filter dynamically using, for example, today's date:

slice: {
reportFilters: [
{
uniqueName: "dayOfYear",
filter: {
query: {
less: getDayNumber(new Date()),
},
},
},
],
// other properties
}

Here is an example illustrating the idea: https://jsfiddle.net/flexmonster/qtxwmLe1/.

Please let us know if it works for you. Looking forward to hearing from you.

Kind regards,
Nadia

Public
Nadia Khodakivska Nadia Khodakivska Flexmonster January 15, 2026

Hello Florian,

Hope you are doing well.

We were wondering if you had a chance to check the suggested approach. Please let us know if our response helped.

Looking forward to hearing your feedback.

Kind regards,
Nadia

Please sign in or register to submit your answer