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

stuartbisset

New Member
Joined
Jan 12, 2018
Messages
4
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)
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

gazpage

Active Member
Joined
Apr 17, 2015
Messages
393
PreviousDate =
CALCULATE (
MAX ( 'FACT'[Date] ),
FILTER (
ALL ('FACT'[Date] ),
'FACT'[Date] < MAX ( 'FACT'[Date] )
)
)
 

stuartbisset

New Member
Joined
Jan 12, 2018
Messages
4
Thank you for your response, but this doesn't include the filter condition for [Val]<>0. How would this be factored in?
 

stuartbisset

New Member
Joined
Jan 12, 2018
Messages
4
My bad, I didn't notice at first that you used FACT date rather than Calendar Date. Your solution works :)

Many Thanks
 

gazpage

Active Member
Joined
Apr 17, 2015
Messages
393

ADVERTISEMENT

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

New Member
Joined
Jan 12, 2018
Messages
4
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

Board Regular
Joined
Apr 18, 2016
Messages
180
if you wanted to use your calendar table you can do this (assuming you have Calendar[Date] on rows )

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

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,329
Members
414,055
Latest member
mcarduner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top