Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
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.
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.
User | Count |
---|---|
20 | |
14 | |
11 | |
8 | |
6 |
User | Count |
---|---|
23 | |
23 | |
20 | |
15 | |
10 |