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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

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, @Anonymous

    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, @Anonymous

    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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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