Hi @mkaess,
So a couple of observations, your measure:
Object Days On-Rent =
VAR OnRentDate =
CALCULATE (
VALUE ( SELECTEDVALUE ( 'Rental Contract Line'[On-Rent Date] ) ),
CROSSFILTER( Dates[Date], 'Rental Contract Line'[On-Rent Date], Both )
)
VAR ExpiryDate =
CALCULATE (
VALUE ( SELECTEDVALUE ( 'Rental Contract Line'[Expiration Date] ) ),
CROSSFILTER( Dates[Date], 'Rental Contract Line'[Expiration Date], Both )
)
VAR MinDateInContext =
VALUE ( MIN ( Dates[Date] ) )
VAR MaxDateInContext =
VALUE ( MAX ( Dates[Date] ) )
RETURN
IF (
AND ( OnRentDate < MinDateInContext, ExpiryDate > MaxDateInContext ),
MIN( ExpiryDate, MaxDateInContext ) - MIN( OnRentDate, MinDateInContext ),
IF (
AND ( OnRentDate > MinDateInContext, ExpiryDate < MaxDateInContext ),
ExpiryDate - MinDateInContext,
BLANK()
)
)
.
Could also be writen as:
Object Days On-Rent SELECTEDVALUE =
VAR OnRentDate = SELECTEDVALUE ( 'Rental Contract Line'[On-Rent Date] )
VAR ExpiryDate = SELECTEDVALUE ( 'Rental Contract Line'[Expiration Date] )
VAR MinDateInContext = MIN ( Dates[Date] )
VAR MaxDateInContext = MAX ( Dates[Date] )
RETURN
IF (
AND ( OnRentDate < MinDateInContext, ExpiryDate > MaxDateInContext ),
MIN( ExpiryDate, MaxDateInContext ) - MIN( OnRentDate, MinDateInContext ),
IF (
AND ( OnRentDate > MinDateInContext, ExpiryDate < MaxDateInContext ),
ExpiryDate - MinDateInContext,
BLANK()
)
)
That produces the same result of course…
.
To bring in the context from the Object No try iterating over a virtual table, first attempt.
Object Days On-Rent v2 =
VAR MinDateInContext = MIN ( Dates[Date] )
VAR MaxDateInContext = MAX ( Dates[Date] )
VAR vTable =
FILTER(
ADDCOLUMNS( 'Rental Contract Line',
"@DaysOnRent",
IF (
AND ( [On-Rent Date] < MinDateInContext, [Expiration Date] > MaxDateInContext ),
MIN( [Expiration Date], MaxDateInContext ) - MIN( [On-Rent Date], MinDateInContext ),
IF (
AND ( [On-Rent Date] > MinDateInContext, [Expiration Date] < MaxDateInContext ),
VALUE( [Expiration Date] ) - VALUE( MinDateInContext ),
BLANK()
)
)
), [@DaysOnRent] <> BLANK()
)
RETURN
SUMX( vTable, [@DaysOnRent] )
However these results don’t match yours…
Investigated a subset a little closer, see the marked values above and below …
Looks like the only difference is that the [Expiration Date] is equal to the last date in your Date table…
To check if that is the case for all results, amended the measure.
Object Days On-Rent v3 =
VAR MinDateInContext = MIN ( Dates[Date] )
VAR MaxDateInContext = MAX ( Dates[Date] )
VAR LastCalendarDate = CALCULATE( MAX( Dates[Date] ), ALL(Dates[Date] ))
VAR vTable =
FILTER(
ADDCOLUMNS( 'Rental Contract Line',
"@DaysOnRent",
IF (
AND ( [On-Rent Date] < MinDateInContext, [Expiration Date] > MaxDateInContext ),
MIN( [Expiration Date], MaxDateInContext ) - MIN( [On-Rent Date], MinDateInContext ),
IF (
AND ( [On-Rent Date] > MinDateInContext, [Expiration Date] < MaxDateInContext ),
VALUE( [Expiration Date] ) - VALUE( MinDateInContext ),
BLANK()
)
)
), [@DaysOnRent] <> BLANK()
&& [Expiration Date] = LastCalendarDate
)
RETURN
SUMX( vTable, [@DaysOnRent] )
.
and this matches your results. The question now becomes, is that correct??
I hope this is helpful.