django date filter time past - django

Is there a nice filter I can use in Django template to allow me to turn a datetime into time period ago from now.
e.g.
{{ mydate | date_ago }}
that would display things like
12 seconds ago
2 hours ago
yesterday
4 days ago
2 weeks ago
12 jan 2001
where by after some distance it just displays the normal date.

timesince

Related

Distinct count basis condition for last 3 months

Outlet ID
Outlet Name
Order Date
Product
Qty
Net Value
Mum_1
Prime Traders
12th Oct 2022
RoundBox
3
300
Mum_4
Avon Trading
13th Oct 2022
Slice 100
10
1000
I have date wise transaction data for past 20 months for retail outlets.
Any outlet that has been billed in the last 3 months can be classified as an 'Available Outlet'.
Eg: Available outlets for Sept 2022 are the ones that have been billed at least once across July, August & Sept 2022.
Similarly I need to have ,month wise availability count in a column chart. Can someone please guide as to how can I write a DAX query for the same ?

Camunda accamulate history data with removal_time < now()

My camunda application accamualated history data with removal_time < now().
There are raws with removal_time from a year ago.
In act_hi_job_log with job_def_type = 'history-cleanup' from 1 to 400 per days.
My settings:
HistoryRemovalTimeStrategy - HISTORY_REMOVAL_TIME_STRATEGY_END
HistoryCleanupBatchSize - 500
HistoryCleanupStrategy - HISTORY_CLEANUP_STRATEGY_REMOVAL_TIME_BASED;
clean windows - 1 hour on working day, 4 hour on weekend days
How to icrease number of removal raws per day?

PowerBI and filtered sum calculation

I should be able to make a report concerning a relationship between sick leaves (days) and man-years. Data is on monthly level, consists of four years and looks like this (there is also own columns for year and business unit):
Month Sick leaves (days) Man-years
January 35 1,5
February 0 1,63
March 87 1,63
April 60 2,4
May 44 2,6
June 0 1,8
July 0 1,4
August 51 1,7
September 22 1,6
October 64 1,9
November 70 2,2
December 55 2
It has to be possible for the user to filter year, month, as well as business unit and get information about sick leave days during the filtered time period (and in selected business unit) compared to the total sum of man-years in the same period (and unit). Calculated from the test data above, the desired result should be 488/22.36 = 21.82
However, I have not managed to do what I want. The main problem is, that calculation takes into account only those months with nonzero sick leave days and ignores man-years of those months with zero days of sick leaves (in example data: February, June, July). I have tried several alternative functions (all, allselected, filter…), but results remain poor. So all information about a better solution will be highly appreciated.
It sounds like this has to do with the way DAX handles blanks (https://www.sqlbi.com/articles/blank-handling-in-dax/). Your context is probably filtering out the rows with blank values for "Sick-days". How to resolve this depends on how your data are structured, but you could try using variables to change your filter context or use "IF ( ISBLANK ( ... ) )" to make sure you're counting the blank rows.

Django ORM group by, and find latest item of each group (window functions)

Say we have a model as below
class Cake(models.Model):
baked_on = models.DateTimeField(auto_now_add=True)
cake_name = models.CharField(max_length=20)
Now, there are multiple Cakes baked on the same day, and I need a query that will return me a monthly cake report which consists of each day of the month, and the names of the first and last cakes baked on that day.
For example, if the data is something like this:
baked_on cake_name
11 Jan 12:30 Vanilla
11 Jan 14:30 Strawberry
11 Jan 20:45 Avocado
12 Jan 09:05 Raspberry
12 Jan 16:30 Sprinkles
12 Jan 20:11 Chocolate
My query's output should look like
date first last
11 Jan Vanilla Avocado
12 Jan Raspberry Chocolate
How should I go about doing this in a single ORM call?
Django 2.0 introduced window functions that are made for that kind of queries. Simple answer for your question will be:
Cake.objects.annotate(
first_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
),
last_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').desc(),
),
day=TruncDate('baked_on'),
).distinct().values_list('day', 'first_cake', 'last_cake')
Why FirstValue in last_cake? That's becaues window query by default will traverse through each row and won't look ahead, so for every row, last row will be equal to current row. Using last_row together with descending sorting will fix that. Either that or you can define frame for which window query should work:
Cake.objects.annotate(
first_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
),
last_cake=Window(
expression=LastValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
frame=ValueRange(),
),
day=TruncDate('baked_on'),
).distinct().values_list('day', 'first_cake', 'last_cake')

Week number calculation in rails

This is my first question on Stack.
I am working on a booking site that relies heavily on searching and finding full weeks of accommodation. Most user searches will be on weeknumber of the year, eg. week 27 for the first week of july.
It is important that the user does not need to fill in year when searching for accommodation, and so the only thing we will get from the user is the weeknumber.
How can I get the year from the week given by the user considering that it always has to be the next upcoming occurrence of that week number?
(There is a gotcha in this. I could get the upcoming week 27 by doing something like this:
def week
week = 27
Date.commercial(Date.current.year + 1, week, 1) # gives the first day of the week
end
But that would only be right until the 1 of January, after that it would be looking for week 27 of 2015.)
You could compare the current calendar week with Date.current.cweek (Reference) with your number.
require 'active_support/core_ext' # Already included in Rails
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013
p calendar_week(1)
# => Mon, 30 Dec 2013 # don't know if that's the way calendar weeks are counted
p calendar_week(27)
# => Mon, 30 Jun 2014