Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Need help with Measure - value before and after 0

Hi team,   I have the following data:   ID Date mm/dd/yyyy hh:ss                             Item1            Item2                1 1/1/2022 0:00             3900       ...
  • v-yanjiang-msft's avatar
    4 years ago

    Hi Anonymous ,

    According to your description, here's my solution.

    Create two measures.

    Measure1 =
    VAR _Pre =
        MAXX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Date]
                    = MAXX (
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Date]
                                < MAXX ( FILTER ( ALL ( 'Table' ), 'Table'[Item1] = 0 ), 'Table'[Date] )
                        ),
                        'Table'[Date]
                    )
            ),
            'Table'[Item1]
        )
    VAR _Fir =
        MAXX (
            FILTER ( ALL ( 'Table' ), 'Table'[Date] = DATE ( YEAR ( TODAY () ), 1, 1 ) ),
            'Table'[Item1]
        )
    VAR _MAX =
        MAXX ( ALL ( 'Table' ), 'Table'[Date] )
    VAR _LAS =
        MAXX (
            FILTER (
                'Table',
                'Table'[Date] = DATE ( YEAR ( _MAX ), MONTH ( _MAX ), DAY ( _MAX ) )
            ),
            'Table'[Item1]
        )
    RETURN
        IF (
            COUNTROWS ( FILTER ( 'Table', 'Table'[Item1] = 0 ) ) > 0,
            _Pre - _Fir,
            _LAS - _Fir
        )
    
    Measure2 =
    VAR _Pre =
        MAXX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Date]
                    = MAXX (
                        FILTER (
                            ALL ( 'Table' ),
                            'Table'[Date]
                                < MAXX ( FILTER ( ALL ( 'Table' ), 'Table'[Item1] = 0 ), 'Table'[Date] )
                        ),
                        'Table'[Date]
                    )
            ),
            'Table'[Item1]
        )
    VAR _Fir =
        MAXX (
            FILTER ( ALL ( 'Table' ), 'Table'[Date] = DATE ( YEAR ( TODAY () ), 1, 1 ) ),
            'Table'[Item1]
        )
    VAR _MAX =
        MAXX ( ALL ( 'Table' ), 'Table'[Date] )
    VAR _LAS =
        MAXX (
            FILTER (
                'Table',
                'Table'[Date] = DATE ( YEAR ( _MAX ), MONTH ( _MAX ), DAY ( _MAX ) )
            ),
            'Table'[Item1]
        )
    RETURN
        IF (
            COUNTROWS ( FILTER ( 'Table', 'Table'[Item1] = 0 ) ) > 0,
            _LAS,
            _LAS - _Fir
        )
    

    Get the result.

    I attach my sample below for reference.

     

    Best Regards,
    Community Support Team _ kalyj

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