Forum Discussion

amaleranda's avatar
amaleranda
Icon for Post Patron rankPost Patron
6 years ago

Calculate previousday total

Hi DAX gurus, 

 

I use to below fromula to calculate previous day total(Measure).

 

It does not seem to like me :(.

 

Any help would be really appriciated.

 

PreviousDayTotal :=
VAR DayPrevous = DATEADD(Targets[Date], -1, DAY)
VAR Total = SUM(Targets[Total])
VAR TotalPerviousDay=
CALCULATE(
[Total],
DayPrevous
 
)

RETURN
TotalPerviousDay

 

 

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    maybe instead of "formula does not seem to like me" show expected results and outcome, it would be easier to help you.

    Anyway i'm not 100% sure you can create a measure inside a var and recalculate it again in the result.

     

    Try to create a measure
    TotalSum= SUM(Targets[Total])

    Then a new one

    PreviousDayTotal =
    VAR DayPrevous = DATEADD(Targets[Date], -1, DAY)
    VAR TotalPerviousDay=
    CALCULATE(
    [TotalSum],
    DayPrevous
    )
    RETURN
    TotalPerviousDay
     
     
     
    • amaleranda's avatar
      amaleranda
      Icon for Post Patron rankPost Patron

      Hi Anonymous ,

       

      Lesson learned. :) thanks for the reply. 

       

      I have done it that way but still not getting the result I want

       

       

      DateTotalPreviousdayTotal
      25-09-195000 
      26-09-1960005000
      27-09-1906000
      28-09-1990000
      29-09-1925009000
      30-09-1902500

       

      Previous day total is the measure I am trying to calculate.

      Thanks for your time

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Icon for Super User rankSuper User

        Hi,

        Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of your Calendar Table.  To your visual, drag the Date field from the Calendar Table.  Write these measures

        Total = SUM(Data[Value])

        PreviousDayTotal = CALCULATE([Total],PREVIOUSDAY(Calendar[Date]))

        Hope this helps.