Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Last 8 Weeks Sales spanning previous year

Dear all,

 

I am struggling with a report that gives total of last 8 weeks sales based on selected Year and Week. For example, if we select week 2 of 2020, last 8 weeks sales will extend back to 2019. I have gone through many posts but it is still not addressing my issue. I have a column for week in my calendar. My week number ends in 52 of every of every year except leap year. 

Kindly assist.

  • Anonymous 

    EDIT: I've added comments below showing how to follow this method but without creating a YearWeek Table

    One way of doing this is to create a YearWeek Table, linked to you calendar table through a common YearWeek field (unique values in the YearWeek Table), including and index column (I've ranked it from most recent backwards, so today's month is ranked 1, last month 2....(You then use this index column in your calculations)

     

    and the model looks like this:

     

    You can then use the fields from this YearWeek table in measures, slicers, visuals filters...

    To calculate the last 8 weeks based on the selections in the slicers:

     

     

    Prev. 8 week Forecast = CALCULATE([Sum Forecast];
                            FILTER(ALL('Year Week Table');
                            'Year Week Table'[YW Index]< SELECTEDVALUE('Year Week Table'[YW Index])+8
                            &&
                            'Year Week Table'[YW Index] >= SELECTEDVALUE('Year Week Table'[YW Index])))

     

     

    So basically you are using the index to establish the 8 week limits (8 weeks previous would be YW Index + 7)

     

    And this method gets you this:

     

    or 

     

    Hope this helps.

     

    EDIT: you can actually follow this method without having to create a new table. All you need is the the YearWeek number and YearWeek index. You can include these with  calculated columns in your calendar table using:

    YearWeek = Calendar [Year] * 100 + Calendar [Month]
    YearWeek Index = RANKX('Calendar'; 'Calendar'[YearWeek];;DESC;Dense)

     

13 Replies

  • PaulDBrown's avatar
    PaulDBrown
    Icon for Community Champion rankCommunity Champion

    Anonymous 

    EDIT: I've added comments below showing how to follow this method but without creating a YearWeek Table

    One way of doing this is to create a YearWeek Table, linked to you calendar table through a common YearWeek field (unique values in the YearWeek Table), including and index column (I've ranked it from most recent backwards, so today's month is ranked 1, last month 2....(You then use this index column in your calculations)

     

    and the model looks like this:

     

    You can then use the fields from this YearWeek table in measures, slicers, visuals filters...

    To calculate the last 8 weeks based on the selections in the slicers:

     

     

    Prev. 8 week Forecast = CALCULATE([Sum Forecast];
                            FILTER(ALL('Year Week Table');
                            'Year Week Table'[YW Index]< SELECTEDVALUE('Year Week Table'[YW Index])+8
                            &&
                            'Year Week Table'[YW Index] >= SELECTEDVALUE('Year Week Table'[YW Index])))

     

     

    So basically you are using the index to establish the 8 week limits (8 weeks previous would be YW Index + 7)

     

    And this method gets you this:

     

    or 

     

    Hope this helps.

     

    EDIT: you can actually follow this method without having to create a new table. All you need is the the YearWeek number and YearWeek index. You can include these with  calculated columns in your calendar table using:

    YearWeek = Calendar [Year] * 100 + Calendar [Month]
    YearWeek Index = RANKX('Calendar'; 'Calendar'[YearWeek];;DESC;Dense)

     

    • foyadeyi's avatar
      foyadeyi
      Frequent Visitor

      PaulDBrown How I do modify the above to cater for a Calender Week range filter instead of just selecting a Week? And I also notice the measure above includes the current week selected as part of the preceding weeks

       

      • PaulDBrown's avatar
        PaulDBrown
        Icon for Community Champion rankCommunity Champion

        By "Calendar week range" do you mean you are selecting a range of weeks? If so, what is the number of preceding weeks you wish to show and which week is the reference to select the preceding weeks?

         

        Can you show a mockuo of what you are trying to acieve?

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak Thank you for sharing however, my report is dynamic meaning it works by the selection on a slicer and that is where my challenge is. The week on the slicer is from 1 - 52 which can also rep any year. I am looking a formula that will work based on any selection made.

      • amitchandak's avatar
        amitchandak
        Icon for Super User rankSuper User

        As long as all the date related information is in Date table. The formula should respond to the date on top or date related stuff on top.

  • V-lianl-msft's avatar
    V-lianl-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    You need to combine "year" and "week" into a new column, and then use rankx to sort the column.

    Please try DAX like this:

    sum_Sales_last_8_weeks =
    CALCULATE (
        SUM ( Sales[Sales] ),
        FILTER (
            Sales,
            Sales[Date] <= MAX ( DimDate[Date] )
                && Sales[Date]
                    > MAXX (
                        FILTER ( DimDate, DimDate[rank] = SELECTEDVALUE ( DimDate[rank] ) - 8 ),
                        DimDate[Date]
                    )
        )

    For more details,please refer to the pbix.

     

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi V-lianl-msft ,

       

      Thank you very much, this should work however the SELECTEDVALUE in the DAX is referencing the ranked week and not country week which is 1-52.

      This is where I really need help, I want user to see only country week 1-52 of any year in the report slicer and be able to get for example past 8 weeks sales even if spilled to previous year.

       

      How can DAX be written such that ranked week can reference the selected country week or any other way to achieve this?

       

      Regards,

      Kunle