Forum Discussion
faaz
7 months agoFrequent Visitor
Rolling 12M Average measure
Hi all. I’m having some issues with this measure. In its current form, it returns a value for every date (note that the value is the same across the entire month). The data works as follows: T...
- 7 months ago
Thank you all for your replies. I was playing around and found this to give me what I needed:
_HC = VAR _MaxCalendarIDInData = CALCULATE( MAX('Employee headcount'[CalendarID]), ALL('Employee headcount') ) // Convert CalendarID back to Date for calculations VAR _MaxDateInData = CALCULATE( MAX('Calendar'[Date]), FILTER(ALL('Calendar'), 'Calendar'[CalendarID] = _MaxCalendarIDInData) ) // Detect Calendar filtering VAR _IsCalendarFiltered = COUNTROWS(ALLSELECTED('Calendar')) < COUNTROWS(ALL('Calendar')) // Raw max date from selection (may be in the future) VAR _SelectedMaxDate = IF( _IsCalendarFiltered, MAX('Calendar'[Date]), _MaxDateInData ) // HARD CAP: never go beyond last date in Employee headcount VAR _MaxDate = MIN(_SelectedMaxDate, _MaxDateInData) // For rolling 12-month window VAR _MaxDateProjectedToMonthEnd = EOMONTH(_MaxDate, 0) VAR _MinMonthFor12Months = EDATE(_MaxDateProjectedToMonthEnd, -11) // Get distinct dates that exist in Employee headcount table within 12-month window VAR _AvailableDates = DISTINCT( SELECTCOLUMNS( FILTER( 'Employee headcount', RELATED('Calendar'[Date]) >= _MinMonthFor12Months && RELATED('Calendar'[Date]) <= _MaxDate && 'Employee headcount'[EmploymentIsTerminatedIn1/0] = 0 ), "Date", RELATED('Calendar'[Date]) ) ) // Build month list from available dates only (last 12 months) VAR _MonthStarts = DISTINCT( SELECTCOLUMNS( _AvailableDates, "MonthStart", DATE( YEAR([Date]), MONTH([Date]), 1 ) ) ) VAR _AvgHC = AVERAGEX( _MonthStarts, VAR _MonthStart = [MonthStart] VAR _MonthEnd = EOMONTH(_MonthStart, 0) RETURN CALCULATE( SUM('Employee headcount'[Count]), 'Employee headcount'[EmploymentIsActiveIn1/0] = 1, 'Employee headcount'[EmploymentIsTerminatedIn1/0] = 0, 'Employee type'[Employee type name] IN { "Apprentice", "Blue-collar Employee", "Expatriate", "Indirect Blue-collar Employee", "Trainee", "White-collar Employee" }, NOT 'Employee status'[Employee status name] IN { "Reported No Show", "Retired", "Terminated" }, 'Calendar'[Date] >= _MonthStart, 'Calendar'[Date] <= _MonthEnd ) ) RETURN IF(_AvgHC >= 4, _AvgHC, BLANK())
faaz
7 months agoFrequent Visitor
Thank you all for your replies. I was playing around and found this to give me what I needed:
_HC =
VAR _MaxCalendarIDInData =
CALCULATE(
MAX('Employee headcount'[CalendarID]),
ALL('Employee headcount')
)
// Convert CalendarID back to Date for calculations
VAR _MaxDateInData =
CALCULATE(
MAX('Calendar'[Date]),
FILTER(ALL('Calendar'), 'Calendar'[CalendarID] = _MaxCalendarIDInData)
)
// Detect Calendar filtering
VAR _IsCalendarFiltered =
COUNTROWS(ALLSELECTED('Calendar')) < COUNTROWS(ALL('Calendar'))
// Raw max date from selection (may be in the future)
VAR _SelectedMaxDate =
IF(
_IsCalendarFiltered,
MAX('Calendar'[Date]),
_MaxDateInData
)
// HARD CAP: never go beyond last date in Employee headcount
VAR _MaxDate =
MIN(_SelectedMaxDate, _MaxDateInData)
// For rolling 12-month window
VAR _MaxDateProjectedToMonthEnd = EOMONTH(_MaxDate, 0)
VAR _MinMonthFor12Months = EDATE(_MaxDateProjectedToMonthEnd, -11)
// Get distinct dates that exist in Employee headcount table within 12-month window
VAR _AvailableDates =
DISTINCT(
SELECTCOLUMNS(
FILTER(
'Employee headcount',
RELATED('Calendar'[Date]) >= _MinMonthFor12Months &&
RELATED('Calendar'[Date]) <= _MaxDate &&
'Employee headcount'[EmploymentIsTerminatedIn1/0] = 0
),
"Date", RELATED('Calendar'[Date])
)
)
// Build month list from available dates only (last 12 months)
VAR _MonthStarts =
DISTINCT(
SELECTCOLUMNS(
_AvailableDates,
"MonthStart",
DATE(
YEAR([Date]),
MONTH([Date]),
1
)
)
)
VAR _AvgHC =
AVERAGEX(
_MonthStarts,
VAR _MonthStart = [MonthStart]
VAR _MonthEnd = EOMONTH(_MonthStart, 0)
RETURN
CALCULATE(
SUM('Employee headcount'[Count]),
'Employee headcount'[EmploymentIsActiveIn1/0] = 1,
'Employee headcount'[EmploymentIsTerminatedIn1/0] = 0,
'Employee type'[Employee type name] IN
{
"Apprentice",
"Blue-collar Employee",
"Expatriate",
"Indirect Blue-collar Employee",
"Trainee",
"White-collar Employee"
},
NOT 'Employee status'[Employee status name] IN {
"Reported No Show",
"Retired",
"Terminated"
},
'Calendar'[Date] >= _MonthStart,
'Calendar'[Date] <= _MonthEnd
)
)
RETURN
IF(_AvgHC >= 4, _AvgHC, BLANK())