Forum Discussion

AnishPNair's avatar
AnishPNair
Frequent Visitor
3 years ago
Solved

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...
  • Greg_Deckler's avatar
    Greg_Deckler
    3 years ago

    AnishPNair Perhaps an image will help:

    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.