Forum Discussion

Uzi2019's avatar
Uzi2019
Community Champion
5 years ago
Solved

Incorrect Previous month value calculation

Hi Community,

 

I am trying to create previous month values. Everything is working as expected only issue is last date in Database is 5th April 2021 but when I select Date range slicer from 1st to 4th April so it gives me correct prevoius month value. BUT when I select date range from 1st April to 5th April so it gives me whole March value in previous measure.

 

I want it to give me 1st to 5th March value in my previous measure when I select date range from 1st to 5th April.

 

I don't know why it's behaving like this.

 

Previous Sales = CALCULATE(SUM(DataTable[Sales]),DATEADD('Calendar'[Date],-1,MONTH))
 
Can anyone please help me in this?
 
  • Hi Syndicate_Admin ,

    I create the following sample data, and then create a measure.

     

    Ventas anteriores = 
    var MonthEnd = 
        CALCULATE(
            SUM('DataTable'[Sales]),
            DATEADD( 'Calendar'[Date], -1, MONTH )
        )
    var Month = 
        CALCULATE(
            SUM('DataTable'[Sales]),
            FILTER(
                ALL('DataTable'),
                'DataTable'[Date] <= EDATE( MAX('Calendar'[Date]), -1 )
                &&'DataTable'[Date] >= EDATE( MIN('Calendar'[Date]), -1 )
            )
        )
    var Result = 
        IF(
            MAX('Calendar'[Date]) = EOMONTH( MAX('Calendar'[Date]), 0 ), 
            MonthEnd, 
            Month
        )
    return Result
    

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

    Best Regards,
    Winniz

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

5 Replies

  • Hi, Uzi2019 

    If it is OK with you, please share your sample pbix file, then I can try to look into it and come up with a fixed result. 

     

    Thank you.

     

    Hi, My name is Jihwan Kim.

    If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.

    • Uzi2019's avatar
      Uzi2019
      Community Champion

      Thank you for reply. But cant share you pbix file for security reasons.

  • Syndicate_Admin , You can try like example

     

    MTD QTY forced=
    var _max = today()
    return
    if(max('Date'[Date])<=_max, calculate(Sum('order'[Qty]),DATESMTD('Date'[Date])), blank())
    //or
    //calculate(Sum('order'[Qty]),DATESMTD('Date'[Date]),filter('Date','Date'[Date]<=_max))
    //calculate(TOTALMTD(Sum('order'[Qty]),'Date'[Date]),filter('Date','Date'[Date]<=_max))


    LMTD QTY forced=
    var _max = date(year(today()),month(today())-1,day(today()))
    return
    if(max('Date'[Date])<=_max, CALCULATE(Sum('order'[Qty]),DATESMTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max), blank())
    //OR
    //CALCULATE(Sum('order'[Qty]),DATESMTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
    //TOTALMTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)

     

    LYMTD QTY forced=
    var _max = date(year(today())-1,month(today()),day(today()))
    return
    if(max('Date'[Date])<=_max, CALCULATE(Sum('order'[Qty]),DATESMTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max), blank())
    //OR
    //CALCULATE(Sum('order'[Qty]),DATESMTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
    //TOTALMTD(Sum('order'[Qty]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)

     

    Refer

     

    Power BI — Month on Month with or Without Time Intelligence
    https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
    https://www.youtube.com/watch?v=6LUBbvcxtKA

  • v-kkf-msft's avatar
    v-kkf-msft
    Community Support

    Hi Syndicate_Admin ,

    I create the following sample data, and then create a measure.

     

    Ventas anteriores = 
    var MonthEnd = 
        CALCULATE(
            SUM('DataTable'[Sales]),
            DATEADD( 'Calendar'[Date], -1, MONTH )
        )
    var Month = 
        CALCULATE(
            SUM('DataTable'[Sales]),
            FILTER(
                ALL('DataTable'),
                'DataTable'[Date] <= EDATE( MAX('Calendar'[Date]), -1 )
                &&'DataTable'[Date] >= EDATE( MIN('Calendar'[Date]), -1 )
            )
        )
    var Result = 
        IF(
            MAX('Calendar'[Date]) = EOMONTH( MAX('Calendar'[Date]), 0 ), 
            MonthEnd, 
            Month
        )
    return Result
    

     

    If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

    Best Regards,
    Winniz

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