cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors