Forum Discussion
Variance between rows if multiple entries exist in specified timeframe
- Anonymous6 years ago
Hi greenawayr,
In fact, I already filter on the 'parent' field to find out the minimum date based on its 'id' and stored into a variable.
After these steps, you can use current id and the first date to lookup the correspond score and calculate with the current score to get the diff.Diff = VAR currScore = MAX ( LIVE[Score] ) VAR currID = MAX ( LIVE[ID] ) VAR currDate = MAX ( LIVE[Modified] ) VAR prevDate = CALCULATE ( MIN ( Staging[Modified] ), FILTER ( ALLSELECTED ( Staging ), [ParentID] = currID && [Modified] <= currDate - 30 ) ) VAR prevScore = LOOKUPVALUE ( Staging[Score], Staging[ParentID], currID, Staging[Modified], prevDate ) RETURN IF ( prevDate <> BLANK (), currScore - prevScore, 0 )BTW, My formula is a measure, please not use it in a calculated column.
Regards,
Xiaoxin Sheng
HI greenawayr ,
I'd like to suggest you use parent key id to lookup related detail records, then you can do variance calculation by VARX.P VARX.S functions. BTW, can you please share the parent table structure to help us clarify your requirement for test?
How to Get Your Question Answered Quickly
Regards,
Xiaoxin Sheng
Thanks,
So I'm dealing with a "LIVE" table and a "Staging" table. So the schemas are much the same, but the live table only holds one row per record and the staging table holds a record for each modifcation made to the parent record.
So LIVE
| ID | MODIFIED | Score |
| 1 | 01/22/2020 | 15 |
| 2 | 01/22/2020 | 8 |
| 3 | 01/20/2020 | 12 |
and Staging
| RowID | ParentID | Modified | Score |
| 6 | 2 | 01/22/2020 | 8 |
| 5 | 1 | 01/22/2020 | 15 |
| 4 | 3 | 01/20/2020 | 12 |
| 3 | 2 | 01/10/2020 | 12 |
| 2 | 2 | 12/20/2019 | 11 |
| 1 | 1 | 12/05/2019 | 13 |
So for the LIVE table, ID 2, it's current score is 8, but 30 days ago it was 11 (we ignore the score change that made it 12, we just want the score as it was 30 days ago) and the variance is -3.
Hope this helps. Thanks
- Anonymous6 years agoNot applicable
Hi greenawayr,
You can try to use the following measure formula to get the diff between the current 'live' score and filtered first 'staging' score based on 'ID' group:
Diff = VAR currScore = MAX ( LIVE[Score] ) VAR currID = MAX ( LIVE[ID] ) VAR currDate = MAX ( LIVE[Modified] ) VAR prevDate = CALCULATE ( MIN ( Staging[Modified] ), FILTER ( ALLSELECTED ( Staging ), [ParentID] = currID ) ) VAR prevScore = LOOKUPVALUE ( Staging[Score], Staging[ParentID], currID, Staging[Modified], prevDate ) RETURN currScore - prevScoreRegards,
Xiaoxin Sheng
- greenawayr6 years agoHelper I
Sorry, I'm not having much luck with this, I may not be explaining it properly.
My Staging table could contain multiple rows child rows and therefore I need a way to return the current score, only for the first row that matches my Parent ID, is older than 30 days and sorted in ascending order.
I've created a column that calculates the number of days between todays date and the modified date, and gives me an integer, so in theory I'm looking for the score where ID = Parent and && CalculatedModifiedDate > 30. However, because this returns more than one value, I'm getting errors around returning multiple results in a scalar value. I've TopN, Min etc, but keep seeming to get the same error.
How do I get only one result returned so I can store that in a column and subtract it from my LIVE score?
Thanks for your help so far.
- Anonymous6 years agoNot applicable
Hi greenawayr,
In fact, I already filter on the 'parent' field to find out the minimum date based on its 'id' and stored into a variable.
After these steps, you can use current id and the first date to lookup the correspond score and calculate with the current score to get the diff.Diff = VAR currScore = MAX ( LIVE[Score] ) VAR currID = MAX ( LIVE[ID] ) VAR currDate = MAX ( LIVE[Modified] ) VAR prevDate = CALCULATE ( MIN ( Staging[Modified] ), FILTER ( ALLSELECTED ( Staging ), [ParentID] = currID && [Modified] <= currDate - 30 ) ) VAR prevScore = LOOKUPVALUE ( Staging[Score], Staging[ParentID], currID, Staging[Modified], prevDate ) RETURN IF ( prevDate <> BLANK (), currScore - prevScore, 0 )BTW, My formula is a measure, please not use it in a calculated column.
Regards,
Xiaoxin Sheng