cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AlexoTomo
Regular Visitor

LAG / LEAD SQL Alternative in power bi. Partitioning.

Hi All, 

 

I've got a list of CustomerIDs and datetimes and a 'status' of either 1,2,3,4/5. e.g. I would like to get a date range by getting and EndDateTime.

 

This is what i have: 

CustomerID, DateTime, Status

1, 2020-02-01 03:00:00.000, 1 
1, 2020-02-01 04:00:00.000, 2

1, 2020-02-01 06:00:00.000, 3

2, 2020-02-03 07:00:00.000, 2

2, 2020-02-03 09:30:00.000, 3

2, 2020-02-03 10:30:00.000, 2

 

 

This is what i want: 


CustomerID, DateTime, EndDateTime, Status

1, 2020-02-01 03:00:00.000, 1,  2020-02-01 04:00:00.000,
1, 2020-02-01 04:00:00.000, 2,  2020-02-01 06:00:00.000

1, 2020-02-01 06:00:00.000, 3,  NULL

2, 2020-02-03 07:00:00.000, 2, 2020-02-03 09:30:00.000

2, 2020-02-03 09:30:00.000, 3, 2020-02-03 10:30:00.000

2, 2020-02-03 10:30:00.000, 2, NULL



In Sql I would usually use:
LEAD([DateTime]) OVER(PARTITION BY [CustomerID] ORDER BY [DateTime]) 
I am unable to use sql here though and need to achieve in Power BI Desktop

I want to do this in Power BI, preffarably not in power query editor but accept it may be easier in Power Query or not possible in Power BI Desktop. 

Thanks


1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @AlexoTomo ,

 

Please try:

EndDateTime = MINX(FILTER('Table',[CustomerID]=EARLIER('Table'[CustomerID]) &&  [DateTime]>EARLIER('Table'[DateTime])),[DateTime])

Eyelyn9_0-1637898485908.png

If it's not you wanted, please share more detail information to help us clarify your scenario.

 

 

Best Regards,
Eyelyn Qin
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

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @AlexoTomo ,

 

Please try:

EndDateTime = MINX(FILTER('Table',[CustomerID]=EARLIER('Table'[CustomerID]) &&  [DateTime]>EARLIER('Table'[DateTime])),[DateTime])

Eyelyn9_0-1637898485908.png

If it's not you wanted, please share more detail information to help us clarify your scenario.

 

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

manikumar34
Solution Sage
Solution Sage

@AlexoTomo , 

 

you can do this with PREVIOUS and EARLIER functions in power BI. 

Tyr the below solutions. 
https://community.powerbi.com/t5/Desktop/Compute-Lead-and-Lag/m-p/425809

 

https://datacaffee.com/sql-equivalent-power-bi-dax-functions-part-2/





If this helps, Appreciate your KUDOS!
Did I answer your question? Mark my post as a solution!


Proud to be a Super User!




Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors