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
Related
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 ?
I have two simple tables. I need to able to determine who is 'new' as of a particular date (say January) and then count only those attributes. There's a 1:M relationship on name. I basically need to answer the following questions with the below data:
What is the total number of FamilyMembers based on log-in for the month? (Done using custom measure)
Out of the total of #1 - how many have logged in for the first time?
Out of the total of #2 - how many were children? How many were adults?
Log In Table
ID
Name
Date
login1
Sam
Jan
login2
Sam
Jan
login3
Dave
Jan
login4
Dave
Jan
login5
Jack
Jan
login6
Sam
Jan
login7
James
Feb
login8
James
Feb
login9
James
Feb
login10
Sam
Feb
login11
Sam
Feb
login12
Steve
Feb
Contact Table
Name
FamilyMembers
Child
Adult
Sam
3
1
2
James
2
1
1
Dave
4
2
2
Jack
1
0
1
Steve
6
1
5
Using this data, filtered on February we would see Steve never signed in prior to that date, so that makes him 'new'. James is also new.
My closest attempt is the custom 'Count of New Individuals' Measure
VAR currentUsers = VALUES('Log-Ins'[Name])
VAR currentDate = MIN('Log-Ins'[Date])
VAR pastUsers = CALCULATETABLE(VALUES('Log-Ins'[Name]),
ALL('Log-Ins'[Date].[Month],'Log-Ins'[Date].[MonthNo],'Log-Ins'[Date].[Year])
, 'Log-Ins'[Date]<currentDate)
VAR newUsers = EXCEPT(currentUsers,pastUsers)
RETURN COUNTROWS(newUsers)
As you can see this gives me the count of new individuals but I want to count their attributes to say :: Out of the 11 total family members, 8 were new. Out of those 8, 6 were adults and 2 were children.
I may be getting lost in the translation, but I don't understand how exactly you want to display the information.
#ContactsWhoLoggedIN :=
CALCULATE(COUNTROWS(Contacts),FILTER(Contacts,CALCULATE(COUNTROWS(LogIN)>0)))
#NewCWhoLoggedIN :=
CALCULATE(COUNTROWS(Contacts),
FILTER(Contacts,
//LoggedIn in the Current Date Context
CALCULATE(COUNTROWS(LogIN))>0
&&
//Never LoogedIN before the Current Date Context
CALCULATE(COUNTROWS(LogIN),FILTER(ALL(Dates),Dates[Date]<MIN(Dates[Date])))=0
)
)
#CWhoLoggedBackIN := [#ContactsWhoLoggedIN]-[#NewCWhoLoggedIN]
#FM_NewCWLI :=
CALCULATE(SUM(Contacts[FamilyMembers]),
FILTER(Contacts,
//LoggedIn in the Current Date Context
CALCULATE(COUNTROWS(LogIN))>0
&&
//Never LoogedIN before the Current Date Context
CALCULATE(COUNTROWS(LogIN),FILTER(ALL(Dates),Dates[Date]<MIN(Dates[Date])))=0
)
)
I remember this pattern from "Microsoft Excel 2013: Building Data Models with PowerPivot"
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')
I want to generate the current day number using RubyMotion code. I have looked at several IOS solutions but I'm not experienced enough to translate the code successfully to RubyMotion.
I am currently at the following point:
def today
NSDate.today
end
def day_number
NSDate.from_components (day: today)
end
When I run the above it gives me an return of 3852055-06-16 00:00:00 +0100. I thought that the 3852055 part was seconds but it doesn't seem to equate to either todays date or to 16th of June - and in any case why should it be returning 06-16 instead of 02-08?? Totally confused here.
I just want to get todays day number. As I write the date is 2nd August 2014 and the day number should be 214 so I'm obviously way out somewhere.
Any help would be greatly appreciated.
cheers
This will do the trick:
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:NSDate.date)
Now suppose you have the date in the form of a string, and you want to get the day number for it:
datestr = "2014-01-01 11:08:56 +0000"
First create an NSDateFormatter to convert the String into an NSDate
df = NSDateFormatter.new
df.dateFormat = "yyyy-MM-dd HH:mm:ss z"
mydate = df.dateFromString(datestr)
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:mydate)
If your date string is simpler:
datestr = "2014-01-01"
just use a simpler dateFormat string:
df.dateFormat = "yyyy-MM-dd"
I suggest you to take a look at motion-support gem and especially at core-exttime.
You can play with dates as you want:
=> Mon, 04 Aug 2014
(main)> Time
=> Time
(main)> Time.today
=> 2014-08-04 00:00:00 +0200
(main)> Date.today.day
=> 4
(main)> Time.today.day
=> 4
and a lot more.
I have an assignees that I've been working on and I'm stuck on the last function.
use the function void Increment(int numDays = 1)
This function should move the date forward by the number of calendar days given in the argument. Default value on the parameter is 1 day. Examples:
Date d1(10, 31, 1998); // Oct 31, 1998
Date d2(6, 29, 1950); // June 29, 1950
d1.Increment(); // d1 is now Nov 1, 1998
d2.Increment(5); // d2 is now July 4, 1950
I don not understand how to do this.
void Date::Increment(int numDays = 1)
I'm stuck, I know how to tell the function to increment, by the ++ operator but i get confuse when I have to get the function to increment the last day of the month to the the fist, or to end at the last date of that month for example. Oct 31 to Nov 1, or June 29 to July 4. I can do July 5 to July 8 but the changing months confuse me
You will need to store a list (or array) of how many days are in each month. If you add numDays to the current date and it becomes bigger than this, you need to increment the month as well.
For example, we have a date object representing 29 March 2010. We call Increment(4) and add 4 to the day variable, ending up with 33 March 2010. We now check how many days March has and find out it's 31 (eg. daysInMonth[3] == 31). Since 33 is greater than 31, we need subtract 31 from 33 and increase the month, ending up with 2 April 2010.
You will need special handling for February in leap years (any year divisible by 4 and not divisible by 100 unless it's also divisible by 400) and for incrementing past the end of December.
30 days has September, April, June, and November. The rest have 31 days, except for February, which has 28 days except on a leap year (every 4 years, and 2008 was the last one) when it has 29 days.
This should be plenty to get you going.
First, construct a function like
int numDaysSinceBeginning( Date );
which counts number of days elapsed from a well known date (e.g. Jan 1 1900) to the specific Date.
Next, construct another function which converts that day-delta to Date
Date createDateWithDelta( int );
From your example,
Date d2(6, 29, 1950); // June 29, 1950
int d2Delta = numDaysSinceBeginnning( d2 );
Date d2Incremented = createDateWithDelta( d2Delta + 5 ); // d2Incremented is July 4, 1950