Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Karnik_
Helper I
Helper I

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

Karnik__0-1694034638696.png

heres the sample data and expected output



1 ACCEPTED SOLUTION
Anonymous
Not 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
    _STEP3

Then 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.

vrzhoumsft_0-1694670988636.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
parry2k
Super User
Super User

@Karnik_ not fully sure what you are trying to achieve, paste sample data in the table format with the expected output.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi please check the post I have uploaded

 

Anonymous
Not 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
    _STEP3

Then 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.

vrzhoumsft_0-1694670988636.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.