Forum Discussion

gladies123's avatar
gladies123
Icon for Helper I rankHelper I
5 years ago
Solved

Reg: WTD

hi Team,

We need an solution for WTD   

Example

if i select any date ex:(18/08/2021) from slicer it should consider the start of the week that is (15/08/2021-sunday), so it should return (15/08/2021-18/08/2021).

kindly help me on this 

 

thanks,

Gladis

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi gladies123 

    I think you need to build a date table and add a custom weeknum column and a custom year column in it by dax. If you use weeknum function directly, you will get wrong weeknum at the end of the previous year and the beginning of the next year.

    For example: 

    In my Sample I let week start on Sunday.

    2020/12/31,2021/01/01,2021/01/02 are in the same weeknum, but weeknum function will give you 53 in 2020 and 1 in 2021. It's wrong result.

    Firstly build a calendar table by dax.

    Date = 
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2019, 01, 01 ), DATE ( 2021, 12, 31 ) ),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "WeekNum", WEEKNUM ( [Date], 1 ),
        "WeekDay", WEEKDAY ( [Date], 1 ),
        "MonthName", FORMAT ( [Date], "MMMM" ),
        "DayName", FORMAT ( [Date], "DDDD" )
    )

    Add calculated columns in it to get correct weeknum and year.

    ISO 8601 WeekNum = 
    VAR _COUNT0 =
        CALCULATE (
            COUNTROWS ( 'Date' ),
            FILTER (
                'Date',
                'Date'[Year] = EARLIER ( 'Date'[Year] )
                    && 'Date'[WeekNum] - 1 = 0
            )
        )
    VAR _BASENUM1 =
        IF ( _COUNT0 < 7, 'Date'[WeekNum] - 1, 'Date'[WeekNum] )
    VAR _ISO_8601_WeekNum =
        IF (
            WEEKDAY ( DATE ( 'Date'[Year] - 1, 01, 01 ) ) <> 1
                && 'Date'[Year] = 'Date'[Year]
                && _BASENUM1 = 0,
            WEEKNUM ( DATE ( MIN ( 'Date'[Year] ), 12, 31 ), 1 ) - 1,
            _BASENUM1
        )
    RETURN
        _ISO_8601_WeekNum
    ISO WeekYear = 
    VAR _COUNT0 =
        CALCULATE (
            COUNTROWS ( 'Date' ),
            FILTER (
                'Date',
                'Date'[Year] = EARLIER ( 'Date'[Year] )
                    && 'Date'[WeekNum] - 1 = 0
            )
        )
    VAR _BASENUM1 =
        IF ( _COUNT0 < 7, 'Date'[WeekNum] - 1, 'Date'[WeekNum] )
    RETURN
    IF(_BASENUM1 = 0,'Date'[Year] -1,'Date'[Year])

    Build a relationship between this table and your Data table.

    Then build a measure to calculate WTD.

    WTD =
    VAR _SELECTISOYEAR =
        SELECTEDVALUE ( 'Date'[ISO WeekYear] )
    VAR _SELECTISOWEEKNUM =
        SELECTEDVALUE ( 'Date'[ISO 8601 WeekNum] )
    RETURN
        CALCULATE (
            SUM ( Data[Value] ),
            FILTER (
                ALL ( 'Date' ),
                'Date'[ISO 8601 WeekNum] = _SELECTISOWEEKNUM
                    && 'Date'[ISO WeekYear] = _SELECTISOYEAR
            )
        )

    Result is sum between 2021/08/15 - 2021/08/21.

     

    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.

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi gladies123 

    I think you need to build a date table and add a custom weeknum column and a custom year column in it by dax. If you use weeknum function directly, you will get wrong weeknum at the end of the previous year and the beginning of the next year.

    For example: 

    In my Sample I let week start on Sunday.

    2020/12/31,2021/01/01,2021/01/02 are in the same weeknum, but weeknum function will give you 53 in 2020 and 1 in 2021. It's wrong result.

    Firstly build a calendar table by dax.

    Date = 
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2019, 01, 01 ), DATE ( 2021, 12, 31 ) ),
        "Year", YEAR ( [Date] ),
        "Month", MONTH ( [Date] ),
        "WeekNum", WEEKNUM ( [Date], 1 ),
        "WeekDay", WEEKDAY ( [Date], 1 ),
        "MonthName", FORMAT ( [Date], "MMMM" ),
        "DayName", FORMAT ( [Date], "DDDD" )
    )

    Add calculated columns in it to get correct weeknum and year.

    ISO 8601 WeekNum = 
    VAR _COUNT0 =
        CALCULATE (
            COUNTROWS ( 'Date' ),
            FILTER (
                'Date',
                'Date'[Year] = EARLIER ( 'Date'[Year] )
                    && 'Date'[WeekNum] - 1 = 0
            )
        )
    VAR _BASENUM1 =
        IF ( _COUNT0 < 7, 'Date'[WeekNum] - 1, 'Date'[WeekNum] )
    VAR _ISO_8601_WeekNum =
        IF (
            WEEKDAY ( DATE ( 'Date'[Year] - 1, 01, 01 ) ) <> 1
                && 'Date'[Year] = 'Date'[Year]
                && _BASENUM1 = 0,
            WEEKNUM ( DATE ( MIN ( 'Date'[Year] ), 12, 31 ), 1 ) - 1,
            _BASENUM1
        )
    RETURN
        _ISO_8601_WeekNum
    ISO WeekYear = 
    VAR _COUNT0 =
        CALCULATE (
            COUNTROWS ( 'Date' ),
            FILTER (
                'Date',
                'Date'[Year] = EARLIER ( 'Date'[Year] )
                    && 'Date'[WeekNum] - 1 = 0
            )
        )
    VAR _BASENUM1 =
        IF ( _COUNT0 < 7, 'Date'[WeekNum] - 1, 'Date'[WeekNum] )
    RETURN
    IF(_BASENUM1 = 0,'Date'[Year] -1,'Date'[Year])

    Build a relationship between this table and your Data table.

    Then build a measure to calculate WTD.

    WTD =
    VAR _SELECTISOYEAR =
        SELECTEDVALUE ( 'Date'[ISO WeekYear] )
    VAR _SELECTISOWEEKNUM =
        SELECTEDVALUE ( 'Date'[ISO 8601 WeekNum] )
    RETURN
        CALCULATE (
            SUM ( Data[Value] ),
            FILTER (
                ALL ( 'Date' ),
                'Date'[ISO 8601 WeekNum] = _SELECTISOWEEKNUM
                    && 'Date'[ISO WeekYear] = _SELECTISOYEAR
            )
        )

    Result is sum between 2021/08/15 - 2021/08/21.

     

    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.