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
Vinay06
Frequent Visitor

Average Tenure

Hi, I want to calculate Average tenure using 'Hiring Date' and 'Leaving date'.but in 'Leaving date' column there are some empty rows, i want replace empty rows with current date. so please help me in creating a dax code. 

 

Screenshot (28).png

 

 

1 ACCEPTED SOLUTION

You're welcome 🙂

 

I think the above measure is doing what you want but let me check it with you. Let me know.

I have attached a sample PBIX.

 

Just to explain the measure:

  • It iterates row by row over the table (named YourTable as a placeholder)
  • For the purpose of the measure, blank Leaving Dates are replaced with TODAY() using the COALESCE function, then the Hiring Date is subtracted from the Leaving Date.
  • So the blank Leaving Dates remain blank in the table, and are just overridden for the measure calculation itself.
  • One thing to note is that TODAY() is determined when the measure is calculated in the report, but query results can be cached, so that if you left a report open from one day to the next, you might get results that are based on a past value of TODAY().

Here are some sample outputs using a subset of your posted data plus some additional rows added by me:

 

OwenAuger_0-1615750879344.png

In case you were considering adding a column that replaces blank Leaving Dates with TODAY(), you would just need to be aware that this column would generally only be updated when refresh occurs.

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

4 REPLIES 4
Vinay06
Frequent Visitor

It's working, Thank you so much.

OwenAuger
Super User
Super User

Hi @Vinay06 

 

If you want to leave the blank Leaving Dates as they are, and in those cases treat "today" as the Leaving Date (when the measure is evaluated), you can write a measure like this:

Average Tenure (Days) = 
VAR TodayDate = TODAY ()
RETURN
    AVERAGEX ( 
        YourTable,
        COALESCE ( YourTable[Leaving Date], TodayDate ) -- Blanks changed to TODAY()
        - YourTable[Hiring Date]
    )

The above measure calcualtes tenure in days by subtracting dates. You can instead use DATEDIFF if you need something different. 

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thanks for spending your time for me. Actually i want to include those blank leaving dates but instead of blank value i want to replace them with today date,and can you provide dax code for calculating averege tenure as well.
1) calculate average tenure using Hiring date and Leaving date

2) Replace blank values in Leaving date with today date.

please provide me  with a complete dax code for above mentioned points.

 

You're welcome 🙂

 

I think the above measure is doing what you want but let me check it with you. Let me know.

I have attached a sample PBIX.

 

Just to explain the measure:

  • It iterates row by row over the table (named YourTable as a placeholder)
  • For the purpose of the measure, blank Leaving Dates are replaced with TODAY() using the COALESCE function, then the Hiring Date is subtracted from the Leaving Date.
  • So the blank Leaving Dates remain blank in the table, and are just overridden for the measure calculation itself.
  • One thing to note is that TODAY() is determined when the measure is calculated in the report, but query results can be cached, so that if you left a report open from one day to the next, you might get results that are based on a past value of TODAY().

Here are some sample outputs using a subset of your posted data plus some additional rows added by me:

 

OwenAuger_0-1615750879344.png

In case you were considering adding a column that replaces blank Leaving Dates with TODAY(), you would just need to be aware that this column would generally only be updated when refresh occurs.

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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.

Top Solution Authors