Forum Discussion

waleed111's avatar
waleed111
Helper V
5 years ago

Row context

what is the row context? and what is its relation with EARLIER function? (i need explanation with some examples)

3 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    waleed111 

    Row context is only enforced when you are in a calculated column situation or you are using ADDCOLUMNS in a measure. Basically means that the row adds context to your calculation.

     

    See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
    The basic pattern is:
    Column = 
      VAR __Current = [Value]
      VAR __Previous = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Value])
    RETURN
      __Current - __Previous

     

    EARLIER is the worst named function ever. Within a column formula it basically means "value in the current row". Many people eschew EARLIER and use something like VAR __Current = [Column], either way. 

  • waleed111 ,

    Row Context - When you create a measure then it works at the grouped data level. Means calculation will happen at city wise, state wise, category wise, etc.

    If I have formula sum(Table[A]) / Sum(Table[B]) , the will first sum up till that level and then divided.

     

    Now if use a filter

    if(Max(City[City]) = "Abc", [Measure1], [MeasureB]), Now from where City value will come here; one that is in the row of your visual. That is your row context. If not in a row; then selected value on-page.  Else it this will get blank value.

     

    Row context help in many calculations. time intelligence.

    Color measure for conditional formatting

    Color Date = if(FIRSTNONBLANK('Date'[Date],TODAY()) <today(),"lightgreen","red")

     

    refer: https://www.sqlbi.com/articles/row-context-and-filter-context-in-dax/

     

    Earlier - Very High level is that it provides another copy to create lookup. We have partitioned data to get value.

    example

    First Sales = minx(filter(Sales, Sales[Customer Id]=EARLIER(Sales[Customer Id]) ),Sales[Sales Date])
    Last Sales = maxx(FILTER(Sales,[Customer Id]=EARLIER(Sales[Customer Id]) && Sales[Sales Date] >EARLIER(Sales[Sales Date])),[Sales Date])

     

    Refer: https://www.red-gate.com/simple-talk/sql/bi/cracking-dax-the-earlier-and-rankx-functions/

     

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, waleed111 

     

    DAX automatically creates a row context when you define a calculated column, use X-ending functions(SUMX, MAXX ...), 'ADDCOLUMNS', 'SUMMERIZE'. 

     

    When you defined the calculated column, DAX started an iteration from the first row of the table; it created a row context containing that row and evaluated the expression. Then it moved on to the second row and evaluated the expression again. A row context is a context that always contains a single row and DAX automatically defines it during the creation of calculated columns. 

     

    Actually, there are always two context: the row context and the filter context. They are the only kind of contexts in DAX. Thus, they are the only way to modify the result of a formula. Any formula will be evaluated under these two distinct contexts: the row context and the filter context.

     

    For further information, you may refer to the link .

     

    Best Reagrds

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.