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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
VHL
Frequent Visitor

How to count employees of different types of seniority

Hello. 

 

I would like to ask for help with something, probably a measure or a set of measures, but I will take anything, that will allow me to count (month by month) how many employees have been with us for less than 6 months, for 6-18 months, for 18-36 months and more than 36 months, at any month during the last 3 years.

 

I have a Calendar table, which is a date table, consisting od Date, Year, Month.

 

Then I have a table of employees consisting of ID, Name, Start_date, End_date, where Start_date is the date employee started on their position and End_date is the date they left this position. If the employee is still hired and working, End_date is left empty. One employee can be in this table multiple times, for example one record (row) dated from first starting with company to leaving for maternity leave, then second record (row) dated from returning from maternity leave to now. Example of data:

IDNameStart_dateEnd_date
1A1.6.2023 
2B1.7.201729.2.2020
2B1.3.202131.10.2022
4C1.12.201731.1.2021
4C1.2.2021 
5D1.3.2002 
6E20.1.202031.12.2022
6E1.1.202331.8.2023
7F11.10.201729.2.2020
7F1.3.202031.3.2022
8G15.3.201430.11.2015
8G1.12.201514.4.2019
8G15.4.201928.2.2020
8G1.1.2023 
9H1.1.202329.3.2023
10I15.1.2002 
11J17.11.2014 
12K1.1.202317.3.2023
13L15.10.201729.2.2020
13L1.3.202030.11.2020
14M19.9.2012 
15N1.10.2005 
16O6.6.201631.1.2021
16O1.2.2021 
17P1.12.201829.2.2020

 

I have multiple interconnected tables in my model, so the Calendar table and Employee table aren't in a relationship and the only relationship I can create between them is inactive (and that seemed to have its problems in measures I tried, as when conecting Date + Start_date and Date + End_date, it was saying there are ambiguous paths between tables).

 

The result I need is a stacked area chart (or something similar) + a table showing the number of employees in each category, like this (rok = year in my language):

VHL_0-1702367116907.png

None of the measures I tried really worked and I tried about 7-8 different ones, and I spent 2 weeks trying to make them work. So apologies, but I am not posting any here - I have no idea which version might be closest to correct.

 

Does anyone know how to do this? 

 

Thank you very much for help.

1 ACCEPTED SOLUTION
VHL
Frequent Visitor

In the end I couldn't make this work through measures alone. I solved this problem by creating a table "SENIORITY" through SQL select with 3 columns (orange): 

1. Date - end of month for every month during the last 3 years (this was enough for the chart and table I need)

2. ID of employee

3. value 1 if this employee was working for us on that date or 0 if they weren't

 

To this table I appended, for each employee ID (if it appeared in 3 rows then 3 times per that ID), with the date of 3 years minus 1 month, count of all the months they were working for us before the oldest date in the table (green):

 

DateIDPresence
31.1.2021  113
31.1.2021  218
31.1.2021  275
31.1.2021  3221
31.1.2021  428
31.1.2021  5101
31.1.2021  514
31.1.2021  519
31.1.2021  6184
28.2.2021  10
28.2.2021  21
28.2.2021  31
28.2.2021  41
28.2.2021  51
28.2.2021  61
31.3.2021  10
31.3.2021  20
31.3.2021  31
31.3.2021  41
31.3.2021  51
31.3.2021  61
30.4.2021  ......

 

This table is refreshed daily. With it, I managed to get my numbers through simple measures:

 

Sum of Presence = 

var _currentdate = SELECTEDVALUE('SENIORITY'[Date])
RETURN
CALCULATE(SUM('SENIORITY'[Presence]), ALL('SENIORITY'), VALUES('SENIORITY'[ID]), 'SENIORITY'[Date] <= _currentdate)
 
 
0 - 0,5 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 6  && 'SENIORITY'[Presence] <> 0)
          )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
0,51 - 1,5 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 18 && 'SENIORITY'[Sum of Presence] > 6  && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
1,51 - 3 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 36 && 'SENIORITY'[Sum of Presence] > 18  && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
3+ years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] > 36 && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)

View solution in original post

3 REPLIES 3
TobyNye
Resolver II
Resolver II

I created a data model based on the values you put in your post:

TobyNye_0-1702378716302.pngTobyNye_1-1702378734894.png

I have kept these tables unrelated as you explained:

TobyNye_2-1702378773333.png

I created a measure for each grouping to create the kind of visual that you wanted:

TobyNye_3-1702378812359.png

This is the general structure that all of the measures follow:

6 - 17 Months = CALCULATE(DISTINCTCOUNT(Data[ID]), FILTER(Data,
                                                    (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) >= 6
                                                    && (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) <= 17
                                                    && YEAR(Data[Start_date]) <= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[Start_date]) <= SELECTEDVALUE(dimDate[Month Num])
                                                    && ((YEAR(Data[End_date]) >= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[End_date]) > SELECTEDVALUE(dimDate[Month Num])) || ISBLANK(Data[End_date]))
)
) + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0)
 
You can change the >= and <= signs as fits your use case, the setup I have put in is going from 6-17 months such that the next grouping can be 18-35 then 36+. The first two arguments in the filter clause are effefctively just a date diff but using only year and month as that is as far as you wanted to drill down on your graph (therefore they would be the only parts of the date with a value at that level). You can also change the numbers as you please if you want the bandings to be a bit different, this is just what I thought you wanted. The + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0) is not entirely necessary, it just means that the value will be blank if the date from the date table is outside of the selected range and replace blanks with a 0 inside the range so it looks nice, you can remove that entirely if you want, the graph will just show blanks instead of 0's. Note that it won't work if you don't have some kind of date filter affecting the visuals that you use it in. 
The rest of the measures:
0 - 5 Months = CALCULATE(DISTINCTCOUNT(Data[ID]), FILTER(Data,
                                                    (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) < 6
                                                    && YEAR(Data[Start_date]) <= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[Start_date]) <= SELECTEDVALUE(dimDate[Month Num])
                                                    && ((YEAR(Data[End_date]) >= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[End_date]) > SELECTEDVALUE(dimDate[Month Num])) || ISBLANK(Data[End_date]))
)
) + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0)
 
18 - 35 Months = CALCULATE(DISTINCTCOUNT(Data[ID]), FILTER(Data,
                                                    (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) >= 18
                                                    && (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) <= 35
                                                    && YEAR(Data[Start_date]) <= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[Start_date]) <= SELECTEDVALUE(dimDate[Month Num])
                                                    && ((YEAR(Data[End_date]) >= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[End_date]) > SELECTEDVALUE(dimDate[Month Num])) || ISBLANK(Data[End_date]))
)
) + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0)
 
36+ Months = CALCULATE(DISTINCTCOUNT(Data[ID]), FILTER(Data,
                                                    (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) >= 36
                                                    && YEAR(Data[Start_date]) <= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[Start_date]) <= SELECTEDVALUE(dimDate[Month Num])
                                                    && ((YEAR(Data[End_date]) >= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[End_date]) > SELECTEDVALUE(dimDate[Month Num])) || ISBLANK(Data[End_date]))
)
) + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0)
 
6 - 17 Months = CALCULATE(DISTINCTCOUNT(Data[ID]), FILTER(Data,
                                                    (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) >= 6
                                                    && (SELECTEDVALUE(dimDate[Year]) - YEAR(Data[Start_date])) * 12 + SELECTEDVALUE(dimDate[Month Num]) - MONTH(Data[Start_date]) <= 17
                                                    && YEAR(Data[Start_date]) <= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[Start_date]) <= SELECTEDVALUE(dimDate[Month Num])
                                                    && ((YEAR(Data[End_date]) >= SELECTEDVALUE(dimDate[Year])
                                                    && MONTH(Data[End_date]) > SELECTEDVALUE(dimDate[Month Num])) || ISBLANK(Data[End_date]))
)
) + IF(SELECTEDVALUE(dimDate[Year]) >= 2020, 0)
 
Hope this helps, let me know if you have any further questions or issues.
VHL
Frequent Visitor

@TobyNye Thank you for trying. I checked your measures, but from what I see, they don't return correct numbers. They seem to have the same problem mine had when I tried - they don't sum peoples months here by ID. 

 

On the sample I offered, I would expect in current month to have overall 9 people:

0 - 5 Months:     0

6 - 17 Month:     1  (A)

18 - 35 Months:  0

36+ Months:       8 (C, D, G, I, J, M, N, O)

 

Any idea how to do that?

Otherwise what you did there is great 🙂

VHL
Frequent Visitor

In the end I couldn't make this work through measures alone. I solved this problem by creating a table "SENIORITY" through SQL select with 3 columns (orange): 

1. Date - end of month for every month during the last 3 years (this was enough for the chart and table I need)

2. ID of employee

3. value 1 if this employee was working for us on that date or 0 if they weren't

 

To this table I appended, for each employee ID (if it appeared in 3 rows then 3 times per that ID), with the date of 3 years minus 1 month, count of all the months they were working for us before the oldest date in the table (green):

 

DateIDPresence
31.1.2021  113
31.1.2021  218
31.1.2021  275
31.1.2021  3221
31.1.2021  428
31.1.2021  5101
31.1.2021  514
31.1.2021  519
31.1.2021  6184
28.2.2021  10
28.2.2021  21
28.2.2021  31
28.2.2021  41
28.2.2021  51
28.2.2021  61
31.3.2021  10
31.3.2021  20
31.3.2021  31
31.3.2021  41
31.3.2021  51
31.3.2021  61
30.4.2021  ......

 

This table is refreshed daily. With it, I managed to get my numbers through simple measures:

 

Sum of Presence = 

var _currentdate = SELECTEDVALUE('SENIORITY'[Date])
RETURN
CALCULATE(SUM('SENIORITY'[Presence]), ALL('SENIORITY'), VALUES('SENIORITY'[ID]), 'SENIORITY'[Date] <= _currentdate)
 
 
0 - 0,5 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 6  && 'SENIORITY'[Presence] <> 0)
          )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
0,51 - 1,5 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 18 && 'SENIORITY'[Sum of Presence] > 6  && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
1,51 - 3 years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] <= 36 && 'SENIORITY'[Sum of Presence] > 18  && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)
 
 
3+ years =
VAR _result = CALCULATE(DISTINCTCOUNT('SENIORITY'[ID]),
          FILTER('SENIORITY', 'SENIORITY'[Sum of Presence] > 36 && 'SENIORITY'[Presence] <> 0)
         )
RETURN
IF(NOT(ISBLANK(_result)), _result, 0)

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.