Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

DateAdd and SamePeriodLastYear Returning Blank Values

Hi,   I'm new to PowerBI and I'm trying to create a measure which calculates the sales for the same time period as last year (and for last week so I'd rather use the dateadd function).   I'm pull...
  • v-zhenbw-msft's avatar
    6 years ago

    Hi Anonymous ,

     

    If you have a date table, your measure is correct, but there is a little detail, maybe you ignore, please refer the following steps,

     

    1. Create a one-to-many relationship between your table and date table.

     

     

    2. Then we create a measure like yours.

     

    Sales LY Test1 = CALCULATE(SUM('Table'[Sales]), SAMEPERIODLASTYEAR('Date'[Date]))

     

    3. At last we put the ‘Date’[date] to the visual, the result like this,

     

     

     

    Or if you don’t have a date table, we can create a new measure to meet your requirement.

     

    Sales LY Test 2 = 
    var _currentyear = MAX('Table'[Year])
    var _lastdate = DATE(_currentyear-1,MONTH(MAX('Table'[Time Period])),DAY(MAX('Table'[Time Period])))
    return
    CALCULATE(SUM('Table'[Sales]),FILTER(ALLSELECTED('Table'),'Table'[Time Period]=_lastdate))

     

     

    If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

    BTW, pbix as attached.

     

    Best regards,

     

    Community Support Team _ zhenbw

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