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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
CeeDee
New Member

Continuous Headcount by Hire and Termination Date

Hello,

 

 

I've been racking my brains trying to get dynamic headcount by day that takes into account their hire and termination dates and only counts if they are between them. I'm either lookin for day by day or a monthly one (any hire or termination within that month counts as an headcount for the end of month total). 

 

I've been able to get a static end of month one but not a dynamic one in the past. It just tends to show the Delta of hire to terminated for each month. 

1 REPLY 1
Mrxiang
Helper II
Helper II

To get a dynamic headcount by day that takes into account hire and termination dates, you can use a combination of SQL queries and date functions. Here's an example query that calculates the headcount for each day:


SELECT 
  date,
  COUNT(*) AS headcount
FROM (
  SELECT 
    hire_date AS date
  FROM employees
  UNION ALL
  SELECT 
    termination_date AS date
  FROM employees
  WHERE termination_date IS NOT NULL
) AS dates
WHERE date BETWEEN '2021-01-01' AND '2021-12-31'
GROUP BY date
This query first selects all the hire dates and termination dates from the employees table, and combines them into a single list using the UNION ALL operator. It then filters the list to only include dates within the desired range (in this case, all of 2021), and groups the dates by day using the GROUP BY clause. Finally, it counts the number of rows in each group to get the headcount for that day.

To get a monthly headcount, you can modify the query to group by month instead of day:


SELECT 
  DATE_TRUNC('month', date) AS month,
  COUNT(*) AS headcount
FROM (
  SELECT 
    hire_date AS date
  FROM employees
  UNION ALL
  SELECT 
    termination_date AS date
  FROM employees
  WHERE termination_date IS NOT NULL
) AS dates
WHERE date BETWEEN '2021-01-01' AND '2021-12-31'
GROUP BY month
This query uses the DATE_TRUNC function to truncate each date to the beginning of the month, and groups the truncated dates using the GROUP BY clause. The rest of the query is the same as the previous one.

Note that this query assumes that the hire and termination dates are stored in separate columns in the employees table. If they are stored in a single column (e.g. as a "status" field that changes between "hired" and "terminated"), you will need to modify the query accordingly.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors