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
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
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.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.