Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

last week

Hello everybody, i faced a problem for creating a new metrics that displays the compations between this year and the previous year but on the same day of the week and the same week of the year. For example i should compare 'Tuesday, week-33, 2023' with 'Tuesday, week-33, 2022'. How i can create using dax 'Tuesday, week33, 2022'? 

Thank you for everything. 

  • Anonymous's avatar
    Anonymous
    3 years ago

    I found the solution:

     

    Sales LastYear =
    IF(
        MOD(YEAR(MAX('Calendar table'[Date]))-1,4) > 0,  -- to check if the previous year is a leap year
        CALCULATE (
            [Tot_sales],
            FILTER (
                ALL('Calendar table'),
                'Calendar table'[Date] <= MAX('Calendar table'[Date]) - 364 && -- if not a leap year subtract 364
                'Calendar table'[Date] >= MIN('Calendar table'[Date]) - 364
            )
        ),
        CALCULATE (
            [Tot_sales],
            FILTER (
                ALL ('Calendar table'),
                'Calendar table'[Date] <= MAX('Calendar table'[Date]) - 363 && -- if is a leap year subtract 363
                'Calendar table'[Date] >= MIN('Calendar table'[Date]) - 363
            )
        )
    )

4 Replies

  • Syk's avatar
    Syk
    Resident Rockstar

    You can use the dateadd function. A quick example that will get your sales from last year...

    Last year Sales = CALCULATE(SUM('Table'[Sales]),DATEADD('Date Table'[Date],-1,year))
  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you very much for you time, but i faced another kind of problem. I should compare the sales of "tuesday 21th june 2022" with the sale of "tuesday 20th june 2023" so the same day of the week (tuesady) and the same week of the year (25th week of the year over 52). My problem is: how can i obtain "tuesday 21th june 2022"?

    • Syk's avatar
      Syk
      Resident Rockstar

      You can reference your previous year measure you just created and do the same thing with dateadd().

      Last year Sales + one day = CALCULATE([Last year Sales],DATEADD('Date Table'[Date],1,day))
  • Anonymous's avatar
    Anonymous
    Not applicable

    I found the solution:

     

    Sales LastYear =
    IF(
        MOD(YEAR(MAX('Calendar table'[Date]))-1,4) > 0,  -- to check if the previous year is a leap year
        CALCULATE (
            [Tot_sales],
            FILTER (
                ALL('Calendar table'),
                'Calendar table'[Date] <= MAX('Calendar table'[Date]) - 364 && -- if not a leap year subtract 364
                'Calendar table'[Date] >= MIN('Calendar table'[Date]) - 364
            )
        ),
        CALCULATE (
            [Tot_sales],
            FILTER (
                ALL ('Calendar table'),
                'Calendar table'[Date] <= MAX('Calendar table'[Date]) - 363 && -- if is a leap year subtract 363
                'Calendar table'[Date] >= MIN('Calendar table'[Date]) - 363
            )
        )
    )