Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Calculated column based on a value or a previous value

Hello, I need to create the calculated column "FLAG" with DAX. Its value will be a 1 if the Start for the ID is B or has been B in the past. Otherwise, the value will be 0.

 

IDYearStartStopFLAG
12000AB0
12001BC1
12002CC1
21990LK0
21991BM1
21992MN1
32021SB0
32022BT1
32024TU1
32025UV1
  • Here is another way to do it. This grabs all the rows of same ID, with same or earlier year, where Start = B; then checks if the result is not empty (ie we returned at least one row that meets the criteria). We then just cast the boolean to INT to get the desired output. We use VAR to capture our values from earlier row context rather than EARLIER*.

     

    FLAG = 
    VAR _thisYear = Data[Year]
    RETURN
    CALCULATE(
        CONVERT( NOT ISEMPTY( Data ), INTEGER ),
        ALLEXCEPT( Data, Data[ID] ),
        Data[Start] = "B",
        Data[Year] <= _thisYear
    )

     

     

    *EARLIER was a needed function before variables were introduced in DAX. Now, "it is recommended using variable (VAR) saving the value when it is still accessible, before a new row context hides the required row context to access the desired value." ( https://dax.guide/earlier )

7 Replies

  • Here is another way to do it. This grabs all the rows of same ID, with same or earlier year, where Start = B; then checks if the result is not empty (ie we returned at least one row that meets the criteria). We then just cast the boolean to INT to get the desired output. We use VAR to capture our values from earlier row context rather than EARLIER*.

     

    FLAG = 
    VAR _thisYear = Data[Year]
    RETURN
    CALCULATE(
        CONVERT( NOT ISEMPTY( Data ), INTEGER ),
        ALLEXCEPT( Data, Data[ID] ),
        Data[Start] = "B",
        Data[Year] <= _thisYear
    )

     

     

    *EARLIER was a needed function before variables were introduced in DAX. Now, "it is recommended using variable (VAR) saving the value when it is still accessible, before a new row context hides the required row context to access the desired value." ( https://dax.guide/earlier )

    • Anonymous's avatar
      Anonymous
      Not applicable

      Worked perfectly, I appreciate your help.

  • Hello Anonymous ,

     

    Can you use below code:

    FLAG =
    VAR CurrentID = 'Table'[ID]
    RETURN
    IF (
    CALCULATE (
    COUNTROWS('Table'),
    ALLEXCEPT('Table', 'Table'[ID]),
    'Table'[ID] = CurrentID,
    'Table'[Start] = "B"
    ) > 0,
    1,
    0
    )

  • v-sdhruv's avatar
    v-sdhruv
    Community Support

    Hi Anonymous ,
    Just wanted to check if you had the opportunity to review the suggestion provided?
    If the response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
    Thank You

  • Hi Anonymous ,

     

    So when you want to flag if a previous or current row (same ID) has “B” in the Start column up to a given year.

    Try this as a calculated column in DAX:

    FLAG =
    VAR CurrentID = Table[ID]
    VAR CurrentYear = Table[Year]
    RETURN
    IF (
        CALCULATE (
            COUNTROWS ( Table ),
            FILTER (
                Table,
                Table[ID] = CurrentID &&
                Table[Year] <= CurrentYear &&
                Table[Start] = "B"
            )
        ) > 0,
        1,
        0
    )
    

     

    For each row, it looks back at all rows for the same ID where the year is less than or equal to the current year, and checks if any have “B” in Start. If yes, it flags it as 1. If not, you get 0.

     

    Couple of tips, Make sure your [Year] column is a number, not text (if it’s text, just wrap with VALUE([Year])). If your data ever gets huge and you want to optimize, you can also do this in Power Query with a custom step. 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Obtaining this error. When I hover over the EARLIER parameter, it reads, "Parameter is not the correct type."

  • Hi,

    This calculated column formula works

    Flag = CALCULATE(COUNTROWS(Data),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&CALCULATE(MIN(Data[Year]),FILTER(Data,Data[ID]=EARLIER(Data[ID])&&Data[Start]="B"))<=EARLIER(Data[Year])&&Data[Start]="B"))+0