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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mleepin
Helper I
Helper I

Dax to Sql, Windowing / Lag function (Over partition by equivalent in Dax)

I am coming from sql to dax.  I am hoping for some assistance please.  I am attempting to create a column that has the previous row value (LAG) for a group.

 

 

My column calc is as follows : 

 

Lag_Inv_Score1 = CALCULATE(MAX('Sheet1'[Inv_Score1]),FILTER(Sheet1,'Sheet1'[ID]=EARLIER('Sheet1'[ID]) &&  Sheet1[Date] < EARLIER(Sheet1[Date])))

 

 

However my result is not as expected.  For lag columns  Inv Score1  - for group AAA1 I am expecting 6, 5 but I we have 6,6.   I am wondering where I am going wrong.  Any advice here would be most appreciated.

 

 

screenshot.pngRegards  ML.

 

 

2 ACCEPTED SOLUTIONS
Phil_Seamark
Employee
Employee

HI @mleepin

 

I think this calculated column is close to what you need.  Your formula was returning the MAX value from all previous values, rather than the preceeding logical row.

 

Lav_Inv_Score1 = 
VAR myID = 'Sheet1'[ID]
VAR myLastDate = MAXX(FILTER('Sheet1','Sheet1'[ID]=myID && 'Sheet1'[Date] < EARLIER('Sheet1'[Date])),'Sheet1'[Date])
RETURN SUMX(FILTER('Sheet1','Sheet1'[Date] = myLastDate && 'Sheet1'[ID] = myID),'Sheet1'[Inv_Score1])

image.png


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

View solution in original post

Lead seems to go like this:

 

Lead_Inv_Score1 = VAR myID = Sheet1[ID] VAR myNextDate= MINX(FILTER(Sheet1,Sheet1[ID] = myID  && Sheet1[Date] > EARLIER(Sheet1[Date])), Sheet1[Date]) RETURN SUMX(FILTER(Sheet1, Sheet1[Date] = myNextDate && Sheet1[ID]  = myID), Sheet1[Inv_Score1]).

 

Regards  ML.

View solution in original post

3 REPLIES 3
Phil_Seamark
Employee
Employee

HI @mleepin

 

I think this calculated column is close to what you need.  Your formula was returning the MAX value from all previous values, rather than the preceeding logical row.

 

Lav_Inv_Score1 = 
VAR myID = 'Sheet1'[ID]
VAR myLastDate = MAXX(FILTER('Sheet1','Sheet1'[ID]=myID && 'Sheet1'[Date] < EARLIER('Sheet1'[Date])),'Sheet1'[Date])
RETURN SUMX(FILTER('Sheet1','Sheet1'[Date] = myLastDate && 'Sheet1'[ID] = myID),'Sheet1'[Inv_Score1])

image.png


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Lead seems to go like this:

 

Lead_Inv_Score1 = VAR myID = Sheet1[ID] VAR myNextDate= MINX(FILTER(Sheet1,Sheet1[ID] = myID  && Sheet1[Date] > EARLIER(Sheet1[Date])), Sheet1[Date]) RETURN SUMX(FILTER(Sheet1, Sheet1[Date] = myNextDate && Sheet1[ID]  = myID), Sheet1[Inv_Score1]).

 

Regards  ML.

Hi There,

 

Thank You  - this works well.  Regards  ML.  If I needed to do the same thing for lead  would the calc be very similar ?  Once again any advice here would be most appreciated.

 

ML.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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