Forum Discussion

Masaoka's avatar
Masaoka
New Member
5 years ago
Solved

SAMEPERIODLASTYEAR returns wrong sum when used with "today"

example:

 

- in a run-of-the-mill "orders" table I'm summing "orderedQuantity" over a period of "orderDate".

- orderDate is filtered using a relative filter of "Last 1 month"

- so far so good.

- i also made a measure called "quantity - last year" using sameperiodlastyear (* see below)

- when i sum "quantity - last year", i get bogus (=visibly inflated) numbers.

 

took me some time to figure out that the problem disappears when i exclude today from the date filter. (or when i filter the day using Between and choose the next-to-last day as the end date.)

 

interestingly, when graphed in a chart, the last year totals are always correct - each day's total is displayed correctly (see screen below). the problem is only manifested in a sum, such as on a Card.

 

any idea what i'm doing wrong? or if it is a bug?

 

Further details:

- the exact formula i use is as follows: quantity-LY = CALCULATE(SUM('table'[quantity]), SAMEPERIODLASTYEAR('table'[orderDate])) )

- the table i use is a view in MSSQL, imported data (not live connect)

- here's a screenshot where the problem is apparent: http://prntscr.com/voir5z

  • Masaoka , Time intelligence function need continuous dates, so you use date from the date any date missed will lead to the wrong result or error

4 Replies

  • Masaoka , Try with date table

    Year behind Sales = CALCULATE(SUM('table'[quantity]),dateadd('Date'[Date],-1,Year))
    quantity-LY = CALCULATE(SUM('table'[quantity]), SAMEPERIODLASTYEAR('Date'[Date]))

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

  • i see. you're right, that should work.

     

    is this a bug? or is using the table's native date hierarchy not recommended for time intel funcions?

     

    thanks!

     

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

      Masaoka , Time intelligence function need continuous dates, so you use date from the date any date missed will lead to the wrong result or error

  • makes sense.

    i still think the app should at least indicate this problem.

     

    thanks!