Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

compare current month vs Previous month

Hi guys, 

     i am new to power bi and i want to compare current month sales with last month.  i am having data from 2017 january to 2019 november. i used a dax function for calculating last month

Last_month= CALCULATE(SUM(Table1[TotalAmount]),FILTER(ALL(Calender_table),Calender_table[Month]=MAX(Calender_table[Month])

 

When i use this formula i cannot filter it year wise say Eg, i am having an year filter and when i click 2019 i shows the sum all three years for the respective months. It is not showing the month values for 2019. please help me with it and provide a solution

 

 

 

 

4 Replies

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

    The easiest way to do this is to create a numeric index for your combination of year and month:

    DateY/MIndex
    2019-01-01jan-191
    2019-02-02feb-192
    2019-02-03feb-192
    2019-03-01mar-193
    2019-04-04apr-194


    Then reference the previous index in the calculation. 

    Assuming that the current date is 2019-04, the following will return the index "4":

     

    Previous month = 
    Calulate(
    SELECTEDVALUE( Calendar[Index] );
    Calendar[Date] = TODAY()
    )

     


    Then you can simply use that to calculate the previous index:

     

    Last_month = 
    CALCULATE(
    SUM(Table1[TotalAmount]);
    Calendar[Index] = [Previous month] -1
    )

     



    • Anonymous's avatar
      Anonymous
      Not applicable

      tex628  Hi,

                          But i am connecting it live so i cannot use selected value in it

       

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

        Selectedvalue works in directquery. Or what do you mean by live?