Forum Discussion

michaelstone's avatar
michaelstone
Icon for Helper I rankHelper I
7 years ago
Solved

Strange behaviour with SAMEPERIODLASTYEAR

Hi,

 

I'm building a Power BI report which is analysing PoS transaction data so have a fact table (Transactions) with every individual transaction including timestamp and sales amount (Transactions[Sales]).

 

I've added a custom column for the transaction date (Transactions[Date]) which is joined to a calendar table (Calendar) which is generated from MIN(Transactions[Date]) to MAX(Transactions[Date]).

 

I've created measures for

 

Total Sales = SUM(Transactions[Sales]
Total Sales Last Year = CALCULATE([Total Sales],SAMEPERIODLASTYEAR('Calendar'[Date]))

 

The problem occurs when I include the last date in the Calendar table when calculating Total Sales Last Year.

 

As you can see below, if I select 13 - 14 July 2019 the calculations are correct i.e. the Total Sales Last Year are for the corresponding 2 days in 2018.

 

Note the the Date slider is Calendar[Date] and the Date column in the table is Transactions[Date].

 

 

 

 

 

 

 

 

 

 

 

If I change this to be 14 - 15 July 2019 where 15 July is the last date for any transactions, the Total Sales Last Year is incorrectly calculated for the period 14 - 30 July.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I have found a workaround which involves adding some additional days to the end of my Calendar table but the problem will occur again if these future days are selected in my date filter.

 

Am I using this function incorrectly or is there a bug?

 

Thanks,

 

Michael

  • v-piga-msft's avatar
    v-piga-msft
    7 years ago

    Hi michaelstone ,

    Sorry for my mistake.

    Please modify the measure Total Sales - Previous Year  like below.

    Total Sales - Previous Year =
    SUMX (
        'Calendar',
        CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Calendar'[Date] ) )
    )
    

    Here is the output.

    Best  Regards,

    Cherry

     

     

8 Replies

    • michaelstone's avatar
      michaelstone
      Icon for Helper I rankHelper I

      Hi v-piga-msft ,

       

      Here is a link to a file which shows the issue.

       

      PBIX File

       

      Note that the end date is now 17 July so you need to have this as the end date to see the issue.

       

      Thanks,

       

      Michael

      • v-piga-msft's avatar
        v-piga-msft
        Icon for Resident Rockstar rankResident Rockstar

        Hi michaelstone ,

        By my test and research based on your pbix, I'm afraid that you should create the table with the date column from Calendar table like below so that you could get the correct output.

        Best  Regards,

        Cherry