Skip to content
Advertisement

Date filter binned data

I have a model in django that contains a start and end date for a financial year and a value for demand volume.

JavaScript

I would like to filter this data on a given date range that may not coincide with the date ranges of the data, taking a proportion of the demand from each data point that may fall somewhere in the filter date range. I am assuming I need to work out if the data point falls over the edge of the filter and divide the demand by the number of days inside the filter? But I am having a hard time trying to write this. any help would be appreciated! :)

Advertisement

Answer

If you don’t need to do the calculation inside the database you can filter for overlapping date ranges (here is a nice picture of when ranges overlap) and perform the calculations in python when iterating over the queryset:

JavaScript
Advertisement