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
Anonymous
Not applicable

Compute Lead and Lag

Hello All,

 

How do you perform lead and lag functions in DAX?

 

Thanks

Aditya

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Anonymous,

 

The DAX calculations are based on context so if you want to perform calculations that are based on the previous records or next records you have several ways of doing this from the EARLIER function, the time intelligence functions, the usage of ID column.

 

I have setup a simple model with tabel below:

 

ID             ENAME     JOB               SAL

1MILLERCLERK1300
2CLARKMANAGER2450
3KINGPRESIDENT5000
4SMITHCLERK800
5ADAMSCLERK1100
6JONESMANAGER2975
7SCOTTANALYST3000
8FORDANALYST3000
9JAMESCLERK950
10MARTINSALESMAN1250
11WARDSALESMAN1250
12TURNERSALESMAN1500
13ALLENSALESMAN1600
14BLAKEMANAGER2850
15MARTINSALESMAN1250
16WARDSALESMAN1250
17TURNERSALESMAN1500
18ALLENSALESMAN1600
19BLAKEMANAGER2850

 

If you add this two measure you will get the calculation with previous row and next row however this is just an example, I did it with a measure but you can also do it in the query editor making it like SQL with an addtinal column. So you have several ways of making LEAD and LAG calculations depending on your model and necessities.

 

Previous Difference =
VAR Selecet_Emp =
    ( MAX ( Salary[ID] ) - 1 )
RETURN
    IF (
        HASONEFILTER ( Salary[ID] );
        SUM ( Salary[SAL] )
            - CALCULATE (
                SUM ( Salary[SAL] );
                FILTER (
                    ALL ( Salary[ID]; Salary[ENAME]; Salary[JOB] );
                    Salary[ID] = Selecet_Emp
                )
            );
        BLANK ()
    )




Next Difference = 
VAR Selecet_Emp =
    ( MAX ( Salary[ID] ) + 1 )
RETURN
    IF (
        HASONEFILTER ( Salary[ID] );
        SUM ( Salary[SAL] )
            - CALCULATE (
                SUM ( Salary[SAL] );
                FILTER (
                    ALL ( Salary[ID]; Salary[ENAME]; Salary[JOB] );
                    Salary[ID] = Selecet_Emp
                )
            );
        BLANK ()
    )

cal.png

 

Regards,

MFelix

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1 REPLY 1
MFelix
Super User
Super User

Hi @Anonymous,

 

The DAX calculations are based on context so if you want to perform calculations that are based on the previous records or next records you have several ways of doing this from the EARLIER function, the time intelligence functions, the usage of ID column.

 

I have setup a simple model with tabel below:

 

ID             ENAME     JOB               SAL

1MILLERCLERK1300
2CLARKMANAGER2450
3KINGPRESIDENT5000
4SMITHCLERK800
5ADAMSCLERK1100
6JONESMANAGER2975
7SCOTTANALYST3000
8FORDANALYST3000
9JAMESCLERK950
10MARTINSALESMAN1250
11WARDSALESMAN1250
12TURNERSALESMAN1500
13ALLENSALESMAN1600
14BLAKEMANAGER2850
15MARTINSALESMAN1250
16WARDSALESMAN1250
17TURNERSALESMAN1500
18ALLENSALESMAN1600
19BLAKEMANAGER2850

 

If you add this two measure you will get the calculation with previous row and next row however this is just an example, I did it with a measure but you can also do it in the query editor making it like SQL with an addtinal column. So you have several ways of making LEAD and LAG calculations depending on your model and necessities.

 

Previous Difference =
VAR Selecet_Emp =
    ( MAX ( Salary[ID] ) - 1 )
RETURN
    IF (
        HASONEFILTER ( Salary[ID] );
        SUM ( Salary[SAL] )
            - CALCULATE (
                SUM ( Salary[SAL] );
                FILTER (
                    ALL ( Salary[ID]; Salary[ENAME]; Salary[JOB] );
                    Salary[ID] = Selecet_Emp
                )
            );
        BLANK ()
    )




Next Difference = 
VAR Selecet_Emp =
    ( MAX ( Salary[ID] ) + 1 )
RETURN
    IF (
        HASONEFILTER ( Salary[ID] );
        SUM ( Salary[SAL] )
            - CALCULATE (
                SUM ( Salary[SAL] );
                FILTER (
                    ALL ( Salary[ID]; Salary[ENAME]; Salary[JOB] );
                    Salary[ID] = Selecet_Emp
                )
            );
        BLANK ()
    )

cal.png

 

Regards,

MFelix

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.

Top Solution Authors