Forum Discussion
Days between two dates and a date slicer
- Anonymous7 years ago
Hi Farmertree ,
Yes, it is possible. You can add variables to extract min and max date from selected calendar date and compare with start/end date to get datediff. After these, package them with a sumx function to summary datediff result.
Meausre = VAR selected = ALLSELECTED ( Calendar[Date] ) VAR _max = MAXX ( selected, [Date] ) VAR _min = MINX ( selected, [Date] ) RETURN SUMX ( ADDCOLUMNS ( ALLSELECTED ( Table ), "Diff", DATEDIFF ( MAX ( [Start date], _min ), MIN ( [End date], _max ), DAY ) ), [Diff] )Regards,
Xiaoxin Sheng
- 7 years ago
Anonymous Thanks! This helps a lot. The only problem is that this formula returns a negative DATEDIFF value for End dates < _min. Do you know how i could apply a filter within this formula that only selects rows in which Enddate >= _min?
Hi Farmertree ,
Yes, it is possible. You can add variables to extract min and max date from selected calendar date and compare with start/end date to get datediff. After these, package them with a sumx function to summary datediff result.
Meausre =
VAR selected =
ALLSELECTED ( Calendar[Date] )
VAR _max =
MAXX ( selected, [Date] )
VAR _min =
MINX ( selected, [Date] )
RETURN
SUMX (
ADDCOLUMNS (
ALLSELECTED ( Table ),
"Diff", DATEDIFF ( MAX ( [Start date], _min ), MIN ( [End date], _max ), DAY )
),
[Diff]
)
Regards,
Xiaoxin Sheng
Anonymous Thanks! This helps a lot. The only problem is that this formula returns a negative DATEDIFF value for End dates < _min. Do you know how i could apply a filter within this formula that only selects rows in which Enddate >= _min?
- Anonymous7 years agoNot applicable
HI Farmertree ,
You can do compare between date and _min to get the bigger one and use in datediff function:
Measure = VAR selected = ALLSELECTED ( Calendar[Date] ) VAR _max = MAXX ( selected, [Date] ) VAR _min = MINX ( selected, [Date] ) RETURN SUMX ( ADDCOLUMNS ( ALLSELECTED ( Table ), "Diff", DATEDIFF ( MAX ( [Start date], _min ), MIN ( MAX ( [End date], _min ), _max ), DAY ) ), [Diff] )Regards,
Xiaoxin Sheng