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
AnishPNair
Frequent Visitor

EARLIER FUNCTION

Hi @Greg_Deckler or Anyone,

 

I started to learn Power BI using Learn Power BI Second Edition book and the Earlier Function was used in Ch 5 . I am not able to understand the Earlier function at all. I went through the docs ( https://learn.microsoft.com/en-us/dax/earlier-function-dax) and i still didn't follow . It mentions outer evaluation pass of the mentioned column . What does "outer evaluation pass of the mentioned column" mean ? Can someone explain the function in simple layman language . 

 

 

 

 

1 ACCEPTED SOLUTION

@AnishPNair Perhaps an image will help:

Greg_Deckler_0-1671331299130.png

So let's just focus on this piece of the code:

 

        ADDCOLUMNS(
            __Table,
            "__Inventory", __Inventory - SUMX(FILTER(__Table,[Date]<=EARLIER([Date])),[Value])
        )

 

The ADDCOLUMNS function takes a table and adds a calculated column. In this case our starting table just has Date and Value columns in it and we are adding a column called __Inventory. This is the main table shown in the image and for each row it is going to execute the forumula:

 

__Inventory - SUMX(FILTER(__Table,[Date]<=EARLIER([Date])),[Value])

 

Within this code is a FILTER statement. This FILTER statement creates a "current context" for the SUMX function. The "earlier" context is the row context in which the __Inventory column is being calculated for the current row, in this case I chose January 3rd, 2022 but remember that every row in the table is getting the same calculation performed. Thus, when the SUMX(FILTER... statement executes for the January 3 2022 row, using EARLIER refers to this row context and thus EALIER([Date]) returns January 3 2022, the value for the current row being iterated on and thus the FILTER statement forming the current context for the SUMX function returns the table highlighted in orange on the right of the image. Thus, when summing up the Value column within this context, it would be 20 + 40 + 30 or 90 and thus return 2000 - 90 = 1910 for the January 3 2022 row.

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

9 REPLIES 9
Greg_Deckler
Super User
Super User

@AnishPNair Thanks @ImkeF, great stuff. Consider that these are equivalent statements:

Days of Supply = 
    VAR __Inventory = 2000
    VAR __Date = TODAY()
    VAR __Table = FILTER(ALLSELECTED('Table'),[Date]>=__Date)
    VAR __Table1 = 
        ADDCOLUMNS(
            __Table,
            "__Inventory", __Inventory - SUMX(FILTER(__Table,[Date]<=EARLIER([Date])),[Value])
        )
    VAR __EndDate = MAXX(FILTER(__Table1,[__Inventory]>=0),[Date])
RETURN
    (__EndDate - __Date) * 1.




Days of Supply sans EARLIER = 
    VAR __Inventory = 2000
    VAR __Date = TODAY()
    VAR __Table = FILTER(ALLSELECTED('Table'),[Date]>=__Date)
    VAR __Table1 = 
        ADDCOLUMNS(
            __Table,
            "__Inventory", 
                VAR __CurrentDate = [Date]
            RETURN
                __Inventory - SUMX(FILTER(__Table,[Date]<=__CurrentDate),[Value])
        )
    VAR __EndDate = MAXX(FILTER(__Table1,[__Inventory]>=0),[Date])
RETURN
    (__EndDate - __Date) * 1.

I am also attaching the PBIX for additional reference. Basically using EARLIER is the same as creating a VAR outside of the current function essentially and using that variable within the function.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thanks for replying to the post. However i am still not able to understand as i am not familiar with variables in DAX. Is there a simpler way as i just started reading your book .

@AnishPNair Perhaps an image will help:

Greg_Deckler_0-1671331299130.png

So let's just focus on this piece of the code:

 

        ADDCOLUMNS(
            __Table,
            "__Inventory", __Inventory - SUMX(FILTER(__Table,[Date]<=EARLIER([Date])),[Value])
        )

 

The ADDCOLUMNS function takes a table and adds a calculated column. In this case our starting table just has Date and Value columns in it and we are adding a column called __Inventory. This is the main table shown in the image and for each row it is going to execute the forumula:

 

__Inventory - SUMX(FILTER(__Table,[Date]<=EARLIER([Date])),[Value])

 

Within this code is a FILTER statement. This FILTER statement creates a "current context" for the SUMX function. The "earlier" context is the row context in which the __Inventory column is being calculated for the current row, in this case I chose January 3rd, 2022 but remember that every row in the table is getting the same calculation performed. Thus, when the SUMX(FILTER... statement executes for the January 3 2022 row, using EARLIER refers to this row context and thus EALIER([Date]) returns January 3 2022, the value for the current row being iterated on and thus the FILTER statement forming the current context for the SUMX function returns the table highlighted in orange on the right of the image. Thus, when summing up the Value column within this context, it would be 20 + 40 + 30 or 90 and thus return 2000 - 90 = 1910 for the January 3 2022 row.

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler . It is clear now. However if you can explain what current context means, it would be helpful .

@AnishPNair Well, the short answer is something along the lines of, let's say you have a Calendar table for 2022 and you have a slicer for Month. You choose October. Your current starting context is all of the dates in October. If you then change that to November, your current context is November. However, context is a very deep subject and DAX formulas can change the context. For example, if you have November selected but do this:

COUNTROWS(ALL('Calendar'))

The ALL function has changed the original context and this this will return the count of all rows in 2022. There is lots and lots written on context, I suggest you read these:

Context in DAX Formulas - Microsoft Support

Learn DAX basics in Power BI Desktop - Power BI | Microsoft Learn

DAX overview - DAX | Microsoft Learn

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
ImkeF
Super User
Super User

Hi @AnishPNair ,
maybe these will get you started:
Understanding Evaluation Context in DAX - BI Gorilla
Microsoft Power BI: Deep dive into DAX evaluation context - BRK3060 - YouTube

it's not easy, but one of the basic pillars of the DAX-language.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thanks @ImkeF . I will take a look . Please bear with me in case i have more questions.

AnishPNair
Frequent Visitor

Hi @ImkeF 

 

Thank you for the response. I am still clueless . I went through the link and not able to understand . Is there anything that can help me understand DAXs evaluation contexts. Thanks 

ImkeF
Super User
Super User

Hi @AnishPNair ,
with the introduction of variables in DAX, the usage of the (not so easy to understand) EARLIER function has become obsolete in most of the cases. This might help: EARLIER – DAX Guide
My understanding of EARLIER is that it references the outer row context of the current evaluation context. So an understanding of DAXs evaluation contexts is needed to get a hang of it at all.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.