Forum Discussion
How to calculate week differences between different data type
I have two columns
InvCom_Date = Which is a date type column
Workweek = Which has data like '23W34' which means year 2023 and week 34
now i want to calculate the difference in weeks.
Tried
for now i have tried splitting the 'workweek' column and extracting week no. and subtracting from the extracted weeknum of 'InvCom_Date'. But issue is handling the years if 'InvCom_Date' is in 2024 then the weekdifference is wrong. I tried a lot with measures I am not able to figure out how to handle year diffrence while calculating also the difference in data types i giving me issue in finding a solution
heres the sample data and expected output
- Anonymous2 years ago
Hi Karnik_ ,
I suggest you to create a Calendar table to help calculation.
Calendar = VAR _STEP1 = ADDCOLUMNS ( CALENDARAUTO (), "Year", YEAR ( [Date] ), "Month", MONTH ( [Date] ), "WeekStart", [Date] - WEEKDAY ( [Date], 2 ) + 1, "WeekYear", YEAR ( [Date] - WEEKDAY ( [Date], 2 ) + 1 ) ) VAR _STEP2 = ADDCOLUMNS ( _STEP1, "ActualWeek", RANKX ( FILTER ( _STEP1, [WeekYear] = EARLIER ( [WeekYear] ) ), [WeekStart], , ASC, DENSE ) ) VAR _STEP3 = ADDCOLUMNS(_STEP2,"Workweek",RIGHT([WeekYear],2)&"W"&FORMAT([ActualWeek],"00")) RETURN _STEP3Then create a calculated column.
Difference in No.of Weeks = VAR _START = CALCULATE ( MAX ( 'Calendar'[WeekStart] ), FILTER ( 'Calendar', 'Calendar'[Workweek] = EARLIER ( 'Table'[Workweek] ) ) ) VAR _END = CALCULATE ( MAX ( 'Calendar'[WeekStart] ), FILTER ( 'Calendar', 'Calendar'[Date] = EARLIER ( 'Table'[InvCom_Date] ) ) ) RETURN DATEDIFF ( _START, _END, WEEK )Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
3 Replies
- Karnik_Helper I
Hi please check the post I have uploaded
- AnonymousNot applicable
Hi Karnik_ ,
I suggest you to create a Calendar table to help calculation.
Calendar = VAR _STEP1 = ADDCOLUMNS ( CALENDARAUTO (), "Year", YEAR ( [Date] ), "Month", MONTH ( [Date] ), "WeekStart", [Date] - WEEKDAY ( [Date], 2 ) + 1, "WeekYear", YEAR ( [Date] - WEEKDAY ( [Date], 2 ) + 1 ) ) VAR _STEP2 = ADDCOLUMNS ( _STEP1, "ActualWeek", RANKX ( FILTER ( _STEP1, [WeekYear] = EARLIER ( [WeekYear] ) ), [WeekStart], , ASC, DENSE ) ) VAR _STEP3 = ADDCOLUMNS(_STEP2,"Workweek",RIGHT([WeekYear],2)&"W"&FORMAT([ActualWeek],"00")) RETURN _STEP3Then create a calculated column.
Difference in No.of Weeks = VAR _START = CALCULATE ( MAX ( 'Calendar'[WeekStart] ), FILTER ( 'Calendar', 'Calendar'[Workweek] = EARLIER ( 'Table'[Workweek] ) ) ) VAR _END = CALCULATE ( MAX ( 'Calendar'[WeekStart] ), FILTER ( 'Calendar', 'Calendar'[Date] = EARLIER ( 'Table'[InvCom_Date] ) ) ) RETURN DATEDIFF ( _START, _END, WEEK )Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.