Forum Discussion
FTE calculation using working days for different time horizons
- 5 years ago
Anonymous ,
Here's a version that does not use CALCULATE and should be MUCH faster. Have a good look at its mechanics and if it does not work on the first attempt, do not panic, just try to understand how it works and adjust it accordingly. But beware of CALCULATE that's executed row by row on a fact table. YOU SHOULD NEVER DO IT AS IT'LL ALWAYS KILL PERFORMANCE.
Total FTEs = // Dates must NOT be connected to Facts // and must be marked as a Date table VAR __firstDate = MIN( 'Dates'[Date] ) VAR _lastDate = MAX( 'Dates'[Date] ) // assuming that WorkingDay is 1 for // a working day and 0 for a weekend var __workingDayCount = SUM( 'Dates'[WorkingDay] ) RETURN if( __workingDayCount > 0, SUMX( FILTER( FACTS, // getting only the rows where // there is a non-empty overlap // between (start, end) and // (firstDate, lastDate) Facts[Start date] <= __lastDate && Facts[Finish date] >= __firstDate ), // for each of the above rows calculate // the percentage of FTE var __fte = FACTS[Result gross (FTE)] var __lowerDate = MAX( __firstDate, Facts[Start date] ) var __upperDate = MIN( __lastDate, Facts[Finish date] ) var __activityWorkingDayCount = SUMX( filter( // We don't have to use // ALL( Dates ) here due // to the nature of the // problem. Dates, and( __lowerDate <= Dates[Date], Dates[Date] <= __upperDate ) ), Dates[WorkingDay] ) return // do not use DIVIDE here as it // does nothing more than what // I've put in here and in fact // it slows down calculations __fte * __activityWorkingDayCount / __workingDayCount ) )
hi Anonymous
For your case, you need to create a measure as below:
Total Amount =
VAR tmpCalendar = ADDCOLUMNS('Date',"MonthYear",VALUE(YEAR([Date]) & FORMAT(MONTH([Date]),"0#")))
VAR tmpBilling = ADDCOLUMNS('Table',"MonthYearBegin",VALUE(YEAR([Start Date]) & FORMAT(MONTH([Start Date]),"0#")),
"MonthYearEnd",VALUE(YEAR([End Date]) & FORMAT(MONTH([End Date]),"0#")))
VAR tmpTable =
ADDCOLUMNS(
SELECTCOLUMNS(
FILTER(
GENERATE(
tmpBilling,
SUMMARIZE(tmpCalendar,[Year],[Month],[MonthYear],"Totalworkingdays",COUNTROWS(FILTER('Date','Date'[Weekend]=FALSE()&&'Date'[Year]=[Year]&&'Date'[Month]=[Month])))
),
[MonthYear] >= [MonthYearBegin] &&
[MonthYear] <= [MonthYearEnd]
),
"Activity",[Activity],
"Year",[Year],
"Month",[Month],
"FTEs",[FTEs],
"Totalworkingdays",[Totalworkingdays],
"Start",IF(DATE([Year],[Month],1)<[Start Date],[Start Date],DATE([Year],[Month],1)),
"End",IF(DATE([Year],[Month]+1,1)-1>[End Date],[End Date],DATE([Year],[Month]+1,1)-1)
),
"Rate",DIVIDE(COUNTROWS(FILTER('Date','Date'[Weekend]=FALSE()&&'Date'[Year]=[Year]&&'Date'[Month]=[Month]&&'Date'[Date]>=[Start]&&'Date'[Date]<=[End])),[Totalworkingdays]))
return
SUMX(tmpTable,[FTEs]*[Rate])
Result:
and here is a similar post for you refer to:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Billing/m-p/409365
Regards,
Lin
- Anonymous5 years agoNot applicable
Hello v-lili6-msft ,
Thanks a lot for your input, quite interesting approach.
Although it worked, I faced some performance issues with the measure. I came up with a different one - that does also have performance issues - but the totals seems ok here. As it is a different aim (only improving the performance), I have posted it into a different link.
Anyway, here is the measure and the expected results (also present in the other link):
Total FTEs = VAR _FirstDate = FIRSTDATE ( 'DateC'[Date] ) VAR _LastDate = LASTDATE ( 'DateC'[Date] ) RETURN SUMX(FACTS, CALCULATE ( SUM ( FACTS[Result gross (FTE)] ) * DIVIDE ( CALCULATE ( SUM ( 'DateC'[WorkingDay] ), DATESBETWEEN ( 'DateC'[Date], MAx(_FirstDate,SUM(FACTS[Start date])), MIN(_LastDate,SUM(FACTS[Finish date]) )) ), Calculate( SUM ( 'DateC'[WorkingDay] ), DATESBETWEEN ( 'DateC'[Date], _FirstDate, _LastDate ) ), 0 ), FACTS[Start date] <= _LastDate, FACTS[Finish date] >= _FirstDate ))Thanks a lot for your help,
Pedro