Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Understanding the DAX Measure

Hallo all,

 

learning power bi und trying to understand how Earlier function works.

 

here is a small example with a following data set

IDValue1Value2
10620
11711
12815
13921

 

I have applied following DAX Mesure as calculated column.

 

 

 

FLAG = 
VAR _value1 = Table1[Value1]
VAR _value2 = Table1[Value2]
VAR Index = Table1[ID]
RETURN

        COUNTROWS(
            FILTER (
                Table1,
                (
                       Table1[Value1]<=_value1  && Table1[Value2] >= _value2 
                       
                )
                    &&  Table1[ID] <> Index 
            ))
      

 

 

 

Based on my manual caculation powerbi will go trough following combination 

 

 

DAx Mesaure Filter will filter alle True values and count the rest so according to me folllowing output has to be there (Manually Calculated )

 

IDValue1Value2FLAG
106201
117113
128153
139213

 

Power Bi gives me following output.

 

IDValue1Value2FLAG
10620 
117111
128151
139210

 

I would like to understand how this Measure gives me only on ID 11 and 12 FLAG 1.

 

please correct my understanding.

 

I have attached the excel with all combination and also pibx file for your refrence.

 

Excel File 

Powerbi.pbix 

 

Any explanation is greatly appreciated.  

  • Good to see you are digging in to understand DAX with a simple example first.  One comment.  You used "measure" to describe what you put in a calculated column.  While both Measures and Calculated Columns contain DAX expressions, they are not interchangeable.  For example, the first variable in your expression (=Table1[Value1]) would return an error in a Measure.  Calculated Columns have row context so it knows to get the value on that row (a measure would expect that column to be aggregated somehow).

     

    I would encourage you to learn to write measures wherever possible, and only do Calculated Columns when you need something for the axis/category/legend of a visual, or for performance reasons.

     

    In any case, the FILTER() function will return only rows that return TRUE().  In your case, there are three conditions.  To pass all three, the iterated row (in FILTER()) has to

    1 - be a different row than the current one (ID <> index)

    2 - Value1 has to be less than the stored Value1

    and

    3 - Value2 has to be greater than the stored Value2

     

    For the 1st row, there are no rows with Value1 <6, so you get blank

    For the 2nd row, only ID 10 row meets all 3, so you get a result of 1.

    For the 3rd row, only the 2nd row meets all 3, so you get a result of 1

    For the 4th row,  there are no rows with Value2 >21, so you get blank (not sure why you show 0 there)

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

6 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Greg_Deckler  I have read that post and I am also calculation MTTR, MTBF but my data is not the same as yours. I have to filter differents dates overlapping and some other stuff. that's why I am taking small example to understand the iteration with earlier function.

       

      can you explain me all iteration for any one ID of my dataset so that I can understand what's happening. 

       

      thanks for looking into my problem. 

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Good to see you are digging in to understand DAX with a simple example first.  One comment.  You used "measure" to describe what you put in a calculated column.  While both Measures and Calculated Columns contain DAX expressions, they are not interchangeable.  For example, the first variable in your expression (=Table1[Value1]) would return an error in a Measure.  Calculated Columns have row context so it knows to get the value on that row (a measure would expect that column to be aggregated somehow).

     

    I would encourage you to learn to write measures wherever possible, and only do Calculated Columns when you need something for the axis/category/legend of a visual, or for performance reasons.

     

    In any case, the FILTER() function will return only rows that return TRUE().  In your case, there are three conditions.  To pass all three, the iterated row (in FILTER()) has to

    1 - be a different row than the current one (ID <> index)

    2 - Value1 has to be less than the stored Value1

    and

    3 - Value2 has to be greater than the stored Value2

     

    For the 1st row, there are no rows with Value1 <6, so you get blank

    For the 2nd row, only ID 10 row meets all 3, so you get a result of 1.

    For the 3rd row, only the 2nd row meets all 3, so you get a result of 1

    For the 4th row,  there are no rows with Value2 >21, so you get blank (not sure why you show 0 there)

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • Anonymous's avatar
      Anonymous
      Not applicable

      mahoneypat 

       

      Thank you very much for your clarification.

       

      still there is two  more thing to understand.

       

      in my Dax Expression i have created two variables

      VAR _value1 = Table1[Value1]
      VAR _value2 = Table1[Value2]

      in the filter expression i have following formula

      Table1[Value1]<=_value1  && Table1[Value2] >= _value2 

      as per my understanding the above expression is equivalent to 

      Table1[Value1]<=EARLIER(Table1[Value1])  && Table1[Value2] >= EARLIER(Table1[Value2]) 

       

      correct?

       

      2.

      what is the difference when i write my expression something like this.

      _value1 <= Table1[Value1] && _value2 >= Table1[Value2]

       it still evaulates the same way what you have explain in your comment or it is completly different.

       

      my confusion is i have seen different post and in some post they wirte earlier function in the beginning and some time after so what will be the diffrence in my case. how my expression is caluclate in this sicenario.

       

      Regads,

      tar.