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

View all the Fabric Data Days sessions on demand. View schedule

Reply
Anonymous
Not applicable

I can mimic SQL Lag but not SQL Lead... Dax help please!

Hello,

I'm trying to mimic the SQL function LEAD in Power BI.  I have a table representing emails that have been sent to customers.  There is a column for CustomerID, a column called Sequence which represents each email received by the customer in sequential order (there is a sequence of nine emails that each new customer gets, so there's Email 1, Email 2 etc), and the date that they received the email.

 

The fourth column, Date of Next Email, is what I want to generate.  In SQL I would use Lead to do this.

 

CustomerIDSequenceDateOfEmailDateOfNextEmail
A12021-01-012021-01-07
A22021-01-07 
B12021-01-022021-01-08
B22021-01-082021-01-23
B32021-01-23 

 

I have found a way to get the *previous* email date (i.e. Lag), but that's no use to me.  To get the previous email date, I use the method given here: From SQL to DAX- ‘Lead’ and ‘Lag’ window functions – Gal Love Data (xuanalytics.com)

 

i.e. 

calculate (max(DateOfEmail),
filter(tablename,
CustomerID=earlier(CustomerID)
&&
Sequence<earlier(Sequence)
))

 

I thought that it would be easy to change it from Lag to Lead - I thought I could change the < to a >.  However, when I do this, the calculated column populated with the LAST email date for each customer.  Not the next, but the last.  

 

The article I linked to suggests that this method works for both Lag and Lead, but the author only gave the syntax for Lag.

 

Any suggestions much appreciated!

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , These two columns will give you previous and next

 

maxx(filter(tablename, CustomerID=earlier(CustomerID) && Sequence<earlier(Sequence)),[DateOfEmail])

 

minx(filter(tablename, CustomerID=earlier(CustomerID) && Sequence >earlier(Sequence)),[DateOfEmail])

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

@Anonymous , These two columns will give you previous and next

 

maxx(filter(tablename, CustomerID=earlier(CustomerID) && Sequence<earlier(Sequence)),[DateOfEmail])

 

minx(filter(tablename, CustomerID=earlier(CustomerID) && Sequence >earlier(Sequence)),[DateOfEmail])

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

Hmm, something weird - I've noticed that the NextEmailDate *does* change, but only when the month changes.  So, let's say emails 1-4 are sent in January, the NextEmailDate will show for Emails 1-3 as the date that Email 4 is sent.  However, once we get into February, the NextEmailDate is showing as the last date in Fenruary that the customer received an email.  I'm completely baffled! 

Anonymous
Not applicable

Maybe ignore that - one of my date columns got reformatted as a US date, which confused everything.

Anonymous
Not applicable

OK, it worked!  Thanks so much, @amitchandak .  I got there in the end - you were right all along.  I'm glad it was a relatively short bit of syntax too - that gives me hope that I can start to understand it!

Anonymous
Not applicable

Thank you for your response.  Unfortunately, I am having exactly the same problem - the Lag one works perfectly, but the Lead one just brings up the LAST email date for each customer (instead of the next).

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Solution Authors
Top Kudoed Authors