Forum Discussion
Calculated Measure and Averages
- 7 years ago
Hi Anonymous ,
At first, you need to create a calendar table.
Calendar = CALENDAR ( MIN ( 'Table'[Date of first Contract] ), TODAY () )
Then add three new columns in it.
Year = YEAR ( 'Calendar'[Date] )
StartDate = DATE ( 'Calendar'[Year], "01", "01" )
EndDate = IF ( 'Calendar'[Year] = MAX ( 'Calendar'[Year] ), MAX ( 'Calendar'[Date] ), DATE ( 'Calendar'[Year], "12", "31" ) )Now create two new measures.
CountPerYearPerEmployee = VAR selectDateS = SELECTEDVALUE ( 'Calendar'[StartDate] ) VAR selectDateE = SELECTEDVALUE ( 'Calendar'[EndDate] ) VAR selectYear = SELECTEDVALUE ( 'Calendar'[Year] ) VAR cease = SELECTEDVALUE ( 'Table'[Date of cease] ) VAR first = SELECTEDVALUE ( 'Table'[Date of first Contract] ) VAR se = IF ( YEAR ( first ) > selectYear, 0, IF ( YEAR ( first ) < selectYear, selectDateS, IF ( YEAR ( first ) = selectYear, first ) ) ) VAR de = IF ( se = 0, 0, IF ( cease = BLANK () && se <> 0, selectDateE, IF ( selectYear > YEAR ( cease ), 0, IF ( selectYear < YEAR ( cease ) && cease <> BLANK (), selectDateE, IF ( selectYear = YEAR ( cease ), cease ) ) ) ) ) RETURN ROUND ( IF ( de = 0, 0, DATEDIFF ( se, de, DAY ) ) / DATEDIFF ( selectDateS, selectDateE, DAY ), 2 )CountPerYear = SUMX ( VALUES ( 'Table'[Employee Code] ), [CountPerYearPerEmployee] )
Create visuals to show your result. You can use the CountPerYear to calculate the average.
Best Regards,
Eads
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi Anonymous ,
At first, you need to create a calendar table.
Calendar = CALENDAR ( MIN ( 'Table'[Date of first Contract] ), TODAY () )
Then add three new columns in it.
Year = YEAR ( 'Calendar'[Date] )
StartDate = DATE ( 'Calendar'[Year], "01", "01" )
EndDate =
IF (
'Calendar'[Year] = MAX ( 'Calendar'[Year] ),
MAX ( 'Calendar'[Date] ),
DATE ( 'Calendar'[Year], "12", "31" )
)
Now create two new measures.
CountPerYearPerEmployee =
VAR selectDateS =
SELECTEDVALUE ( 'Calendar'[StartDate] )
VAR selectDateE =
SELECTEDVALUE ( 'Calendar'[EndDate] )
VAR selectYear =
SELECTEDVALUE ( 'Calendar'[Year] )
VAR cease =
SELECTEDVALUE ( 'Table'[Date of cease] )
VAR first =
SELECTEDVALUE ( 'Table'[Date of first Contract] )
VAR se =
IF (
YEAR ( first ) > selectYear,
0,
IF (
YEAR ( first ) < selectYear,
selectDateS,
IF ( YEAR ( first ) = selectYear, first )
)
)
VAR de =
IF (
se = 0,
0,
IF (
cease = BLANK ()
&& se <> 0,
selectDateE,
IF (
selectYear > YEAR ( cease ),
0,
IF (
selectYear < YEAR ( cease )
&& cease <> BLANK (),
selectDateE,
IF ( selectYear = YEAR ( cease ), cease )
)
)
)
)
RETURN
ROUND (
IF ( de = 0, 0, DATEDIFF ( se, de, DAY ) )
/ DATEDIFF ( selectDateS, selectDateE, DAY ),
2
)
CountPerYear = SUMX ( VALUES ( 'Table'[Employee Code] ), [CountPerYearPerEmployee] )
Create visuals to show your result. You can use the CountPerYear to calculate the average.
Best Regards,
Eads
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi v-eachen-msft ,
That worked wonders! It's magnificent!
That said, I was wondering if it would be possible to bring it to the next level... Because the present year hasn't already ended, when compared with the results of previous years, it will always present lower values: the amount of employees hasn't changed that much, but I'm comparing absences committed during 12 month against 8 months.
Could be possible adding to those measures, a last 12 months average ("N-12") comparable to previous years?
That would actually give way more information about how things are evolving...
Any further help wold be much appreciated!
Anyways, thank you a lot for what you have already done!