Get a date from ISO week and year in power query - date

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))

Related

DAX Expression for calculating end of every year

I am looking for DAX expression to show me monthly figure for last financial years. Here is an example of the information;
The figure is accumulated since the starting of 31 Jan 2017 or earlier which I have no information. The data I have does not have a monthly view but Year to Date (YTD).
How could I structure the DAX so that it would be able to find out what is the figure for the month.
For example, Jan 2017 $2,000 , Feb 2017 $5000 (included figure from Jan 2017), March 2017 $8000 (included Jan 2017 and Feb 2017).
How can it be done to have the monthly view ?
Date............. Balance Sheet Amount.......View
31-Dec 2017................24,000.......................YTD *1
31-Jan 2018................24,010.......................YTD *1
28-Feb 2018................24,310.......................YTD *1
31-Dec 2018................30,000.......................YTD *2
31-Dec 2019................31,000.......................YTD *3
31-Dec 2020................40,000.......................YTD *4
30-June 2021................5,000.......................YTD *5
Let's say your current table does not contain previous balance, then it will be easier to obtain the current month movement with the following formula:
Current Month = Sheet1[YTD Bal] -
CALCULATE(SUM(Sheet1[YTD Bal]),
FILTER(Sheet1,Sheet1[Index] = EARLIER(Sheet1[Index]) - 1))
Before you start the dax calculation, you will need to add index column first and here is the output:

Month Start Date through DAX formula

I have dates in data from
02 Aug 2018
03 Aug 2018
04 Aug 2018
.
.
.
.
30 Aug 2018..
Now i want start of the month date through Dax formula which is 01/08/2018. But in data date is 02/08/2018 which i dont want
i tried below formula
Start_Monthdate = STARTOFMONTH(EStart_Date[Date])
through above formula i get 02 Aug 2018 which i dont want
In DAX, what you can do is use the EOMONTH function.
https://dax.guide/eomonth/
Column Name = EOMONTH(table[date], -1) + 1
So the above DAX is finding the end of the previous month, then adding 1 day to it.
For the date 2/4/2020, EOMONTH gets the date 31/3/2020, then adds one day to get 1/4/2020
Time intelligence only works reliably if you use it on a calendar table that has all the dates in the year you're working with. Since your date column is missing the first day of the month, STARTOFMONTH returns the first one that you do have.
Without creating a proper calendar table, you either use EOMONTH as #Jonee mentioned or try this:
DATE ( YEAR ( EStart_Date[Date] ), MONTH ( EStart_Date[Date] ), 1 )

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")))

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.

Add month constraint to date_trunc by week

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))