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
yasbos
Resolver I
Resolver I

Unique Records Based On Employee Name

Greetings to everyone 🙂 I tried to solve this below problem in SQL (my preference), but I couldn't, so I'm now trying to solve it in DAX.

There is a scenario where an employee is considered active with the company based on whether they are active with at least one of the company's clients. The example below illustrates this. All six records belong to the same employee, EmpX, shown under the Full_Name column. However, because EmpX provides services to six clients, C1, C2, etc., as shown in the column Code, EmpX appears 6 times with different Start dates (always has a value) and End_Date (which will have a value only if EmpX service to this client has been discontinued.)

What I'm trying to do is to count the number of employees who have been active in each year. The active criterion is that if the employee has been active for at least one client during that year (the start date is equal to or less than the year) then this employee gets counted as active in, say 2020. I managed to do this step by adding the last three columns indicating if the employee was/is active during that year for that client. However, I now need to count the employees that were/are active under each year. If I add the 1s under each of the columns, I will clearly count the same employee multiple times as being active during this year.

In the below example, EmpX should be counted as only 1 under each year, rather than now 5 under 2020, 4 under 2021 and 4 under 2022.

In summary, I need to count the employee as being active for our company (not the client) during a given year. The employee is active for the company if they are active with at least one client. Once that is accomplished, I can then sum up the number of active employees per year in order to produce the total per year of active employees for our company.

Thank you.

 code  FULL_NAME  Start_Date  End_Date  ACTIVE_2020  ACTIVE_2021  ACTIVE_2022 
 -----------  -----------  -----------  -----------  -----------  -----------  ----------- 
 c1  EmpX  2017-04-21  NULL 111
 c2  EmpX  2014-12-24  2021-05-12 100
 c3  EmpX  2013-01-11  NULL 111
 c4  EmpX  2013-05-31  NULL 111
 c5  EmpX  2014-12-24  NULL 111
 c6  EmpX  2017-04-21  2017-04-21 000
1 ACCEPTED SOLUTION

HI @yasbos 
I transformed the same formula to another one working at year level only and with simple subtraction ( - 1 ) we can get the desired result. This solution is faster in terms of performance but it works only at year level not date not week not month. https://www.dropbox.com/t/zkNkoE4gdTOvgLKC

1.png2.png

Annual Active Employees = 
VAR CurrentPeriod = 
    VALUES ( 'Date'[Date].[Year] )
RETURN
    SUMX (
        VALUES ( Data[FULL_NAME] ),
        VAR StartDate = 
            YEAR ( CALCULATE ( MIN ( Data[Start_Date] ) ) )
        VAR EndaDateCheck = 
            COUNTROWS ( CALCULATETABLE ( Data, Data[End_Date] <> BLANK ( ) ) ) 
                = COUNTROWS ( CALCULATETABLE ( Data ) )
        VAR EndDate = 
            IF ( EndaDateCheck, YEAR ( CALCULATE ( MAX ( Data[End_Date] ) ) ) - 1, YEAR ( TODAY ( ) ) )
        VAR ServicePeriod =
            GENERATESERIES ( StartDate, EndDate, 1 )
        RETURN 
            IF ( NOT ISEMPTY ( INTERSECT ( CurrentPeriod, ServicePeriod ) ), 1 )
    )

View solution in original post

12 REPLIES 12
tamerj1
Super User
Super User

Hi @yasbos 
Another way of doing that: https://www.dropbox.com/t/MvlaosDA8p7vw6dD

The thing that I don't understand is why you don't want to include EmpZ in 2020 while he was active until Aug. 2020? Please clarify this point and what is your criteria.

1.png

Active Employees = 
VAR CurrentPeriod = 
    VALUES ( 'Date'[Date] )
RETURN
    SUMX (
        VALUES ( Data[FULL_NAME] ),
        VAR StartDate = 
            CALCULATE ( MIN ( Data[Start_Date] ) )
        VAR EndaDateCheck = 
            COUNTROWS ( CALCULATETABLE ( Data, Data[End_Date] <> BLANK ( ) ) ) 
                = COUNTROWS ( CALCULATETABLE ( Data ) )
        VAR EndDate = 
            IF ( EndaDateCheck, CALCULATE ( MAX ( Data[End_Date] ) ), TODAY ( ) )
        VAR ServicePeriod =
            CALENDAR ( StartDate, EndDate )
        RETURN 
            IF ( NOT ISEMPTY ( INTERSECT ( CurrentPeriod, ServicePeriod ) ), 1 )
    )

Thanks so much,  @tamerj1 . If the year ended and the employee was still active, then I would like to count them. So, if the employee started in 2014 and ended in 2014, then I would like to exclude that from the count. Also, if the employee started in 2013 and ended in, say, 2016, then they were active in 2013, 2014, and 2015. However, I still need to count all of the active employees for the current year (2022) because I need to compare 2022 to the previous years. You see, in my mind, the solution should be easily convertable between incuding the year in which the employee ceased to be active or excluding it. I basically thought/think that it should be just the equality/inequality signs that will need to be adjusted as desired. Thanks again. You @tamerj1  and @Jihwan_Kim are wonderful.

HI @yasbos 
I transformed the same formula to another one working at year level only and with simple subtraction ( - 1 ) we can get the desired result. This solution is faster in terms of performance but it works only at year level not date not week not month. https://www.dropbox.com/t/zkNkoE4gdTOvgLKC

1.png2.png

Annual Active Employees = 
VAR CurrentPeriod = 
    VALUES ( 'Date'[Date].[Year] )
RETURN
    SUMX (
        VALUES ( Data[FULL_NAME] ),
        VAR StartDate = 
            YEAR ( CALCULATE ( MIN ( Data[Start_Date] ) ) )
        VAR EndaDateCheck = 
            COUNTROWS ( CALCULATETABLE ( Data, Data[End_Date] <> BLANK ( ) ) ) 
                = COUNTROWS ( CALCULATETABLE ( Data ) )
        VAR EndDate = 
            IF ( EndaDateCheck, YEAR ( CALCULATE ( MAX ( Data[End_Date] ) ) ) - 1, YEAR ( TODAY ( ) ) )
        VAR ServicePeriod =
            GENERATESERIES ( StartDate, EndDate, 1 )
        RETURN 
            IF ( NOT ISEMPTY ( INTERSECT ( CurrentPeriod, ServicePeriod ) ), 1 )
    )

Thanks, @tamerj1 . Something seems wrong, though -- unless I am missing something. For example, you have 3 for 2015, when 2015 has only one entry. Similar problems with 2016, etc. They don't seem to meet the criteria.

yasbos_0-1658768048665.pngyasbos_1-1658768094355.png

 

@yasbos 
Yes but the employees from previous years are still active. Aren't they?

@tamerj1 , Thanks so much. Yes. This seems to be working. Much appreciated.

@tamerj1  oh my! You are right!! Sorry...in meetings (and in one now) and clearly am not focused. I will review after done in the next hour or so. I'll get back to you in under 2 hours. Thanks so much!

Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

Untitled.png

 

Active FullName count measure: = 
VAR _mindate =
    MIN ( 'Calendar'[Date] )
VAR _maxdate =
    MAX ( 'Calendar'[Date] )
VAR _activeemptable =
    SUMMARIZE (
        FILTER (
            ADDCOLUMNS (
                SUMMARIZE ( Data, Data[Code], Data[FULL_NAME] ),
                "@active",
                    CALCULATE (
                        COUNTROWS (
                            FILTER (
                                Data,
                                Data[Start_Date] <= _maxdate
                                    && OR ( Data[End_Date] >= _mindate, Data[End_Date] = BLANK () )
                            )
                        )
                    )
            ),
            [@active] >= 1
        ),
        Data[FULL_NAME]
    )
RETURN
    IF( HASONEVALUE('Calendar'[Year CC] ), COUNTROWS(_activeemptable) )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


@Jihwan_Kim Thanks so much! There just seems to be a tiny glitch there. for example, 2020 active employees should be 2, but the table is shown 3. Regardless, I really appreciate your work. This is great!!

yasbos_0-1658688758785.png

 

Hi,

EmpZ for code10 worked in 2020.

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks @Jihwan_Kim . The screen that I provided above and below shows that the total for 2020 should be 2. However, the table in your pbix shows 3 for 2020. Thanks so much for the effort. 

yasbos_0-1658721386992.pngyasbos_1-1658721410226.png

 

@Jihwan_Kim I have been making modifications to the date logic in your code -- to no avail 😞

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.