Forum Discussion
Remove Future Dates from Rolling 12 Month Measures
- Anonymous5 years ago
My measure absolutely works the way you want. It's an adaptation of the technique from www.sqlbi.com by Alberto and Marco. It uses a technique known as "the interception of filters" to do the right thing. I have used it in many other projects and it's always worked correctly. I would really be surprised if it didn't do what it's supposed to. It surely does.
And, of course, it's not true what you say: "I dot not have that issue with the 27 until I use your measure." If you take a look at your very first post in this thread... you'll notice that, indeed, you also have a blank row paired with the numer 26,438. It means the blank row either does exist in your data (it might be a blank or an empty text ""), or the model is creating it due to RI problems. There is NO other possibility.
By the way, I'm talking about this measure (just to be sure we're talking about the same thing):
Rolling 12 Month External Hiring Sum = var VeryLastDateWithAnyHires = CALCULATE( MAX( 'Date'[Date] ), 'Date'[DatesWithHires], ALL( 'Date' ) ) var TotalPeriodWithHires = CALCULATETABLE( DISTINCT( 'Date'[Date] ), 'Date'[Date] <= VeryLastDateWithAnyHires ) var EffectiveDates = INTERSECT( TotalPeriodWithHires, DISTINCT( 'Date'[Date] ) ) var MaxEffectiveDate = MAXX( EffectiveDates, 'Date'[Date] ) var Result = CALCULATE( COUNTROWS( 'External Hires' ), DATESINPERIOD( 'Date'[Date], MaxEffectiveDate, -1, YEAR ), KEEPFILTERS( 'Date'[DatesWithHires] ) ) return Result
Rolling 12 Month External Hiring Sum =
CALCULATE(
COUNTROWS( 'External Hires' ),
DATESINPERIOD(
'Date'[Date],
MAX( 'Date'[Date] ),
-1,
YEAR
),
// DatesWithHires is a logical column
// so there's no need to equate it to
// TRUE.
KEEPFILTERS( 'Date'[DatesWithHires] )
)Thanks! That is super, super close. I am noticing future months and a count of 27 hires that are not attributed to a date. I can't quite figure out why.
This is what I get when using the DatesWithHires filter applied on the visual. This is what I would expect the measure to return.
- Anonymous5 years agoNot applicable
The fact that you see BLANK in the visual means that you've got referential integrity problems in your data. You should fix it. Secondly, it's pretty obvious why you've got numbers running all the way through all the months, even into the future. The fact that the measure filters by DatesWithHires does not mean that in the future the formula can't see any dates. In fact it does see dates in the past throughout all the year before the current MAX date. To do what you want, the formula must be different, of course. It must check if in the currently visible period there any dates where DatesWithHires is true and then calculate as above. If there are no such dates, BLANK should be returned. Can you program this yourself?
- Anonymous5 years agoNot applicable
Here's the measure:
Rolling 12 Month External Hiring Sum = var VeryLastDateWithAnyHires = CALCULATE( MAX( 'Date'[Date] ), 'Date'[DatesWithHires], ALL( 'Date' ) ) var TotalPeriodWithHires = CALCULATETABLE( DISTINCT( 'Date'[Date] ), 'Date'[Date] <= VeryLastDateWithAnyHires ) var EffectiveDates = INTERSECT( TotalPeriodWithHires, DISTINCT( 'Date'[Date] ) ) var MaxEffectiveDate = MAXX( EffectiveDates, 'Date'[Date] ) var Result = CALCULATE( COUNTROWS( 'External Hires' ), DATESINPERIOD( 'Date'[Date], MaxEffectiveDate, -1, YEAR ), KEEPFILTERS( 'Date'[DatesWithHires] ) ) return ResultHowever, whether it does what you want in the first year or not... depends on what your requirements are. For instance, is it correct to say that the 12-month sum is X if you only have 5 months of data? This happens in you calculate this measure in May of the first year in your Date table...
- CatManKuhn5 years ago
Helper II
Thanks daxer. I'm not sure what the referential integrity issue would be. I have checked the hires source and I do not have any null dates. All dates exist on the Date table, so I am unsure why this would return 27 rows without a date associated with it.
Here is a simple count of the external hires:
I want the Rolling 12 Month External Hires Sum measure to sum the last 12 months of hires. For example, 2020 row would should the past year of hires (1/1/2020 - 12/31/2020). For April 2021, this would calculate 4/8/2020 - 4/7/2021. I have successfully done so when my Date table has an end date equal to today, but trying to add future dates is making this challenging for me. I appreciate your help.