Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago

Same Week last Year Flag

Hey all,

 

Apologies in advance as I'm fairly new to more complex DAX functions.

 

I'm attempting to create a column that flags whether or not a product was being sold in the same week last year and if so, flag 'Y' and if not, flag 'N'. I have a date table as well as ISO weeks and years to work with along with the product name, just unsure on how to put this all together to make sure it's flagging by week specifically.

 

Thanks in advance!

16 Replies

  • truptis's avatar
    truptis
    Community Champion

    Hi Anonymous ,
    try using SAMEPERIODLASTYEAR & WEEKNUM()
    Logic steps:
    1- create a column that would return your current year's week number (using WEEKNUM())
    eg-WeekNum =WEEKNUM([Date])
    2- Create a column called WeekYear = [WeekNum] * 10000 + [Year]
    eg- 12022, 22022,32022... for 1st week of 2022, 2nd week of 2022, 3rd week of 2022 and so on respectively.
    3- Create another column Week_Last_Year = [WeekYear] -1
    eg- (if WeekYear is 22022 then Week_Last_year will be 22021)
    4- Create a measure called TOTALSALES=sum(totalsales)
    5- Create a measure for current week
    Current_week = WEEKNUM(TODAY()) * 10000 + YEAR(TODAY())
    6-Create measure for current week's sales

    Current_Week_Sales= Calculate ([TotalSales],Filter(ALL(Calendartable), [WeekYear] = [Current_week ] ))

    7. Create a measure called LastYear_SameWeekSales = Calculate ([TotalSales],Filter(ALL(Calendartable), [WeekLastYear] = [Current_Week] - 1))

    Using this, you can compare your Sales at the same time this year and last year.
    Please mark it as a solution if this helps you. Thanks.

    Regards,
    TruptiS

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello and thanks for your reply!

       

      To clarify, I am not looking for total sales. I am looking to see which products (by name) were being sold in the same week last year.

       

      For example, if I know I sold Product A, B, and C this year this week, but I only sold Products A and B last year on the same week, I'd want to flag that product C was not being sold last year same week. 

  • truptis's avatar
    truptis
    Community Champion

    Anonymous -> Try using the below:

    Logic steps:
    1- create a column that would return your current year's week number (using WEEKNUM())
    eg-WeekNum =WEEKNUM([Date])
    2- Create a column called WeekYear = [WeekNum] * 10000 + [Year]
    eg- 12022, 22022,32022... for 1st week of 2022, 2nd week of 2022, 3rd week of 2022 and so on respectively.
    3- Create another column Week_Last_Year = [WeekYear] -1
    eg- (if WeekYear is 22022 then Week_Last_year will be 22021)
    4- Create a measure called TOTALSALES=sum(totalsales)
    5- Create a measure for current week
    Current_week = WEEKNUM(TODAY()) * 10000 + YEAR(TODAY())
    6-Create measure for current week's sales

    Current_Week_Sales= Calculate ([TotalSales],Filter(ALL(Calendartable), [WeekYear] = [Current_week ] ))

    7. Create a measure called LastYear_SameWeekSales = Calculate ([TotalSales],Filter(ALL(Calendartable), [WeekLastYear] = [Current_Week] - 1))

    Using this, you can compare your Sales at the same time this year and last year.
    Please mark it as a solution if this helps you. Thanks.

    Regards,
    TruptiS

  • Anonymous's avatar
    Anonymous
    Not applicable

    I'm still having an issue with this one. I've tried a couple of the solutions offered here but nothing seems to be giving me the desired outcome. As a sample table below:

     

    In this instance, I would be trying to flag down 'Brand C' because it was sold in the same week as last year. 

     

    BrandDates
    Brand A1/1/2021
    Brand B1/2/2021
    Brand C1/3/2021
    Brand D2/1/2022
    Brand C1/3/2020
    Brand B1/1/2021
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Please create two calculated columns.

    week_and_year = YEAR('Table'[Dates])+WEEKNUM('Table'[Dates],2)*10000
    Column = 
    VAR _value_1 = 'Table'[Brand]
    VAR _value_2 =
        CALCULATE (
            MAX ( 'Table'[week_and_year] ),
            FILTER ( 'Table', 'Table'[Brand] = _value_1 )
        )
    VAR _value_3 =
        CALCULATE (
            MIN ( 'Table'[week_and_year] ),
            FILTER ( 'Table', 'Table'[Brand] = _value_1 )
        )
    RETURN
        SWITCH (
            TRUE (),
            'Table'[Brand] = _value_1
                && _value_2 - _value_3 = 1, "Y",
            "N"
        )

    The result should be like this.

    Attach the PBIX file for reference. Hope it helps.

     

    If this doesn't work for you or I misunderstand your needs, please consider sharing more details about it. And it would be great if there was a sample file without any sensitive information here.

     

    It makes it easier to give you a solution.

    1. Sample (dummy dataset) data as text, use the table tool in the editing bar.
    2. Expected output from sample data.
    3. Explanation in words of how to get from 1. to 2.

     

    Best Regards,
    Community Support Team_Gao

     

    If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly -- How to provide sample data

    • Anonymous's avatar
      Anonymous
      Not applicable

      First, thanks so much for your response! 

       

      I attempted to implement this solution, but received "N" for every single result, so it doesn't appear to be working. I'd love to include a more thorough file that includes an anonymized version of the data I'm working with, but I don't see a way to attach files here. Is there some way I can send it to you?

       

      Thanks!