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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sidexcel
Helper I
Helper I

Average Tenure by Department / Location by Time

Hi All,

 

I am posting this again as I did not have too much luck getting it correctly answered previous time. I did however make some progress in getting the correct result  by employee (even if not at the aggregation level that I am looking for). 

 

The Excel Data set is here.

The PBIX file is here

 

Essentially I am looking to create a report that shows average tenure calculated over time. The measure that I created seems to work perfectly if I look at by employee but not if I would like to aggregate by a dimension (such as department or location). 

 

My measure is as follows:

 

Tenure = 
VAR HireDate = CALCULATE(MAX('Employee Roster'[Hire Date]), ALLEXCEPT('Employee Roster', 'Employee Roster'[External ID]))
VAR TermDate = CALCULATE(MAX('Employee Roster'[Term Date]), ALLEXCEPT('Employee Roster', 'Employee Roster'[External ID]))
VAR EOM = MAX('Calendar Table'[Date])
VAR StartDate = IF(HireDate<=EOM, HireDate, blank())
VAR EndDate = IF(TermDate=BLANK(), EOM, IF(EOMONTH(TermDate,0)>=EOM, min(EOM,TermDate), BLANK()))
VAR TimeElapsed = calculate(averagex(all('Employee Roster'),DATEDIFF(StartDate, EndDate, Month)))
RETURN
TimeElapsed

 

 

The solution is incorrect because the aggregation for average is not working as seem in the screenshot:

 

sidexcel_0-1651784869540.png

 

Thank you in advance! 

@amitchandak you were very helpful in the previous post! 

 

Edit: Updated correct link! 

1 ACCEPTED SOLUTION

Hi @sidexcel ,

 

Here I create a calculated table to achieve your goal.

Table = 
VAR _GENERATE =
    GENERATE (
        SUMMARIZE (
            'Employee Roster',
            'Employee Roster'[Location],
            'Employee Roster'[External ID]
        ),
        SUMMARIZE('Calendar Table','Calendar Table'[Month & Year],'Calendar Table'[MonthnYear])
    )
VAR _ADD =
    ADDCOLUMNS (
        _GENERATE,
        "1",
            VAR HireDate =
                CALCULATE (
                    MAX ( 'Employee Roster'[Hire Date] ),
                    ALLEXCEPT ( 'Employee Roster', 'Employee Roster'[External ID] )
                )
            VAR TermDate =
                CALCULATE (
                    MAX ( 'Employee Roster'[Term Date] ),
                    ALLEXCEPT ( 'Employee Roster', 'Employee Roster'[External ID] )
                )
            VAR EOM =
                CALCULATE ( MAX ( 'Calendar Table'[Date] ) )
            VAR StartDate =
                IF ( HireDate <= EOM, HireDate, BLANK () )
            VAR EndDate =
                IF (
                    TermDate = BLANK (),
                    EOM,
                    IF ( EOMONTH ( TermDate, 0 ) >= EOM, MIN ( EOM, TermDate ), BLANK () )
                )
            VAR TimeElapsed =
                CALCULATE ( DATEDIFF ( StartDate, EndDate, MONTH ) )
            RETURN
                TimeElapsed
    )
RETURN
    _ADD

Then create a visual by columns in this table directly and use average function in matrix value field.

RicoZhou_0-1652262785850.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
TheoC
Super User
Super User

Hi @sidexcel 

 

It looks like both links are to Excel files?  Could you just check that the PBIX is uploaded correctly?

 

Thanks heaps,

Theo

 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Hi @TheoC sorry about that! I fixed the links in the original post! 

Hi @sidexcel ,

 

Here I create a calculated table to achieve your goal.

Table = 
VAR _GENERATE =
    GENERATE (
        SUMMARIZE (
            'Employee Roster',
            'Employee Roster'[Location],
            'Employee Roster'[External ID]
        ),
        SUMMARIZE('Calendar Table','Calendar Table'[Month & Year],'Calendar Table'[MonthnYear])
    )
VAR _ADD =
    ADDCOLUMNS (
        _GENERATE,
        "1",
            VAR HireDate =
                CALCULATE (
                    MAX ( 'Employee Roster'[Hire Date] ),
                    ALLEXCEPT ( 'Employee Roster', 'Employee Roster'[External ID] )
                )
            VAR TermDate =
                CALCULATE (
                    MAX ( 'Employee Roster'[Term Date] ),
                    ALLEXCEPT ( 'Employee Roster', 'Employee Roster'[External ID] )
                )
            VAR EOM =
                CALCULATE ( MAX ( 'Calendar Table'[Date] ) )
            VAR StartDate =
                IF ( HireDate <= EOM, HireDate, BLANK () )
            VAR EndDate =
                IF (
                    TermDate = BLANK (),
                    EOM,
                    IF ( EOMONTH ( TermDate, 0 ) >= EOM, MIN ( EOM, TermDate ), BLANK () )
                )
            VAR TimeElapsed =
                CALCULATE ( DATEDIFF ( StartDate, EndDate, MONTH ) )
            RETURN
                TimeElapsed
    )
RETURN
    _ADD

Then create a visual by columns in this table directly and use average function in matrix value field.

RicoZhou_0-1652262785850.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-rzhou-msft 

 

This is great! I am curious, is it possible to do this completely via virtual table and not creating a helper table to achieve?

 

Highly grateful of your help!

-Sid

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.