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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors