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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ranis1227
Helper I
Helper I

DAX for LAG

Hello,

 

Sorry if this redundant. Please link a previous solution if applicable. 

 

I am looking to running a LAG similar to SQL partitioned by category name and visit number, ordered by unit. 

 

Here is an example, the highlighed transfer unit is what I am looking for:

 

ranis1227_0-1657644549617.png

The data is huge and SQL takes a while to load with it.  Thank you again. 

2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

Hi @ranis1227 

Please try

Transfer Unit =
VAR CurrentVisitNumTable =
    CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[Visit_Number] ) )
VAR CurrentDateTime = TableName[Check_In_Time]
VAR TableAfter =
    FILTER ( CurrentVisitNumTable, TableName[Check_In_Time] > CurrentDateTime )
VAR NextRecordTable =
    TOPN ( 1, TableAfter, TableName[Check_In_Time], ASC )
VAR Result =
    MAXX ( NextRecordTable, TableName[Category_Name] )
RETURN
    Result

View solution in original post

Good morning @ranis1227 
Here is a sample file with the solution https://we.tl/t-5GlKHJIedF

1.png

From Unit = 
VAR CurrentVisitNumTable =
    CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[visit_number ] ) )
VAR CurrentDateTime = TableName[Check_In_Time]
VAR TableBefore =
    FILTER ( CurrentVisitNumTable, TableName[Check_In_Time] < CurrentDateTime )
VAR PreviousRecordTable =
    TOPN ( 1, TableBefore, TableName[Check_In_Time] )
VAR Result =
    MAXX ( PreviousRecordTable, TableName[To Unit] )
RETURN
    Result

View solution in original post

10 REPLIES 10
ranis1227
Helper I
Helper I

Outstanding! Thank you very much @tamerj1   I have been spinning my wheels for about a week now on this.  This will help with future lags in our data. 

tamerj1
Super User
Super User

Hi @ranis1227 

Please try

Transfer Unit =
VAR CurrentVisitNumTable =
    CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[Visit_Number] ) )
VAR CurrentDateTime = TableName[Check_In_Time]
VAR TableAfter =
    FILTER ( CurrentVisitNumTable, TableName[Check_In_Time] > CurrentDateTime )
VAR NextRecordTable =
    TOPN ( 1, TableAfter, TableName[Check_In_Time], ASC )
VAR Result =
    MAXX ( NextRecordTable, TableName[Category_Name] )
RETURN
    Result

This is exactly what I am looking for.  Thank you! 

@ranis1227 
How many rows do you have in this table?

Over a 1000.  However, I am filtering some out in power query.

When you said huge I though milions 🙂 I was worried about the performance 😅

@tamerj1 

Sorry about this. I misinterpreted the issue.  The actual data should look like this:

 

To Unitvisit_number check_in_timeFrom Unit
9A50162262696/24/2022 2:37 
12BI50162262697/7/2022 4:409A
12BI50162262697/7/2022 22:5512BI
5JIN50162262697/8/2022 15:2412BI
12BI50162262697/8/2022 15:325JIN
5JIN50162262697/8/2022 17:0712BI
12BI50162262697/9/2022 18:485JIN

 

I updated your dax on various iterations and could not get it.   Let me know if you have any ideas. Thank you again for the input. Sorry for the confusion.  

@ranis1227 
From Unit is the column to be calculated?

 

Correct. 

Good morning @ranis1227 
Here is a sample file with the solution https://we.tl/t-5GlKHJIedF

1.png

From Unit = 
VAR CurrentVisitNumTable =
    CALCULATETABLE ( TableName, ALLEXCEPT ( TableName, TableName[visit_number ] ) )
VAR CurrentDateTime = TableName[Check_In_Time]
VAR TableBefore =
    FILTER ( CurrentVisitNumTable, TableName[Check_In_Time] < CurrentDateTime )
VAR PreviousRecordTable =
    TOPN ( 1, TableBefore, TableName[Check_In_Time] )
VAR Result =
    MAXX ( PreviousRecordTable, TableName[To Unit] )
RETURN
    Result

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.