cancel
Showing results for
Did you mean:

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

Responsive Resident

## calculate average duration of users were active in selected period

Hi,
I have a report of employees' attendance in the office.
To calculate the percent of working days that they attended correctly, I need to calculate how many working days they have in potential. So I have to require a calendar of working days and their hire date and their end date.
The formula I used is working perfectly for an employee granularity and incorrect for averages of all organization levels/managers/divisions etc...
I will thank if somebody can help me to fix the last part of the formula to get the correct average.

I also put the PBIX with an example here:

WorkDaysForEmployee =
var start_day = if(max('employeesTable'[StartEmpoyeeDate])>min('Calendar'[Date]),max('employeesTable'[StartEmpoyeeDate]),min('Calendar'[Date]))
var end_day = if (ISBLANK( max('employeesTable'[LastEmpoyeeDate])),
max('Calendar'[Date]),if(MAX('employeesTable'[LastEmpoyeeDate]) > max('Calendar'[Date]),max('Calendar'[Date]),MAX('employeesTable'[LastEmpoyeeDate])))
var workdays = CALCULATE(COUNTROWS('Calendar'),'Calendar'[Date]>=start_day,'Calendar'[Date]<=end_day,'Calendar'[WorkdayFlag]=1)
var averageWorkdays = if (HASONEVALUE('employeesTable'[FullName]),workdays,AVERAGEX(VALUES('employeesTable'[FullName]),workdays))
return
averageWorkdays
1 ACCEPTED SOLUTION
Super User

@Ritaf I just tidy it up a bit and it is working, maybe tweak it further as you see fit

``````Workdays Final =
VAR __min = MIN ( 'Calendar'[date] )
VAR __max = MAX ( 'Calendar'[date] )
RETURN
AVERAGEX (
employeesTable,
VAR start_day = MAX ( 'employeesTable'[StartEmpoyeeDate], __min )
//if('employeesTable'[StartEmpoyeeDate]>__min,'employeesTable'[StartEmpoyeeDate],__min)
VAR end_day = COALESCE ( MIN ( 'employeesTable'[LastEmpoyeeDate], __max ), __max )
//if (ISBLANK( 'employeesTable'[LastEmpoyeeDate]), __max,if('employeesTable'[LastEmpoyeeDate] > __max,__max,'employeesTable'[LastEmpoyeeDate]))
VAR workdays =
CALCULATE (
COUNTROWS ('Calendar' ),
'Calendar'[Date] >= start_day,
'Calendar'[Date] <= end_day,
'Calendar'[WorkdayFlag] = 1
)
RETURN
workdays
)``````

. You can  remove those commented lines once you are happy with the result

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤

Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

7 REPLIES 7
Super User

@Ritaf I just tidy it up a bit and it is working, maybe tweak it further as you see fit

``````Workdays Final =
VAR __min = MIN ( 'Calendar'[date] )
VAR __max = MAX ( 'Calendar'[date] )
RETURN
AVERAGEX (
employeesTable,
VAR start_day = MAX ( 'employeesTable'[StartEmpoyeeDate], __min )
//if('employeesTable'[StartEmpoyeeDate]>__min,'employeesTable'[StartEmpoyeeDate],__min)
VAR end_day = COALESCE ( MIN ( 'employeesTable'[LastEmpoyeeDate], __max ), __max )
//if (ISBLANK( 'employeesTable'[LastEmpoyeeDate]), __max,if('employeesTable'[LastEmpoyeeDate] > __max,__max,'employeesTable'[LastEmpoyeeDate]))
VAR workdays =
CALCULATE (
COUNTROWS ('Calendar' ),
'Calendar'[Date] >= start_day,
'Calendar'[Date] <= end_day,
'Calendar'[WorkdayFlag] = 1
)
RETURN
workdays
)``````

. You can  remove those commented lines once you are happy with the result

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤

Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Super User

@Ritaf this is what I see, there are 3 files, if you just give me the file name, it will be easier. 😁

Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤

Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Responsive Resident

sorry it's named SampleFile.pbix
maybe this link will take you to the right place

Super User

@Ritaf there are few files which one to use?

Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤

Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Responsive Resident

I really don't know why it showing multiple files

Super User

@Ritaf hmmm, can you share a sample pbix file, remove sensitive information before sharing.

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤

Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Responsive Resident

Hi,
thanks for responding
I put a PBIX with an example here

Announcements

#### Fabric certifications survey

Certification feedback opportunity for the community.

#### Power BI Monthly Update - April 2024

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

#### Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors