I have the following data structure:
Then I have used the following Tableau function to convert week number and year to first day of the week:
DATE(DATETRUNC('week',DATEPARSE("w-yyyy",STR([Week Number])+"-"+ STR([year_axis]))))
Issue, is that when it gets to week 1 and year 2021, instead of showing the week 04/01/2021, it aggregates this data to the last week of December (28/12/2020).
Anyone knows why?
Thanks!
Yes, depending upon your locale, tableau starts a week either on Sunday or a Monday. Moreover, if a first day of a year is not Monday, then the dates before first Monday are considered as week 0 of that year.
Use of this calculation is suggested in this case (for newer versions of tableau desktop)
DATE(DATETRUNC('week',
IF ISOWEEKDAY(DATEPARSE("w-yyyy",STR([Week Number])+"-"+ STR([Year Axis]))) =1
then DATEPARSE("w-yyyy",STR([Week Number])+"-"+ STR([Year Axis]))
else DATEPARSE("w-yyyy",STR([Week Number]+1)+"-"+ STR([Year Axis]))
END
))
For those versions of tableau where ISOWEEKDAY is not listed, the following calculation is suggested
DATE(DATETRUNC('week',
IF DATEPART('weekday', DATEPARSE("w-yyyy",STR([Week Number])+"-"+ STR([Year Axis]))) =1
then DATEPARSE("w-yyyy",STR([Week Number])+"-"+ STR([Year Axis]))
else DATEPARSE("w-yyyy",STR([Week Number]+1)+"-"+ STR([Year Axis]))
END
))
If first day of Year is Monday then your calculated field otherwise week+1.
See the screenshot where both calculations are compared. (Note for year 2018)
I hope this solves your problem.
Related
I want to find out the previous weeks's last day in Teradata SQL using Sunday as the last day of the week. For example, today is Friday 1/27, so the last week ended on Sunday (1/22) and I would want to return 2023-01-22.
Other examples:
If current date is '2023-01-02', then the output I require is '2023-01-01'
If current date is '2023-01-18', then the output I require is '2023-01-15'
With Hive query I would use:
date_sub(current_date, cast(date_format(current_date, 'u') as int));
What would the equivalent be in Teradata? I've tried using the code below but it seems to return the date of the closest Sunday instead of the date of the previous Sunday.
SELECT ROUND(current_date, 'd') (FORMAT 'yyyy-mm-dd');
There are several ways:
Probably the best one is one of the built-in functions to return the previous xxxday <= the input date:
Td_Sunday(Current_Date - 1)
Or the function to return the next xxxday > input date:
Next_Day(Current_Date - 8, 'sun')
Truncating is least understandable:
Trunc(Current_Date, 'IW') -1
TRUNC supports three variations, only IW is usable, but restricted to Monday as week start:
IW: the Monday of the ISO week
WW: the same day of the week as January 1st of the year
W: the same day of the week as the first day of the month
You can use the trunc function to return the first day of the a week, month, ect.
select trunc(current_date -7 ,'IW')
Current date today is 2023-01-27. This will return 2023-01-15, the previous Sunday.
EDIT: Sorry, meant to use the ISO week. As Dnoeth points out, the regular week option doesn't work consistently (which I didn't know, never used it for this before). Anyhoo, his answer is better than mine...
I've been having a look around but I can't seem to find anything that answers this question. I want to calculate the date for the last week of every month. For example, the date for the last week of April 2021 is 26-04-2021. I want a date, not a week number.
I use Google Big Query, I do have a calendar table I could use to extract year and month.
Thanks,
Emily
Try date_trunc:
SELECT date_trunc(last_day(month1st, month), week(monday))
from unnest(GENERATE_DATE_ARRAY('2021-01-01', '2021-12-01', interval 1 month)) AS month1st;
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.
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.
I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?