Forum Discussion
EARLIER FUNCTION
- 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.
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.
Thanks Greg_Deckler . It is clear now. However if you can explain what current context means, it would be helpful .
- Greg_Deckler3 years agoCommunity Champion
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