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

Preparing for a certification exam? Ask exam experts all your questions on May 15th. Register now.

Reply
Ritaf
Responsive Resident
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:
https://drive.google.com/file/d/1UU-u3Z0xjU0MqPrC_RoRLhyvI1ksmYrC/view

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
parry2k
Super User
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

 

parry2k_0-1641745084929.png

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

 

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.

View solution in original post

7 REPLIES 7
parry2k
Super User
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

 

parry2k_0-1641745084929.png

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

 

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.

parry2k
Super User
Super User

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

 

parry2k_0-1641743675792.png

 



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.

Ritaf
Responsive Resident
Responsive Resident

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

https://drive.google.com/file/d/1UU-u3Z0xjU0MqPrC_RoRLhyvI1ksmYrC/view?usp=sharing

 

parry2k
Super User
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.

Ritaf
Responsive Resident
Responsive Resident

if you press download it takes only one PBIX.
I really don't know why it showing multiple files

parry2k
Super User
Super User

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

 

 

Follow us on LinkedIn and YouTube.gif to our YouTube channel

 

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.

Ritaf
Responsive Resident
Responsive Resident

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

https://drive.google.com/drive/folders/180w1__xEBq31goTDTM_gOOK_Y7MUzHkq?usp=sharing

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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