Rolling 12 months regardless of year in Django - django

How would I go about setting up a rolling 12 month period in Django without any year.
I am trying to plot the academic year but the problem I am finding mainly due to my lack of knowledge is that I have to set the year in datetime
I would like to try and create a start and end date for the academic year which runs from the beginning of April to the end of March every year however I have to include the year in all the objects such as datetime.
Is there a way for me to set a start and end date regardless of the year, the only way I can think of so far is to take the end of March as a start date and then count 12 months from then.
As always, thank you for the help.

timedelta might be help full. check out documentation
import datetime
start_date = datetime.datetime.now()
end_date = start_date + datetime.timedelta(days=365)

Related

How to sort , filter and display model data using week number in django

I would like to sort my model's data on a week number for example when user logins user will see current week number and associated data and user should be able to select previous week number and get all past data as well also week starts on Sunday and ends on Saturday (considering 1 week)
Select Week :[2021 - 30 ] <- week number
You can filter it such way:
from datetime import datetime, timedelta
itemsList = Model_Name.objects.filter(user=request.user, created_date__gte=datetime.now()-timedelta(days=7))
I have got similar problem in my task and I solved it in simple steps:
#django #django-models #django-views #problem-solving
def get_weeks(request):
week = now().isocalendar()
if Event.objects.filter(start_date__week=week.week).exists():
instances=Event.objects.filter(start_date__week=week.week)

Same Period Last Month

I'm struggling to get the same period of last month.
I want to compare the current month period, for instance, today is 16June2021 so I want to get the sales from May 1st to May 16th.
I'm using this formula but I get the whole month total:
Prev MTD = calculate(sum(Sales[Sales_Amount]),DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH))
Creating a table with DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH), I also get every day of last month.
Is it mandatory to use a Date Table? Already tried but the results came empty.
Is it something regarding my date format? From the import it comes as date/time format.
Thank you very much
Try with:
Prev MTD = calculate(sum(Sales[Sales_Amount]),filter(ALL(Sales[Sale_Date]),Sales[Sale_Date] >= DATE( YEAR(TODAY()), MONTH(TODAY())-1, 1) && Sales[Sale_Date]<=today()))
where order_date between '2022-08-01' and DATE_SUB(CURRENT_DATE(),32)
last month 31 days that's why i am calculated 32 days.
if last month 30 day than you need to calculate 31 days.

Seperating sickness between months in PowerBI

I have set up a table showing number of sick days based on absence start and finish date in Power BI. The date tables have been set up.
I am having an issue with sick days that continue in the following month, for e.g .
Absence Start Date
5 May 21
Absence End Date
5 June 21
My table sums all the absence days in May.
How do I allocate the 25 days in May and then the remaining go to June even though it is one occurrence?
How Data is being summarised
Unfortunately you don't provide enough information about your model/ data. Consider this example. Here is my dummy table:
Table 3 =
var __baseTable =
DATATABLE("StartD",DATETIME, "EndD", DATETIME,
{
{"2020-01-28","2020-02-24"},
{"2019-12-23","2020-01-31"},
{"2020-01-20","2020-02-17"}
}
)
return
ADDCOLUMNS(GENERATE(__baseTable, CALENDAR([StartD],[EndD])),"Month", FORMAT([Date],"yyyy-mm"), "WorkDay", IF(NOt(WEEKDAY([Date]) in {1,7}) ,1,0))
And here are measures (calculating days without a weekend, because I don't have Calendar with businessHoliday):
SumOfSickDays = CALCULATE(SUM('Table 3'[WorkDay]))

Previous Week Measure - Week 52 2019 and Week 1 2020

I currently use the below measure to retrieve a value for the previous week, however as it's now week 1 of a new year, the field is returning "blank" as it cannot find any previous weeks.
How would I adapt my measure to include something along the lines of... if week 1 use the last week of the previous year, if not use the previous week.
VAR CURRENT_WEEK = WEEKNUM(TODAY()) return
CALCULATE(AVERAGE(DATA_TABLE[VALUE]),
FILTER (DATA_TABLE, WEEKNUM(DATA_TABLE[DATE]) = CURRENT_WEEK -1))
Thanks in advance for your help
I would suggest using a start date and calculating the weeknum from that date. For example, if you choose 1 Jan 2018 as start date, then 1-7 Jan 2018 would be week 1 and first week in 2020 would be week 105. This would solve your issue.
The method you are using becomes really hard to handle if your data has multiple years. Weeknum 1 would denote the 1st week of 2020, 2019 and all the other years. This will mess up the calculation. The above method will make sure you have a unique number for a week.

Django Query same week last year

I need to compare sums of events in the same week (isocalendar), year over year.
class MyModel(models.Model):
date = models.DateTime(...
hits = models.IntegerField(...
I've got this sorted for last year, last month, same month last year, same day last year using variations on:
same_month_last_year = MyModel.objects.filter(date__month = (datetime.datetime.now().month), date__year = (datetime.datetime.now() - relativedelta(years = 1)).year).aggregate(total=Sum('hits')['total']
I don't see an equivalent 'week' function. I can use .isocalendar()[1] on the right side of the equation but that's of no help on the left. Any ideas? Thanks.
Django 1.11 added a week filter. Can you use that?
If not, you might be able to look at the source code and cherry pick it.
As a last resort, you could use Python to calculate the date range for the same week last year, then pass those dates as filters to the Django query.