Forum Discussion

gopichilla's avatar
gopichilla
Resolver III
9 years ago
Solved

previous week sales.

Hi All,

 

I want previous week sales anyone can send me DAX query.

 

Thanks


  • gopichilla wrote:

    Hi All,

     

    I want previous week sales anyone can send me DAX query.

     

    Thanks


    gopichilla

    You'll need a calendar table as below.

    calendar =
    VAR TEMPTBL =
        ADDCOLUMNS (
            CALENDAR ( "2016-12-31", "2017-12-31" ),
            "Year", YEAR ( [Date] ),
            "weekNo", WEEKNUM ( [Date] ),
            "YearWeek", CONCATENATE (
                YEAR ( [Date] ),
                RIGHT ( CONCATENATE ( "0", WEEKNUM ( [Date] ) ), 2 )
            )
        )
    RETURN
        ADDCOLUMNS ( TEMPTBL, "WeekIndex", RANKX ( TEMPTBL, [YearWeek],, ASC, DENSE ) )

    Then create a one to many relationship from calendar table to data table. Then use a measure as below to get the previous week's value.

    previous week value =
    CALCULATE (
        SUM ( data[value] ),
        FILTER (
            ALLSELECTED ( 'calendar' ),
            MAX ( 'calendar'[WeekIndex] )
                = 'calendar'[WeekIndex] + 1
        )
    )

     

     

    See more details in the attached pbix file.

6 Replies

  • Eric_Zhang's avatar
    Eric_Zhang
    Icon for Microsoft Employee rankMicrosoft Employee

    gopichilla wrote:

    Hi All,

     

    I want previous week sales anyone can send me DAX query.

     

    Thanks


    gopichilla

    You'll need a calendar table as below.

    calendar =
    VAR TEMPTBL =
        ADDCOLUMNS (
            CALENDAR ( "2016-12-31", "2017-12-31" ),
            "Year", YEAR ( [Date] ),
            "weekNo", WEEKNUM ( [Date] ),
            "YearWeek", CONCATENATE (
                YEAR ( [Date] ),
                RIGHT ( CONCATENATE ( "0", WEEKNUM ( [Date] ) ), 2 )
            )
        )
    RETURN
        ADDCOLUMNS ( TEMPTBL, "WeekIndex", RANKX ( TEMPTBL, [YearWeek],, ASC, DENSE ) )

    Then create a one to many relationship from calendar table to data table. Then use a measure as below to get the previous week's value.

    previous week value =
    CALCULATE (
        SUM ( data[value] ),
        FILTER (
            ALLSELECTED ( 'calendar' ),
            MAX ( 'calendar'[WeekIndex] )
                = 'calendar'[WeekIndex] + 1
        )
    )

     

     

    See more details in the attached pbix file.