Forum Discussion
jeronimo2334
Helper III
6 years agoCalculate date table from timestamps
Hello, I was hoping I could get some input on how to achieve this through DAX or SQL : I have a table with 3 columns: ids , lengths & timestamps. What I am trying to do is create a new table tha...
- 6 years ago
Yes, it is. 🙂 Seriously, @jeronimo2334, this was a real challenge.
So I thought about that. I think you just have to make a very small edit:
Table 2 = VAR __Table = SUMMARIZE( 'Table', 'Table'[id], 'Table'[weekLength], 'Table'[firstDate], "__DateNum1",INT([firstDate]) ) VAR __Calendar = ADDCOLUMNS( CALENDAR( DATE(2019,1,1), DATE(2021,12,31) ), "__Weeknum",WEEKNUM([Date],17), "__DateNum2",INT([Date]), "__Year",YEAR([Date]) ) VAR __GeneratedTable = FILTER( GENERATE(__Table,__Calendar), [__DateNum2]>=[__DateNum1] ) VAR __GeneratedTable2 = ADDCOLUMNS( __GeneratedTable, "__Sequential", VAR MaxWeeks = SUMMARIZE(FILTER(__GeneratedTable,[id]=EARLIER([id])),[__Year],"MaxWeek",MAXX(FILTER(__GeneratedTable,[id]=EARLIER([id])),[__Weeknum])) VAR MyYear = [__Year] VAR MyStart = SUMX(FILTER(MaxWeeks,[__Year]<MyYear),[MaxWeek]) VAR firstYear = MINX(FILTER(__GeneratedTable,[id]=EARLIER([id])),[__Year]) VAR myNum = IF(MyYear=firstYear,[__Weeknum],MyStart+[__Weeknum]) RETURN myNum ) VAR __GeneratedTable3 = ADDCOLUMNS( __GeneratedTable2, "__WeeksFromMin", [__Sequential] - MINX(FILTER(__GeneratedTable2,[id]=EARLIER([id])),[__Sequential]) + 1 ) RETURN SELECTCOLUMNS( FILTER( __GeneratedTable3, [__WeeksFromMin] <= [weekLength] ), "id",[id], "date",[Date], "week",[__WeeksFromMin] )
amitchandak
Super User
6 years agojeronimo2334 , This should be in second or millisecond. You can create new columns like
Date = dateadd(date(1900,1,1),[firstdate]/(24*60*60),DAY)
WeekNum = weeknum ([Date])
Weekday = weekday([date])
- jeronimo23346 years ago
Helper III
amitchandakYou misunderstood, I don't want new columns. I need a new table that takes each timestamp from the original table and auto increments in a date format depending on the number of length * 7.
eg
id date week 1 01/23/2020 1 1 01/24/2020 1 1 01/25/2020 1 1 01/26/2020 1 1 01/27/2020 1 1 01/28/2020 1 1 01/29/2020 1 1 01/30/2020 2 1 01/31/2020 2 1 02/01/2020 2 1 02/02/2020 2 1 02/03/2020 2 2 04/12/2020 1 2 04/13/2020 1 2 04/14/2020 1 2 04/15/2020 1 2 04/16/2020 1 2 04/17/2020 1 2 04/18/2020 1 2 04/19/2020 2 - edhans6 years ago
Community Champion
Try this:
- In Power Query, right-click on your original table and select "Reference"
- In that table, select the ID and timestamp columns then REMOVE OTHER COLUMNS from the Home ribbon menu.
- Do whatever you do to convert that timestamp to a real date.
- Use the Date.AddDay([Date],-7) feature to roll it back 7 days in a new column.
- You can then click on that new column, select the "Add Columns" menu, then Date, Week, Week Number to add a week number column. Week of year or month, whatever you need.