Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

Counting number of days employee was active during a period

I am trying to count the number of employees that were active during a period. I define a person as active in the year if they were around for at least 50% of the days, based purely on their start and end dates. For example, if a person started 2/1/2018 and left 11/1/2018, I would still like to count him in our 2018 tally because he was around for 274 out of the 365 days. 

 

In general, the formula would be:

Count Employee if (Days Active in Period)/(Days in Period) >= 50%

Days Active in Period = MIN(Period End, End Date) - MAX(Period Start, Start Date)

Days in Period = Period End - Period Start

 

I am having trouble wrapping my head around how to do the 'Days Active in Period' piece in Power BI.

 

I have done the calculations using the logic above in Excel:

 

 Period StartPeriod End Count threshold 
 1/1/201812/31/2018 50%  
       
 Start DateEnd DateDays active in PeriodDays in Period% Active in PeriodCount Employee?
Bob2/1/201811/1/201827436575%Y
John6/1/201811/1/201815436542%N
Steve6/1/2018 21436559%Y
Joe6/1/201711/1/201830536584%Y
Alice10/1/2018 9236525%N
Nancy3/1/2018 30636584%Y
Kate11/1/2018 6136517%N

 

 

Thank you in advance for your help once more ... slowly getting my handle on this 🙂 

 

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Figured it out thanks to a previous solution from @v-shex-msft 

 

Days Active in Period = 
var period_start = FIRSTDATE(ALLSELECTED('Calendar'[Date]))
var period_end = LASTDATE(ALLSELECTED('Calendar'[Date]))
var Start_Date = max(Employees[Start Date])
var End_Date = max(Employees[End Date])
return
DATEDIFF(MAX(period_start, Start_Date), if(End_Date=blank(), period_end, min(End_Date, period_end)), DAY)

 

View solution in original post

@Anonymous ,

 

In a measure, you need to use aggregation function to achieve the result(not like calculate column), var Start_Date = max(Employees[Start Date]) and var End_Date = max(Employees[End Date]) just means to save current row value to the variable. Every time a new row has been iterated, a new row value will be saved to the variable.

 

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Figured it out thanks to a previous solution from @v-shex-msft 

 

Days Active in Period = 
var period_start = FIRSTDATE(ALLSELECTED('Calendar'[Date]))
var period_end = LASTDATE(ALLSELECTED('Calendar'[Date]))
var Start_Date = max(Employees[Start Date])
var End_Date = max(Employees[End Date])
return
DATEDIFF(MAX(period_start, Start_Date), if(End_Date=blank(), period_end, min(End_Date, period_end)), DAY)

 

Anonymous
Not applicable

Can anyone explain how this code works? Especially the four 'var' lines. 

I am especially confused on how the followings lines of code works:

var Start_Date = max(Employees[Start Date])
var End_Date = max(Employees[End Date])

Thank you! 

@Anonymous ,

 

In a measure, you need to use aggregation function to achieve the result(not like calculate column), var Start_Date = max(Employees[Start Date]) and var End_Date = max(Employees[End Date]) just means to save current row value to the variable. Every time a new row has been iterated, a new row value will be saved to the variable.

 

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.