Get "Week of 2nd" from Date field in power BI - data-visualization

I have a date field in my DimDate table.
I want to get another column WeekOf that would show the week number based on Monday.
For example I have date:
Date WeekOf
10/2/2017 Week of 2nd
10/9/2017 Week of 9th
10/16/2017 Week of 16th

Creating a new column with the following formula should give you what you want.
If you every want to change it to be the Week of a different day, change the 2 in the TargetDate variable to which ever day of the week you want.
WeekOf =
VAR TargetDate = DAY(DATEADD(Dates[Date], 2 - WEEKDAY(Dates[Date]), DAY))
VAR TargetDateText = CONCATENATE(TargetDate, SWITCH(TargetDate, 1, "st", 21, "st", 31, "st", 2, "nd", 22, "nd", 3, "rd", 23, "rd", "th"))
RETURN
CONCATENATE("Week of ", TargetDateText)

Related

How do you extract the date format "Month_name date, year" into separate columns of date, month and year in Pandas? For eg. "August 30, 2019"

I've seen extractions of date, month and year from data format: "DD-MM-YYYY" and the like. (Where the month is numbered rather than named)
However, I have a dataset which has date values in the format: "Month_name date, year".
Eg. "August 30, 2019".
Assume that your DataFrame contains TxtDate column, with
date strings:
TxtDate
0 August 30, 2019
1 May 12, 2020
2 February 16, 2020
The first step is to convert the source column to datetime type and save it
in a new column:
df['Date'] = pd.to_datetime(df.TxtDate)
This function is so "clever" that you can do even without explicit
format specification.
Then extract partilular date components (and save them in respective
columns):
df['Year'] = df.Date.dt.year
df['Month'] = df.Date.dt.month
df['Day'] = df.Date.dt.day
And the last step is to drop Date column (you didn't write
that you need the whole date):
df.drop(columns='Date', inplace=True)
The result is:
TxtDate Year Month Day
0 August 30, 2019 2019 8 30
1 May 12, 2020 2020 5 12
2 February 16, 2020 2020 2 16
Maybe you should also drop TxtDate column (your choice).

DateTimePicker only for current Month

I have a DateTimePicker in a Form. I need to restrict the user to only select a Date from the current Month: for example, from 1 November 2018 to 30 November 2018.
I tried these lines of code:
DtpIssue.MinDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
DtpIssue.MaxDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
But it shows only the current date: 10 November 2018.
What should I do to fix the current Month for the DateTimePicker?
The last day of the month is returned by DateTime.DaysInMonth(), passing the current Year and Month.
Dim FirstOfMonth As Date = New DateTime(Date.Now.Year, Date.Now.Month, 1)
Dim EndOfMonth As Date =
New DateTime(Date.Now.Year, Date.Now.Month, Date.DaysInMonth(Date.Now.Year, Date.Now.Month))
In a similar format, applied to your controls:
DtpIssue.MinDate = New Date(Today.Year, Today.Month, 1)
DtpIssue.MaxDate = New Date(Today.Year, Today.Month, Date.DaysInMonth(Today.Year, Today.Month))

Get last day of previous quarter

DateAdd("m", -(Month(Date) - 1) Mod 3 - 1, Date)
This gives me 6/28/2015 while I need 20150630 . I can work on the format part but don't know how to get the last day of previous quarter instead of today's date of last quarter.
This should work for VBA. It just determines the first day of the current quarter and then subtracts one day.
Debug.Print DateAdd("q", DatePart("q", Date) - 1, "1/1/" & Year(Date)) - 1

SQL-Automatically Bucketize all dates into that weeks monday date-

all- I'm looking to automatically standardize a certain date field into the monday date of that week. Essentially, if an entry came in with a date in this field of Tuesday, July 30th, 2013- I would want to standardize it to Monday, July 30th, 2013. I'd like to be able to apply this to only dates in a certain column where entries may have more than one date in seperate columns.
Thank you!!
If you want Monday of the current week you can use the following in SQL Server:
SELECT DateAdd (Day, 2 - DatePart (dw, YourDate), YourDate)
Depending on desired behavior, you may have to use SET DATEFIRST 2 to adjust the behavior.
List<List<myRecord>> recordList = new List<<myRecord>>();
List<DateTime> mondays = new List<DateTime>();
DateTime first = new DateTime(2013, 1, 30);
DateTime last = new DateTime(2013, 7, 29);
for (Date mon = first; mon <= last; mon = mon.AddDays(7)) {
recordList.Add(new List<myRecord>([SELECT * FROM myRecord WHERE myRecord.Date >= mon AND myRecord.Date < mon.AddDays(7)] ))
}
foreach (List<myRecord> monList : recordList) {
//do stuff with each monday bucket
}
dunno what technology youre using but maybe something like this would work?

Getting the first and last dates in a month

I have two comboboxes that lets the user select a date and year. What I want to do is to translate this into my query in access, which requires a [start date] and an [end date] parameter.
I.E. user picks "May" and "2013" in the combo boxes
My query is setup between [start date] and [end date], so I want to translate this month and year selection from the combo boxes into two strings (startdate and enddate, then pass them as command parameters) that contain MM/DD/YYYY, MM/DD/YYYY. What is the best way to take two strings and get the first valid day and last valid day. I have:
FirstDayInMonth = DateSerial( _
Year(Date), Month(Date), 1)
LastDayInMonth = DateSerial( _
Year(dtmDate), Month(dtmDate) + 1, 0)
But I need to make the switch from a string into a date format to get back the first/last day of the selected month, using only the month ("MAY") and year ("2013")? Am I missing something relatively simple?
Make the "Month" combo box have two columns (Column Count property is 2):
1 | Jan
2 | Feb
...
12 | Dec
Set the Bound Column of the combo box to 1 so its .Value is the month number, but display only the month name (hide the number) by setting the width of the first column to zero:
Column Widths: 0";0.75"
So, do you get the month and year (as integer or long, not a date yet) from the comboboxes?
Then try this:
Dim m As Long
Dim y As Long
Dim d_from As Date
Dim d_until As Date
m = CLng("01") ' get these two from your comboboxes
y = CLng("2013")
d_from = DateSerial(y, m, 1)
d_until = DateAdd("m", 1, d_from)
d_until = DateAdd("d", -1, d_until)
When you have the first date, you calculate the second (last day in month) by adding one month, then going one day back.