Forum Discussion
First date attribute seen within sequence
- 4 years ago
Earliest Current Condition Date =
var currentAsset = SELECTEDVALUE('Table'[AssetID])
var currentCondition = SELECTCOLUMNS( CALCULATETABLE( TOPN( 1,'Table', 'Table'[Date], DESC),'Table'[AssetID] = currentAsset),
"@condition", 'Table'[Condition] )
var prevConditionDate = CALCULATE( MAX('Table'[Date]), REMOVEFILTERS('Table'), 'Table'[AssetID] = currentAsset && 'Table'[Condition] <> currentCondition )
var result = CALCULATE( MIN('Table'[Date]),REMOVEFILTERS('Table'),
'Table'[AssetID] = currentAsset && 'Table'[Condition] = currentCondition && 'Table'[Date] > prevConditionDate )
return result
Hi,
Thanks for your repsonse. I'm getting a circular dependency when I add the table name and paste that DAX
<pi>A circular dependency was detected: Sheet2[Earliest Current Condition Date], 1e517ef3-3123-4a08-a46c-73704fa051fc, Sheet2[Earliest Current Condition Date].</pi>
How should I be using the code and any ideas what the issue is? Once I've got the first date i've got a few other steps so ideally i'd do this in Power Query if its possible?
Its meant to be a measure, not a column. If you create it as a measure you should be able to add it to a table visual along with the asset ID column and it will show the current values for each asset.
If you needed to refer to it in other calculations then you could either calculate it on the fly or create a calculated table storing the asset ID and the current earliest condition date using the below code
Summary Table = SUMMARIZECOLUMNS( 'Sheet2'[AssetID], "Earliest Date", [Earliest Current Condition Date])
- Anonymous4 years agoNot applicable
Is it possible to do this type of thing in Power Query? Once I've determined the first date I then have a workflow which combines the asset data with deteriortion and costing model data. It merges and pivots the data around to work out how long the asset has left and how much its going to cost. Thats mostly created in Power Query
- johnt754 years agoSuper User
You could probably do a bunch of transformations and turn it into a custom function, but my Power Query isn't good enough to write the code.
The basic principle is to find the max date where the condition is different to the current value, then find the min date which is greater than that and the condition is the same as the current value.
- Anonymous4 years agoNot applicable
Thank you. With your explanation and working through the logic of the DAX you provided i've replicated the steps in Power Query.
- Group by Asset ID to give Max Date per asset
- Using Max Date and Asset ID determine latest condition
- Merge this back to table on Asset ID and Date
- If statement to determine which dates are, and which are not the current condition
- Filter down to those that are not the current condition
- Group by Asset ID to give Max non current date per asset
- Merge this back to table on Asset ID
- If statement to determine which dates are greater than Max non current date
- Filter to just those that are greater
- Group by Asset ID to give minimum date per asset