Group date by week number in the form YYYY-MM-WW - postgresql

Currently have rows aggregated by week number.
SELECT to_char(date, 'IYYY-MM-IW') AS week, from TABLE GROUP BY week
The results will show the form "2021-07-29". Is it possible to change the week number such that it is the number week of the month (instead of year).
For example, instead of "2021-07-29", we convert to "2021-07-04" since the 29th week of the year is actually the 4th week of the month.

Quote from the manual
W week of month (1–5) (the first week starts on the first day of the month)
So you can use:
to_char(date, 'YYYY-MM-W')
For e.g 2021-10-18 this yields 2021-10-3 (third week in October)

Related

How to horizontally use an arrayformula to increment the week number based on a range of days?

https://docs.google.com/spreadsheets/d/1Z9Wn9Kpe7nAR4nwfpPSNtvIAOkTtTp7IxV7WHp9a84M/edit?usp=sharing
I have a spreadsheet for the year. Every day is represented as its date and as its day of the week (bleu rows in the above spreadsheet). There is a blank column at the beginning of every month (ie, the "Febuary" column (greyed out in the above spreadsheet) doesnt increment the day of the week or the date).
What I want to do is, I want to look at either the day of the week or the date and write in above Sundays the week number. So "Wk01, Wk02, Wk03" up to Wk52. An example is marked in red on the link above. These "Wk"s would have 6 blanks between them (or 7 if the week is across the greyed out month column).
Use weeknum(), like this:
=arrayformula( if( B11:11 = "Sun", "Wk" & weeknum(B12:12), iferror(1/0) ) )

Group by week of the year in Tableau

I have a table where I only have week number and year (and another not timing variables).
Week number : I have it as 1, 2,3,4,5 up to 53.
Year : 2020, 2021, etc
Therefore for ever week-year I have a row.
Goal:
I have seen that in Tableau you can show by week too but showing the first day of the week (see screenshot).
How to create/convert a column/existing columns so that first day of week is shown as date instead.
Thanks,
You can do this. Try this formula:
DATETRUNC('week',DATEPARSE("w-yyyy",STR([Week])+"-"+ STR([Year])))
DATEPARSE converts your week and year to a date.
DATETRUNC returns the first date of each week.

Start of Week in Amazon Redshift (Sunday Start of the Week)

I need to convert Week to start with Sunday (range 1-53, with a Sunday in this year)
Ex. DATE_PART(w,'2020-01-05') (Sunday)
This should return 2 instead of 1.
Thanks in advance.
If you wish to obtain the Week Number where a week starts on Monday instead of Sunday:
Subtract one day to the date being convert to the Week Number
So, use:
DATE_PART(w, date_field - INTERVAL '1 DAY')
This returns the Week Number of the 'previous day'.
Unfortunately, it won't work for the first day of the year, which will appear to be the last day of the previous year. Depending on how your organization treats the first/last weeks of the year, this might be okay.
If that is not satisfactory, then you could write your own User-Defined Function (UDF) and code it to return the values you desire.

Crystal Reports: Display date range (month, first day of month, year to month, last day, year) based on field value

I need to display a date range based on the month before a date in a field. For example, if the date value (DOCDATE) is 6/6/18, I'd like the range to display as:
5/1/18 to 5/31/18
I was able to use LASTFULLMONTH with minimum and maximum constraints and it works fine for records in the current month, but I need to use DOCDATE as a control.
You can achieve this with the following formulas.
Note the formula names, because they are partly dependent on each other.
{#prevMonth}
Gets the previous month by subtracting one month from {#DOCDATE}
DateAdd("m", -1, {#DOCDATE})
{#startDate}
Gets the start-date (first day of previous month) by creating a new date from the year and month of {#prevMonth} and day 1.
Date(Year({#prevMonth}), Month({#prevMonth}), 1)
{#endDate}
Gets the end-date (Last day of previous month) by creating a new date from the year and month of {#DOCDATE} and day 1, minus one day
Date(DateAdd("d", -1, Date(Year({#DOCDATE}), Month({#DOCDATE}), 1)))
{#displayRange}
Shows the range.
CStr({#startDate}) & " to " & CStr({#endDate})

Extract matching year for week number in postgresql

In postgresql extract(week from '2014-12-30'::timestamp) gives week number 1 of 2015. How do I extract the associated year that corresponds to the week number? Using extract(year ... gives 2014
Since week is the ISO-defined week, you want the isoyear:
#= select extract(isoyear from '2014-12-30'::timestamp);
date_part
-----------
2015
(1 row)
From the docs:
By definition, ISO weeks start on Mondays and the first week of a year
contains January 4 of that year. In other words, the first Thursday of
a year is in week 1 of that year.
In the ISO week-numbering system, it is possible for early-January
dates to be part of the 52nd or 53rd week of the previous year, and
for late-December dates to be part of the first week of the next year.
For example, 2005-01-01 is part of the 53rd week of year 2004, and
2006-01-01 is part of the 52nd week of year 2005, while 2012-12-31 is
part of the first week of 2013. It's recommended to use the isoyear
field together with week to get consistent results.
According to the postgresql documentation for extract week:
The number of the week of the year that the day is in. By definition
(ISO 8601), the first week of a year contains January 4 of that year.
(The ISO-8601 week starts on Monday.) In other words, the first
Thursday of a year is in week 1 of that year. (for timestamp values
only)
Based on this, you might have to do a little logic:
case when extract(week from '2014-12-30'::timestamp) = 1
and extract(month from '2014-12-30'::timestamp) = 12
then extract(year from '2014-12-30'::timestamp)+1
else extract(year from '2014-12-30'::timestamp)
end