Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Previous Year Measure completely wrong

Hi All - I am pulling my hair out with overcoming this basic requirement that other BI tools have handled so well.

 

The requirement is that users can interact with an embedded date slicer and control which date range to display a measure for. In this case it would be 'Sales'. There is also a requirement to shows last year sales for the same date range -1 year.

 

So I set it up my measure being 

Booked Sales = SUM(vw_fact_booking[net_total_amt])

and my date slicer works fine:

measure with date slicer works

So now to do 'Year ago' - easy enough:
Booked Sales YAGO = CALCULATE(SUM(vw_fact_booking[net_total_amt]),PREVIOUSYEAR(vw_fact_booking[booking_date]))


Nope! You can see that the sales for previous year are more like this:

 

 

What am I doing wrong?

 

 

 

 

 

  • Phil_Seamark's avatar
    Phil_Seamark
    8 years ago

    Figured it out!

     

    Took me a while to download the 800MB model over my phone :o

     

    The correct DAX formula should be

     

    Booked Sales YAGO = CALCULATE(SUM(vw_fact_booking[net_total_amt]),SAMEPERIODLASTYEAR(dates[date]))

     

    The PREVIOUSYEAR calc was taking the whole previous year into account even if you were only selecting a month from the current year.  Useful for somethings, but not for you.

  • Anonymous's avatar
    Anonymous
    8 years ago

    Thank you Phil_Seamark

    Something so simple turning my hairs grey.

    Appreciate your help mate,

    Jake

10 Replies

  • Phil_Seamark's avatar
    Phil_Seamark
    Microsoft Employee

    Hi Anonymous

     

    Just wondering if you have gaps in dates in your data.

     

    I strongly recommend you use the pattern of adding a date/calender table to your model.  A quickfire way to do this is using the following DAX calculate table statement.

     

    dates = CALENDARAUTO()

     

    When you have a date table, create a relationship between the [date] column and your vw_fact_booking[booking_date] column.

     

    Change your measure to use the date column in the Date table and use the column from the date table in your slicer.

     

    Does that make sense?

     

     

     

     

    Create a relationship

    • Anonymous's avatar
      Anonymous
      Not applicable

      HI Phil_Seamark, what if we have multiple dates that we want to analyse ?

      I.E. we have booking_date and arrival_date and there is a need to look at both in same reports. However, I noticed that PowerBI only allows one date relationship at a time?

       

      I tried your suggestion anyway, and it's still broke.

       

       

      Booked Sales YAGO = CALCULATE(SUM(vw_fact_booking[net_total_amt]),PREVIOUSYEAR(dates[Date]))

       

       

       

       

       

      • Phil_Seamark's avatar
        Phil_Seamark
        Microsoft Employee

        Hi Anonymous

         

        You can have multiple relationships between your sales and date table.  Only one can be active.  Active just means default, in that you can write minimal DAX code that assumes the rules of the active relationship.  You can still analyise using the other dates, only you need to write your calculations to specify which of the relationships you'd like you use.