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

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

Reply
Anonymous
Not applicable

Leg function on Row_number in Dax

I am checking the missing index in my query and if so I skip the row in SQL. But because there are too many checks the query take long time and getting time out error in Power Query. My SQL query

 SELECT em.EqmmspvDate,
		em.EqmmspvEqmId,
		em.EqmmspvValue,
		em.Rownumber,
		em.t
FROM (

SELECT    e.[eqmmspveqmid], 
       e.[eqmmspvdate], 
       e.[eqmmspvvalue], 
       e.t, 
       e.rownumber,
	    Lag(e.rownumber, 1, 0) 
                OVER( 
                  partition BY [eqmmspveqmid] 
                   ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC)
				   AS 
              'previuos_rownumber'


FROM  (SELECT   Row_number() 
                OVER( 
                  partition BY [eqmmspveqmid] 
                  ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC) AS 
                    Rownumber, 
              [eqmmspveqmid], 
              [eqmmspvdate], 
              Lag(eqmmspvvalue, 1, 0) 
                OVER( 
                  partition BY [eqmmspveqmid] 
                  ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC) AS 
              'previuos_kilometerage', 
              Lead(eqmmspvvalue, 1, 0) 
                OVER( 
                  partition BY [eqmmspveqmid] 
                  ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC) AS 
              'next_kilometerage', 
              Max(Max(eqmmspvvalue)) 
                OVER( 
                  partition BY [eqmmspveqmid] 
                  ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC) AS 
                    'Last_value', 

              eqmmspvvalue, 
            [eqmmspvdifference]    AS              t			
																					
       FROM   [ULTIMO].[dba].[equipmentmspvalue] AS e 
	 
       WHERE  e.eqmmspvrecstatus = 2 
              AND e.[eqmmspvmspid] = 'DEFAULT' 
       GROUP  BY [eqmmspveqmid], 
                 eqmmspvdate, 
                 [eqmmspvvalue], 
                 [eqmmspvdifference] 
				 ) AS e 
       INNER JOIN #temp AS te 
              ON te.rownumber     = e.rownumber  

		WHERE  e.EqmmspvValue >=  e.previuos_kilometerage		   
 
 
GROUP  BY e.[eqmmspveqmid], 
          e.[eqmmspvdate], 
          e.eqmmspvvalue, 
          e.t, 
          e.rownumber) as em
		  
WHERE em.previuos_rownumber + 1  = em.Rownumber 
AND previuos_rownumber <> 0	
 

The bottelneck is here :
Lag(e.rownumber, 1, 0)
OVER(
partition BY [eqmmspveqmid]
ORDER BY Min(Datediff(hh, [eqmmspvdate], Getdate())) DESC)
AS
'previuos_rownumber'

 

 

Can we do the same in Dax?

 

regards, 

Ezz


 

1 REPLY 1
amitchandak
Super User
Super User

@Anonymous , Difficult to tell looking at SQL.

Can you share sample data and sample output in a table format?

 

But Rank and Earlier can help

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale
https://community.powerbi.com/t5/Community-Blog/Dynamic-TopN-made-easy-with-What-If-Parameter/ba-p/367415

 

https://powerpivotpro.com/2012/03/the-correct-usage-of-earlier/

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

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.