[英]Doing a distinct count on an employee history table, based on departments at a current point in time
So I have an employee table with data on all employee since the beginning.所以我有一个员工表,其中包含从一开始就所有员工的数据。 In the data I have all the data I should need.
在数据中,我拥有我需要的所有数据。 I have the employee startdate, enddate (null if nothing), I have the name of the department, and if a department has changed, that specific employee has a new line, with a new department value, and two columns called "DepValidFrom" and "DepValidto", in date format that determines the time-period that the current employee was in that specific department.
我有员工的开始日期、结束日期(如果没有则为 null),我有部门的名称,如果部门发生变化,该特定员工有一个新行,有一个新的部门值,以及两列名为“DepValidFrom”和“DepValidto”,采用日期格式,确定当前员工在该特定部门的时间段。
My goal is, to get into a matrix, a list of all the departments as rows, and with year and month as columns, and the number of employees in that department at that time as values.我的目标是,进入一个矩阵,所有部门的列表作为行,年和月作为列,以及当时该部门的员工人数作为值。 I have all the data, I just cannot find the exact way to write my PowerBI Measure or perhaps even SQL query.
我拥有所有数据,只是找不到编写 PowerBI Measure 或什至 SQL 查询的确切方法。
So.... I am trying to pull this into Power BI, and I am getting an incomplete view.所以....我正在尝试将其放入 Power BI,但我得到的是一个不完整的视图。 I want my data to look like the following:
我希望我的数据如下所示:
Department | Jan | Feb | Mar | Apr |
Dep1 | 3 | 5 | 6 | 4 |
Dep2 | 2 | 3 | 2 | 3 |
Dep3 | 1 | 1 | 2 | 3 |
Right now I am just using a very simple DISTINCTCOUNT(Emp_Table[EmployeeInitials])
which gives me an incomplete view, as it only counts on the specific date, and doesn't retain the number into a total, leaving a bunch of empty values.现在我只是使用一个非常简单的
DISTINCTCOUNT(Emp_Table[EmployeeInitials])
,它给了我一个不完整的视图,因为它只计算特定日期,并且不将数字保留为总数,留下一堆空值。
I hope someone can understand what I mean, and that someone can help!我希望有人能理解我的意思,并且有人可以提供帮助!
Thanks!谢谢!
You can start by unpivoting the dates and generating a query that gives the number of employee per department and date:您可以首先逆透视日期并生成一个查询,该查询给出每个部门和日期的员工人数:
select e.dept, x.dt, sum(cnt) over(partition by dept order by dt) cnt
from employees e
cross apply (values (startdate, 1), (enddate, -1)) as x(dt, cnt)
where dt is not null
Then, you can do conditional aggregation to pivot the results - this requires enumerating the dates though:然后,您可以对结果进行条件聚合 pivot - 这需要枚举日期:
select dept,
max(case when dt >= '20200101' and dt < '20200201' then cnt else 0 end) cnt_202001,
max(case when dt >= '20200201' and dt < '20200301' then cnt else 0 end) cnt_202002,
...
from (
select e.dept, x.dt, sum(cnt) over(partition by dept order by dt) cnt
from employees e
cross apply (values (startdate, 1), (enddate, -1)) as x(dt, cnt)
where dt is not null
) t
group by dept
When an employee changes in the middle of the month, it is counted in both departments for that month.当一名员工在月中发生变动时,该月的两个部门都会对其进行计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.