Forum Discussion

ahsan005's avatar
ahsan005
Frequent Visitor
3 years ago
Solved

Compare Previous Two Rows with a Current Row using DAX

Hi Everyone,   I have to create a new column and am trying to compare rows in a single column when a particular value exists in any of the rows. For e.g.   If Table[category] = "Site Down", then ...
  • AlexisOlson's avatar
    3 years ago

    There are newer DAX functions that are similar to SQL's LAG. For example, OFFSET:

     

    IsSiteDown =
    VAR Summary =
        SUMMARIZE (
            Table2,
            Table2[sitename],
            Table2[category],
            Table2[firstoccurrence]
        )
    VAR Prev1Cat =
        MAXX (
            OFFSET (
                -1,
                Summary,
                ORDERBY ( Table2[firstoccurrence] ),
                PARTITIONBY ( Table2[sitename] )
            ),
            Table2[category]
        )
    VAR Prev2Cat =
        MAXX (
            OFFSET (
                -2,
                Summary,
                ORDERBY ( Table2[firstoccurrence] ),
                PARTITIONBY ( Table2[sitename] )
            ),
            Table2[category]
        )
    VAR Result =
        IF (
            Table2[category] = "Site Down"
                && Prev1Cat = "Low Voltage"
                && Prev2Cat = "AC Mains Failure",
            "Yes",
            "No"
        )
    RETURN
        Result