Forum Discussion
Use value from other row in dataset based on condition
I am trying to do some advanced DAX/PowerQuery equations and I am coming up short and looking for some help.
I have a dataset like the below and the use case is to get an average amount of time spent for each 'code'. To come up with that I need to take each true value timestamp and comare it against the next false vale timestamp. Ideally I'd like to store the DATEDIFF in a new column on each TRUE value. Does anyone know how I can achieve this? Keep in mind, each equipment ID can have many true/false values.
Thank you in advance!
| equipID | down | code | timestamp |
| 25DC-001 | FALSE | 1/5/2018 8:00AM | |
| 25DC-001 | TRUE | DN03 | 1/4/2018 8:00AM |
| 25DC-001 | TRUE | DN02 | 1/3/2018 8:00AM |
| 25DC-001 | TRUE | DN01 | 1/2/2018 8:00AM |
| 25DC-001 | FALSE | 1/1/2018 8:00AM | |
| 25DC-002 | FALSE | 1/6/2018 8:00AM | |
| 25DC-002 | TRUE | DN01 | 1/3/2018 8:00AM |
| 25DC-002 | FALSE | 1/1/2018 8:00AM |
Example: Output
| equipID | down | code | timestamp | dateDiff |
| 25DC-001 | FALSE | 1/5/2018 8:00AM | ||
| 25DC-001 | TRUE | DN03 | 1/4/2018 8:00AM | 1440 |
| 25DC-001 | TRUE | DN02 | 1/3/2018 8:00AM | 2880 |
| 25DC-001 | TRUE | DN01 | 1/2/2018 8:00AM | 4320 |
| 25DC-001 | FALSE | 1/1/2018 8:00AM | ||
| 25DC-002 | FALSE | 1/6/2018 8:00AM | ||
| 25DC-002 | TRUE | DN01 | 1/3/2018 8:00AM | 4320 |
| 25DC-002 | FALSE | 1/1/2018 8:00AM |
Hi thmonte
Sorry, I missed that. I have ammeded my calc
Column = VAR x = MAXX( FILTER( 'Table1', 'Table1'[equipID] = EARLIER('Table1'[equipID]) && 'Table1'[timestamp] > EARLIER('Table1'[timestamp]) && 'Table1'[down] = FALSE() ), 'Table1'[timestamp]) VAR y = if(ISBLANK(x),NOW(),x) RETURN IF( 'Table1'[code]<>"", DATEDIFF('Table1'[timestamp],y,MINUTE))
11 Replies
- Phil_Seamark
Microsoft Employee
Hi thmonte
Any chance you can post the ideal output for that sample of data? It might help answer a few questions
- thmonte
Helper IV
Sure Phil_Seamark
For each True do DATEDIFF to the NEXT False timestamp in group, if no false found use NOW().
DN03 = DATEDIFF(1/4/2018 8:00AM,1/5/2018 8:00AM,MINUTES)
Example: Output
equipID down code timestamp dateDiff 25DC-001 FALSE 1/5/2018 8:00AM 25DC-001 TRUE DN03 1/4/2018 8:00AM 1440 25DC-001 TRUE DN02 1/3/2018 8:00AM 2880 25DC-001 TRUE DN01 1/2/2018 8:00AM 4320 25DC-001 FALSE 1/1/2018 8:00AM 25DC-002 FALSE 1/6/2018 8:00AM 25DC-002 TRUE DN01 1/3/2018 8:00AM 4320 25DC-002 FALSE 1/1/2018 8:00AM - Phil_Seamark
Microsoft Employee
Hi thmonte
Please try this calculated column
Column = VAR x = MAXX( FILTER( 'Table1', 'Table1'[equipID] = EARLIER('Table1'[equipID]) && 'Table1'[timestamp] > EARLIER('Table1'[timestamp]) && 'Table1'[down] = FALSE() ), 'Table1'[timestamp]) RETURN IF( 'Table1'[code]<>"", DATEDIFF('Table1'[timestamp],x,MINUTE))