Forum Discussion
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
- amitchandak
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/367415https://powerpivotpro.com/2012/03/the-correct-usage-of-earlier/