Add month constraint to date_trunc by week - postgresql

I am trying to truncate several dates of a certain month to the start of the week, except the ones that start in the previous month.
Example:
Wednesday 5 Dec 2012 should be truncated to Monday 3 Dec 2012
Saturday 1 Dec 2012 should not be truncated to Monday 26 Nov 2012
For truncating the dates I am using:
date_trunc('week', dates.d)
The problem is that Saturday 1 Dec 2012 still gets truncated to Monday 26 Nov 2012
Can I add a constraint to it, so that it takes the month into account?
Thanks in advance :)
NOTE: I am using version 9.2

GREATEST(date_trunc('week', dates.d), date_trunc('month', dates.d))

Related

Get a date from ISO week and year in power query

I have a column with a year and a ISO week. I would like to get the corresponding date, but at the moment my formula is wrong.
I have the following table:
Year Week
2020 52
2020 53
2021 1
2021 2
I used in power query editor the following formula:
Date.StartOfWeek(Date.AddWeeks(#date([Year], 1, 1), [Week]), Day.Monday)
and I obtained:
Year Week Date
2020 52 28.12.2020
2020 53 04.01.2021
2021 1 04.01.2021
2021 2 11.01.2021
What I would like to have instead:
Year Week Date
2020 52 21.12.2020
2020 53 28.12.2020
2020 1 04.01.2021
2021 2 11.01.2021
For example, in DAX, this works:
Date = DATE([Year],1,-2)-WEEKDAY(DATE([Year],1,3))+[Week]*7
But I would prefer to have it in power query because my data source needs to be updated regularly. Thank you for your attention!
In case you have the same problem, this works now:
Date.AddDays((Date.AddDays(#date([Year],1,1),-4)),(-Date.DayOfWeek(Date.AddDays(#date([Year],1,1),-4)) + [Week]*7))

List of months between two dates as month & year

How do I get a dynamic list of months down a column between two dates in the month year format in Google Sheets?
example:
Start date: 5/18/2016
End date: 5/24/2019
Jan 2016
Feb 2016
Mar 2016
..
...
May 2019
=ARRAYFORMULA(UNIQUE(TEXT(TO_DATE(ROW(
INDIRECT("A"&DATEVALUE(B1)):
INDIRECT("B"&DATEVALUE(B2)))), "mmm yyyy")))

PowerBI Datesbetween - Line Chart Visualisation

I am trying to plot on a line chart running data for 3 fiscal years. My fiscal year being 30 June and not 31 December
Data for Year 1 [1 July 2015 to 30 June 2016]
Data for Year 2 [1 July 2016 to 30 June 2017]
Data for Year 3 [1 July 2016 to 30 June 2018] This is the current year
The following is what I am hoping to achieve.
3 Year Chart
I am not certain how to achieve this.
I was thinking the following might be of some relevance
Revenue from Start = CALCULATE(Report[Revenue], DATESBETWEEN('Dates'[Date], BLANK(), LASTDATE('Dates'[Date])))
I have had it suggested to set up a new measure "Date2". But I really am not sure how to do this. Also given that I am after a third series would I then need to set up a "Date3"
For Date2 [and Date3 if applicable], how would this/these measures be coded, ie is this logical
Date2 = DATE(2015,07,01)
Date3 = DATE(2016,07,01)
Thanks for any help that can be offered. You will see from my question, I know what I want as an output, but have no idea how to really implement.
I would use the TOTALYTD function, e.g.
Revenue from Start = TOTALYTD ( [Revenue] , 'Dates'[Date] , "30 June 2018" )
This blog post is probably the best description of this and related functions:
https://www.sqlbi.com/articles/time-intelligence-in-power-bi-desktop/

How to subtract two dates in Oracle and extract the year part from the difference?

My typical requirement is that I want to subtract two Dates cast as timestamp. The Minuend (First parameter) is the current date and the Subtrahend(second parameter) is stored separately as DD, MM and YYYY in three columns. The final output should be a discrete year as number. I am playing with something like :
SELECT (TO_DATE('05-DEC-2013') -
CASE LENGTH(CAND_DOB_DD)
WHEN 1 THEN
CAST(TO_DATE('0'||CAND_DOB_DD||'-'||CAND_DOB_MM||'- '||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP)
ELSE
CAST(TO_DATE(CAND_DOB_DD||'-'||CAND_DOB_MM||'-'||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP) / 365 END YEAR
FROM CANDIDATE
The Year part as Integer will be used for a very sensitive calculation. Please suggest if the above piece of SCRIPT will yield the desired result. Thanks in advance.
For most purposes you could just use the Months_Between() function to determine the number of months between two dates, and then divide by 12 etc.. Note that the number of months is an integer when comparing two dates that have the same day of the month or are both the last day of the month.
This is tricky when it comes to leap years.
Do you count 28th Feb 2015 to 28th Feb 2016 as exactly one year, when 28th Feb 2015 to 29th Feb 2016 is one day longer but plainly is a year?
What about 29th Feb 2016 to 28th Feb 2017, or 28th Feb 2016 to 28th Feb 2017?
Think carefully about these boundary cases, but Months_Between() is likely to be your best choice.

MySQL populate empty date values

I am running some query's against our incident logging database for our individual customers. What I want to return is all the month names from 2012 and a count of how many open incidents there have been. This works fine for a customer who has been with us for over 12 months but not for a customer who has been with us for a shorter period of time.
The query I have is this:
SELECT DATE_FORMAT(FROM_UNIXTIME(createdtime/1000), '%Y') as 'Year',
DATE_FORMAT(FROM_UNIXTIME(createdtime/1000), '%M') as 'Month',
count(wo.workorderid) as 'Total Logged'
FROM workorder_threaded wot
inner join workorder wo
on wo.workorderid = wot.workorderid
and wo.siteid = 4806
and DATE_FORMAT(FROM_UNIXTIME(createdtime/1000), '%Y') <> '2011'
where wot.workorderid = wot.thd_woid
GROUP BY DATE_FORMAT(FROM_UNIXTIME(createdtime/1000), '%Y%m')
The output I get is this:
Year Month Total Logged
2012 August 3
2012 September 356
2012 October 212
2012 November 120
I need however the following:
Year Month Total Logged
2012 January 0
2012 February 0
2012 March 0
2012 April 0
2012 May 0
2012 June 0
2012 July 0
2012 August 3
2012 September 356
2012 October 212
2012 November 120
2012 December 0
It doesn't have to be limited by year i.e. the call logging DB has data in this from 2011 so the query can be modified to filter the year range.
I know that this is not populating because the data does not exist and I have seen ways of creating a numbers \ date table and referencing this to populate the date but am struggling to do this. I have also changed around joins and where conditions to no avail.
Thanks in advance.
You need to use LEFT JOIN here