Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
NatashaSchuster
Helper II
Helper II

Replicate LAG T-SQL function logic


Hello, 

 

I was hoping to get an answer to this problem both in DAX and M. 

I am trying to replicate LAG T-SQL function below and return number of days since the last order

SELECT
  CustomerID
, SalesOrderID
, CAST(OrderDate as DATE) AS OrderDate
, DATEDIFF(d,LAG(OrderDate) OVER (PARTITION BY CustomerID ORDER BY SalesOrderID), OrderDate) as DaysSinceLastOrder
FROM Sales.SalesOrderHeader;

 

 

We are trying to get here is the number of days since the last order.

e.g. 7/22/2005 to 7/22/2007 is 730 days and 

7/22/2007 to 11/4/2007 is 105 days 

 

CustomerIDSalesOrderIDOrderDateDaysSinceLastOrder
11000437937/22/2005NULL
11000515227/22/2007730
110005741811/4/2007105
11001437677/18/2005NULL
11001514937/20/2007732
11001727736/12/2008328
11002437367/10/2005NULL
11002512387/4/2007724
11002532378/27/200754
11003437017/1/2005NULL
11003513157/9/2007738
110035778311/11/2007125
11004438107/26/2005NULL
11004515957/26/2007730
110045729311/2/200799

 

 

I started putting together a DAX code but it returns total number of days which for the exaple above for the 3 row instead of 105 return  835 (730+105). 

 

 

Spoiler
  = DATEDIFF(
        CALCULATE(MIN(LAGfunction[OrderDate]), ALLEXCEPT(LAGfunction, LAGfunction[CustomerID]) ),
        CALCULATE(MAX(LAGfunction[OrderDate]), ALLEXCEPT(LAGfunction,  LAGfunction[CustomerID], LAGfunction[SalesOrderID]) ),
        DAY 
    )

 

image.png

 

Thank you

 

2 ACCEPTED SOLUTIONS
v-lili6-msft
Community Support
Community Support

hi, @NatashaSchuster

    After my research, you can do these follow my steps as below:

Step1:

use rankx function to add a group rank column

group rank = RANKX(FILTER(LAGfunction,LAGfunction[CustomerID]=EARLIER(LAGfunction[CustomerID])),LAGfunction[SalesOrderID],,ASC)

Step2:

use eralier function to add the result column

Column = DATEDIFF(CALCULATE(MAX(LAGfunction[OrderDate]),FILTER(LAGfunction,LAGfunction[CustomerID]=EARLIER(LAGfunction[CustomerID])&&LAGfunction[group rank]=EARLIER(LAGfunction[group rank])-1)),LAGfunction[OrderDate],DAY)

Result:

1.PNG

 

here is pbix, please try it.

https://www.dropbox.com/s/8ivwqgzxqmk7byg/Replicate%20LAG%20T-SQL%20function%20logic.pbix?dl=0

 

Best Regards,

Lin

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

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

 

Try this calculated column formula

 

=if(ISBLANK(CALCULATE(MAX(Data[OrderDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[OrderDate]<EARLIER(Data[OrderDate])))),BLANK(),[OrderDate]-CALCULATE(MAX(Data[OrderDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[OrderDate]<EARLIER(Data[OrderDate]))))

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

 

Try this calculated column formula

 

=if(ISBLANK(CALCULATE(MAX(Data[OrderDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[OrderDate]<EARLIER(Data[OrderDate])))),BLANK(),[OrderDate]-CALCULATE(MAX(Data[OrderDate]),FILTER(Data,Data[CustomerID]=EARLIER(Data[CustomerID])&&Data[OrderDate]<EARLIER(Data[OrderDate]))))

 

Hope this helps.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-lili6-msft
Community Support
Community Support

hi, @NatashaSchuster

    After my research, you can do these follow my steps as below:

Step1:

use rankx function to add a group rank column

group rank = RANKX(FILTER(LAGfunction,LAGfunction[CustomerID]=EARLIER(LAGfunction[CustomerID])),LAGfunction[SalesOrderID],,ASC)

Step2:

use eralier function to add the result column

Column = DATEDIFF(CALCULATE(MAX(LAGfunction[OrderDate]),FILTER(LAGfunction,LAGfunction[CustomerID]=EARLIER(LAGfunction[CustomerID])&&LAGfunction[group rank]=EARLIER(LAGfunction[group rank])-1)),LAGfunction[OrderDate],DAY)

Result:

1.PNG

 

here is pbix, please try it.

https://www.dropbox.com/s/8ivwqgzxqmk7byg/Replicate%20LAG%20T-SQL%20function%20logic.pbix?dl=0

 

Best Regards,

Lin

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

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.