# DAX Measure to find date of previous record for each piece of data in a time series - HELP



## stuartbisset (Jan 12, 2018)

Hi folks
I am trying to write a DAX measure that will return the date of the previous piece of data in a time series.  See the example below (UK date format dd/mm/yy):

FACT TABLE
Date          Value    PreviousDate
01/03/17    100       
03/04/17    200      01/03/17
09/06/17    175      03/04/17
15/06/17    250      09/06/17
...
(PreviousDate is not part of the FACT table.  The first 2 cols are the FACT table.  The 3 cols together represent the format of my required output report)


CALENDAR TABLE
Date
01/01/17
02/01/17
03/01/17
...

1 to many relationship between Calendar[date] and Fact[Date]

measure Val:=Sum(Fact[Value])   - this works fine


My Attempts:
PreviousDate:=CALCULATE(MAX('Calendar'[Date]),FILTER('Calendar',[Val]<>0)) - this does return a value but it returns the same date as the date of each record.  So I tried to exclude the date of the record from the MAX calc, as follows:

PreviousDate:=CALCULATE(MAX('Calendar'[Date]),FILTER('Calendar',[Val]<>0),DATESBETWEEN('Calendar'[Date],MIN('Calendar'[Date]),DATEADD('Calendar'[Date],-1,DAY))) - but this throws an error

I have tried a couple of other variants, including using FILTER instead of DATESBETWEEN and I even had an EARLIER statement in there at some point but I just can't work it out.  

Any help would be greatly appreciated.  

Thanks in advance
Stuart
(Excel 2016)


----------



## gazpage (Jan 12, 2018)

PreviousDate = 
CALCULATE (
    MAX ( 'FACT'[Date] ),
    FILTER (
        ALL ('FACT'[Date] ),
        'FACT'[Date] < MAX ( 'FACT'[Date] )
    )
)


----------



## stuartbisset (Jan 12, 2018)

Thank you for your response, but this doesn't include the filter condition for [Val]<>0.  How would this be factored in?


----------



## stuartbisset (Jan 12, 2018)

My bad, I didn't notice at first that you used FACT date rather than Calendar Date.  Your solution works 

Many Thanks


----------



## gazpage (Jan 12, 2018)

To be clear, this finds the next lowest date in the fact table. Would pick up a date that was entered with a blank or 0 value.


----------



## stuartbisset (Jan 13, 2018)

Yep, Gotcha 

Its interesting that you chose to use Fact[Date] rather than Calendar[Date] to do the MAX on.  I had initially been trying to do it on the Calendar table - rationale being that it would be the smaller dimension therefore more efficient (potentially).  I'm sure that it could be done from that side.  I'm quite keen to understand WHY my version was wrong.  Purely out of interest and not out of need, as you have already solved this for me, I would be keen to see an alternative equivalent measure using the calendar table, but only if that isn't too much trouble (sorry I know its slightly cheeky to be asking for 2 solutions! ... it would really help my learning progress) 

Many Thanks


----------



## akice (Jan 15, 2018)

if you wanted to use your calendar table you can do this (assuming you have Calendar[Date] on rows )


```
Previous Date = VAR _dates =
    FILTER ( ALL ( Calendar[Date] ), Calendar[Date] < MAX ( Calendar[Date] ) )
RETURN
    LASTNONBLANK ( _dates, [Val] )
```


----------

