Forum Discussion

Ncf5031's avatar
Ncf5031
Icon for Advocate I rankAdvocate 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 types of equipment, it can be taken out of service when it fails, repaired, and then put back into service. What I am wanting to do is calculate the hours of service between failures. Equipment of type 2 typically will see multiple failures over it's servicable life, where equipment of type 1 typically DOES NOT see multiple failures, but has the potential to. I am most interested in capturing the life between failures in type 2 equipment. The failure data is housed in the Failures table which is a fact table and the inventory of equipment lives in the Inventory table (Dimension table). I have tried solving my issue using variables with no luck, though I am sure I am doing something incorrectly. I had also thought about assigning indexes to the hours column, one starting at 1 and one starting at 0, but the data is SQL server based and it's typically best practice at my company to do this in that environment, which isn't possible at this time. 

 

Below is a snap shot of a sample data model, and what data in the tables may look like.

 

 

 

 

 

Here is an example of what I would like the measure to output

 

 

Any help is greatly appreciated. 

  • 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)))

     

7 Replies

  • Ncf5031 , with help from a date table

     

    example

    Last Day Month Continuous = CALCULATE([sales],filter(ALL('Date'), eomonth('Date'[Date],0) = eomonth(MAXX(FILTER(ALLSELECTED('Date'),'Date'[Date]<max('Date'[Date])),'Date'[Date]),0)))

     

    Last Day Month Continuous = CALCULATE([sales],filter(ALL('Date'), eomonth('Date'[Date],0) = eomonth(Calculate(max(Table[Date]), FILTER(ALL('Date'),'Date'[Date]<max('Date'[Date]))),0)))

     

     

    if you want use same table then also add in filter

    && Table[SN] = max(Table[SN])

  • Anonymous's avatar
    Anonymous
    Not applicable

    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
      Icon for Advocate I rankAdvocate I

      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)))

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Ncf5031 ,

        If your problem has been resolved, could you please mark your post as Answered? It will help the others in the community find the solution easily if they face the same problem as yours. Thank you.

        Best Regards