Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
EmaVasileva
Helper V
Helper V

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            

1000 

2

1/2/2022 2:00            

4000            

1200

3

2/15/2022 11:00        

8000            

5000

4

2/15/2022 12:00        

0                  

5100

5

5/17/2022 0:00          

500              

6000

6

6/7/2022 0:00            

1000            

7000

7

6/7/2022 01:00          

1100

7000

 

I have a value column and sometimes it is possible to have 0 in a row. I need measures which calculate the following:

Measure1: If in Item1 column there is 0, then give me the difference between the previous row value and the value on 1/1/currentyear 0:00 h (8000 – 3900 = 4100). If there isn’t 0 in the column, then give me difference between the latest row 0:00 h and the first row 1/1/current year 0:00.

Measure2:   If in Item1 column there is 0, then give me the difference between the last available row value at 0:00 h and the row with the 0.  (1000 – 0 = 1000). If there isn’t 0, then give me difference between the latest row 0:00 h and the first row 1/1/current year 0:00.

 

Thank you.

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @EmaVasileva ,

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.

vkalyjmsft_0-1655270916207.png

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.

 

View solution in original post

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @EmaVasileva ,

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.

vkalyjmsft_0-1655270916207.png

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.

 

danextian
Super User
Super User

Hi @EmaVasileva ,

 

  • Can't this be done with calculated columns instead of measures? It would be easier to follow this route.
  • If there isn’t 0 in the column, then give me difference between the latest row 0:00 h and the first row 1/1/current year 0:00. Whatis latest row and what is first row? Are you referring to the previous and current rows or to rows 1 and 7?









Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.