I have a dataset of users who log into an app. I want to find the # of days between their last two logins. I have the DAX expression to get their last login (latest date)
=CALCULATE(Max([Date]),ALL(Table1),Table1[Name]=EARLIER(Table1[Name]))
But now I'd like to get their 2nd to last login, and subtract the two. I see some posts about the 2nd to last login, but it puts a blank if there are only two logins, whereas I want the number of days between these as well.
dcheney,
this one is tricky, but doable. It might be a bit difficult to understand but given you have already used EARLIER function, you are very close to your desired result with calculating the day difference between last and second-to-last date of login.
So assuming your source data look like this:
I would start with creating a new calculated column that would sort of rank each visit for specific user. This formula should do it:
This will rank add the user based rank to you datatable:
With this done, there is one more magic formula for another nested column (I have named it Date of Last Login) that does all the heavy lifting:
Honestly, this is one of the longest formula I have ever written in Powerpivot. You could do it with separated calculated columns, but I am not a big fan of that. This is what the formula basically does:
The resulting table then looks like this:
With that, you can then create a simple (Power)pivot table to do all the following (analytic) work that needs to be done.
Check out my source file in Excel (2013) if needed. Hope this helps!