Forum Discussion

mes0012's avatar
mes0012
Frequent Visitor
2 years ago
Solved

Calculating previous months sales using monthly data

Hi All,

 

I'm trying to build a formula into dax that calculates the previous months sales depending on which month is selected. For example in the screenshot below i want 'Sales (£) PM' to pull through July sales when August is selected in the Month filter.

 

The data is at month level therefore a lot of the solutions posted on this forum aren't giving me the desired result, I have attached the calendar table below:

 

 

Any help would be massively appreicated!

 

Thanks,

Matt.

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi mes0012 ,

     

    I suggest you to create a DimDate table with continuous date in it and then try PREVIOUSMONTH.

     

    If this reply still couldn't help you solve your issue, please share a sample file with us and show us a screenshot with the result you want.

     

    Best Regards,
    Rico Zhou

     

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

     

     

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi mes0012 ,

     

    I suggest you to create a DimDate table with continuous date in it and then try PREVIOUSMONTH.

     

    If this reply still couldn't help you solve your issue, please share a sample file with us and show us a screenshot with the result you want.

     

    Best Regards,
    Rico Zhou

     

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

     

     

  • ValtteriN's avatar
    ValtteriN
    Icon for Community Champion rankCommunity Champion

    Hi,

    Here is one way to do this:
    Data:

    Relation:

    Dax:

    LM = var _cmonth = MAX('Table (7)'[YearMonth])
    return
    CALCULATE(max('Table (7)'[Value]),ALL('Table (7)'[YearMonth]),'Table (7)'[YearMonth]<_cmonth)

    End result:

     

    Here we remove YearMonth filter context and take the maximum value which is lower than current mont -> last month as context.


    I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

    My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/
    • mes0012's avatar
      mes0012
      Frequent Visitor

      ValtteriN, thanks for coming back on this.

      I've given the above suggestion a go but i can't get it to work. I created a new table, summarised in the same way as in your suggestion above (Total Debit Balance is the original field name before i've renamed it in the visual):

      However, when i build the measure i'm still getting blanks:

       
      Sales (£) PM = var _cmonth = MAX('AP - DR PM Table'[Y/MN Key])
      return
      CALCULATE(max('AP - DR PM Table'[Sales]),ALL('AP - DR PM Table'[Y/MN Key]),'AP - DR PM Table'[Y/MN Key]<_cmonth)
       

      Any chance you can see where i've gone wrong from those screenshots?

       

      Thanks,

      Matt.

      • ValtteriN's avatar
        ValtteriN
        Icon for Community Champion rankCommunity Champion

        Hi mes0012 ,

        Now that I revisited this I realized that there isn't anything preventing you from using previousmonth like Anonymous  suggested. 

        Here is a bit better dax (if you don't want to use PREVIOUSMONTH for one reason or another):

        LM =

        var _cmonth = MAX('Calendar'[YearMonth])
         var _lmonth = CALCULATE(MAX('Table (7)'[YearMonth]),ALL('Table (7)'),'Table (7)'[YearMonth]<_cmonth)
         return

        CALCULATE(MAX('Table (7)'[Value]),ALL('Table (7)'),'Table (7)'[YearMonth]= _lmonth)

        And here is one with PREVIOUSMONTH:

         
        LM2 = CALCULATE(MAX('Table (7)'[Value]),PREVIOUSMONTH('Calendar'[Date]))

        End results: