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
jsanchezm
Frequent Visitor

Create a measure comparing two values of different rows and columns if they meet two conditions

Hi,

 

I'm stuck trying to create a measure that compares values from two columns of two different rows, but from the same table, if, and only if, they meet two conditions:

In example: if the two rows are from the same date (data in my language) and it is also from the same bus, compare the H_FIN from the first one to the H_INI of the second one. For the data included in the next image, the first row wouldn't be compared to any other row, as it meets the date condition, but not the bus one (it is the only one with the bus 12377). The second row wouldn't need to be compared, as it has not any previous row of that meets the two condition criteria, but the third one as it is from the same date and bus of the previous one, would need to calculate and get the value "01:55" as it is the duration between the H_FIN of the first row and H_INI of the second one.

 

jsanchezm_0-1649843877895.png

 

I've seen some posts talking about similar things, but they don't seem to fit with my problem or, at least, I can't see how to work the examples for my problem.

 

Any hints, please? Thanks in advance for your help.

 

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

Hi @jsanchezm ,

According to your description, here's my solution.

Create a measure.

 

Diff =
VAR _Pre =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[DATE] = MAX ( 'Table'[DATE] )
                && 'Table'[BUS] = MAX ( 'Table'[BUS] )
                && 'Table'[IDTRAM] < MAX ( 'Table'[IDTRAM] )
        ),
        'Table'[H_FIN]
    )
RETURN
    IF ( _Pre = BLANK (), BLANK (), MAX ( 'Table'[H_INI] ) - _Pre )

 

Get the correct result.

vkalyjmsft_0-1650249295676.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

1 REPLY 1
v-yanjiang-msft
Community Support
Community Support

Hi @jsanchezm ,

According to your description, here's my solution.

Create a measure.

 

Diff =
VAR _Pre =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[DATE] = MAX ( 'Table'[DATE] )
                && 'Table'[BUS] = MAX ( 'Table'[BUS] )
                && 'Table'[IDTRAM] < MAX ( 'Table'[IDTRAM] )
        ),
        'Table'[H_FIN]
    )
RETURN
    IF ( _Pre = BLANK (), BLANK (), MAX ( 'Table'[H_INI] ) - _Pre )

 

Get the correct result.

vkalyjmsft_0-1650249295676.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.

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.