Forum Discussion
Calculating Days Based On Dates & Criteria
- 5 years ago
SamFletch
Add the following code as a new column to your table. Check with other scenarios and reply with your findings.Days Active This Month = var _thismonth = (EOMONTH(TODAY(),0)) var _active = T3[Is Active] = "Yes" return IF( _active, Min(MIN( _thismonth - T3[Activate Date] + 1 , _thismonth ), DAY(_thismonth)), Max(0,T3[Deactivate Date] - (_thismonth - DAY(_thismonth)) ) )
SamFletch
Add the following code as a new column to your table. Check with other scenarios and reply with your findings.
Days Active This Month =
var _thismonth = (EOMONTH(TODAY(),0))
var _active = T3[Is Active] = "Yes"
return
IF(
_active,
Min(MIN( _thismonth - T3[Activate Date] + 1 , _thismonth ), DAY(_thismonth)),
Max(0,T3[Deactivate Date] - (_thismonth - DAY(_thismonth)) )
)
- SamFletch5 years ago
Helper I
Wow this worked and you managed to do it in less than 10 lines of code!
Thank you so much, I'm very impresed and incredibly grateful! Would you mind explaining a little how it works? I understand the creation of the variables and honestly should have thought about doing that myself and I didn't realise EOMONTH was a thing, I'm just struggling to understand how the MIN and MAX lines are working.
- Fowmy5 years ago
Super User
SamFletch
I noted that in my formula, I did not account for Activation dates within the month for Status NO, try this code instead of the above.Days Active This Month = var _thismonth = (EOMONTH(TODAY(),0)) var _active = T3[Is Active] = "Yes" return IF( _active, MIN( _thismonth - T3[Activate Date] + 1 , DAY(_thismonth) ), IF( ISBLANK( T3[Deactivate Date] ), 0 , COUNTROWS( INTERSECT( CALENDAR(EOMONTH(_thismonth,-1),_thismonth), CALENDAR(T3[Activate Date],T3[Deactivate Date]) ) ) ) )