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
WTAS80486
Helper IV
Helper IV

how to compare two rows in column and return value in DAX calculated column

Ankita80486_0-1661956767123.png

I am using this to calculate cumulative runhours

 

Rolling Runhours = CALCULATE (
                SUM ( 'MeterReading'[ Run Hours]),
                FILTER (
                    ALL ( 'MeterReading'),
                    'MeterReading'[Asset PK ID] = EARLIER ( 'MeterReading'[Asset PK ID]  )
                        && 'MeterReading'[readingdate]<= EARLIER ('MeterReading'[readingdate])
                )
            )
 
But this returns
 
Ankita80486_1-1661956892462.png

same value for same dates. I want to have 0 in first row for 11/9 and 265 in second row for 11/9

 

How can i achieve that?

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

Hi @WTAS80486 ,

I have create a simple sample, please refer to it to see if it helps you.

Add an index column first.

Then create a measure.

Measure =
VAR _count =
    CALCULATE (
        COUNT ( MeterReading[readingdate] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
VAR _1mindate =
    CALCULATE (
        MIN ( MeterReading[Index] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
VAR _result =
    CALCULATE (
        SUM ( MeterReading[Run Hours] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] <= SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
RETURN
    IF ( _count <> 1 && _1mindate = MAX ( MeterReading[Index] ), 0, _result )

 

or a column.

Column =
VAR _count =
    CALCULATE (
        COUNT ( MeterReading[readingdate] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = EARLIER ( MeterReading[readingdate] )
        )
    )
VAR _1mindate =
    CALCULATE (
        MIN ( MeterReading[Index] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = EARLIER ( MeterReading[readingdate] )
        )
    )
VAR _result =
    CALCULATE (
        SUM ( MeterReading[Run Hours] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] <= EARLIER ( MeterReading[readingdate] )
        )
    )
RETURN
    IF ( _count <> 1 && _1mindate = ( MeterReading[Index] ), 0, _result )

vpollymsft_0-1662348014683.png

If I have misunderstood your meaning, please provide a pbix file without privacy information and more details with your desired output.

 

Best Regards

Community Support Team _ Polly

 

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

3 REPLIES 3
v-rongtiep-msft
Community Support
Community Support

Hi @WTAS80486 ,

I have create a simple sample, please refer to it to see if it helps you.

Add an index column first.

Then create a measure.

Measure =
VAR _count =
    CALCULATE (
        COUNT ( MeterReading[readingdate] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
VAR _1mindate =
    CALCULATE (
        MIN ( MeterReading[Index] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
VAR _result =
    CALCULATE (
        SUM ( MeterReading[Run Hours] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] <= SELECTEDVALUE ( MeterReading[readingdate] )
        )
    )
RETURN
    IF ( _count <> 1 && _1mindate = MAX ( MeterReading[Index] ), 0, _result )

 

or a column.

Column =
VAR _count =
    CALCULATE (
        COUNT ( MeterReading[readingdate] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = EARLIER ( MeterReading[readingdate] )
        )
    )
VAR _1mindate =
    CALCULATE (
        MIN ( MeterReading[Index] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] = EARLIER ( MeterReading[readingdate] )
        )
    )
VAR _result =
    CALCULATE (
        SUM ( MeterReading[Run Hours] ),
        FILTER (
            ALL ( MeterReading ),
            MeterReading[readingdate] <= EARLIER ( MeterReading[readingdate] )
        )
    )
RETURN
    IF ( _count <> 1 && _1mindate = ( MeterReading[Index] ), 0, _result )

vpollymsft_0-1662348014683.png

If I have misunderstood your meaning, please provide a pbix file without privacy information and more details with your desired output.

 

Best Regards

Community Support Team _ Polly

 

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

Kriekis
Frequent Visitor

1. You can add Index column

Kriekis_0-1661971032528.png

2. Then You add readingdate_v2

Kriekis_1-1661971106344.png

3. And finally Your [Rolling Runhours] think works fine

Kriekis_2-1661971196094.png

 

This solution works fine if You don't get exactly the same runhours in the same day multiple times.

If this is a case then adding additional Index column in Power Query can help here.

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.

Top Solution Authors