Forum Discussion

Ncf5031's avatar
Ncf5031
Advocate I
4 years ago
Solved

Difference in Row Values Based on Other Columns

Good afternoon,   I'm hoping I can get some clarification on an issue I've run into.    For each piece of equipment that I track, I also track the hours that were accrued on that S/N. For certain...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Ncf5031 ,

    I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a measure as below:

    Hrs Diff = 
    VAR _selsn =
        SELECTEDVALUE ( 'Failures'[S/N] )
    VAR _curdate =
        SELECTEDVALUE ( 'Failures'[date] )
    VAR _predate =
        CALCULATE (
            MAX ( 'Failures'[date] ),
            FILTER ( ALLSELECTED ( 'Failures' ), 'Failures'[date] < _curdate )
        )
    VAR _prehours =
        CALCULATE (
            SUM ( 'Failures'[Hours] ),
            FILTER ( ALLSELECTED ( 'Failures' ), 'Failures'[date] = _predate )
        )
    RETURN
        IF ( ISBLANK ( _prehours ), BLANK (), SUM ( 'Failures'[Hours] ) - _prehours )

    If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

    How to upload PBI in Community

    Best Regards

  • Ncf5031's avatar
    Ncf5031
    4 years ago

    Thank you all for your help! I ended up using techniques from the suggestions of both amitchandak and Anonymous.

    Below is the code snippet that results in a ~mostly~ correct solution. I say mostly because Ideally I would eliminate the hours for the first failure, but it's not the end of the world if they show up. Perhaps this could be solved by using EVALUATE and START AT

     

     

    Time Between Failures = 
    VAR SerialNumber = 'FAILURES'[SERIAL_NUM]
    VAR DT = 'FAILURES'[DATET]
    VAR LAST_HOURS = 'FAILURES'[HRS]
    
    RETURN
    LAST_HOURS - (CALCULATE(MAX('FAILURES'[HRS]), TOPN(1,FILTER('FAILURES', 'FAILURES'[SERIAL_NUM] = SerialNumber && 'FAILURES'[DATE] < DT), 'FAILURES'[DATE], DESC)))