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
LyonsBI_BRL
Helper III
Helper III

Calculate # of Days - Based on Month and if EndDate ends during the month

Currently working on my companies finance PowerBI solution though I'm working to convert what was being done in Excel to PowerBI.

Scenario: I'm able to calculate # of Working Days excluding Weekends and Holidays no problem which is what I see in the "Working Days column". To the right I have the total number of Billable hours for November 2021 and total number of Billable Days for November 2021. 

 

What I'm trying to find here is what would be the user's Total number of Billable Days and Billable Hours based off their End date in November. As some end on November 9th, November 12th and November 16th. 

 

Basically it's a filter of saying "Based off this employee's StartDate and EndDate, only calculate the total number of billable days and hours to their end date

 

LyonsBI_BRL_2-1634068539994.png

Any help would be much appreciated. 

 

Thanks!

 

1 ACCEPTED SOLUTION

@LyonsBI_BRL 

 

Alright, finally.... apparently a little more challenging than "<>" LOL!

 

I have updated the formula. However, you will also need to add a Dates table & Public Holidays table.  

 

Dates table is as follows and no relationship is needed to other tables.

TheoC_1-1634110320850.png

Once you have the Dates table, add the following Calculated Column for the Weekday:

TheoC_2-1634110370144.png

Then you can add the Public Holidays table (make sure you convert your dates to Date). I simply used the "Enter Data" function and pressed Ctrl C and Ctrl V (copy / paste) shortcuts, then converted Date Type to "Date".

 

TheoC_3-1634110418189.png

Once this is in, you can create a Calculated Column in your Date table using LOOKUPVALUE like below:

TheoC_4-1634110507007.png

 

And finally, you can then update the below! 🙂  

 

TheoC_0-1634110251800.png

Enjoy mate! And make sure to give me a high five and tick as solved LOL!!!! 😄

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

View solution in original post

19 REPLIES 19
TheoC
Super User
Super User

Hi @LyonsBI_BRL,

 

Days Between = 

VAR _MonthStart = DATE ( 2021, 11, 01 )
VAR _MonthEnd = EOMONTH ( _MonthStart , 0 )
VAR _dateBegin = 'Table'[dateBegin]
VAR _dateEnd = 'Table'[dateEnd]
VAR _StartDate = IF ( _dateBegin >= _MonthStart , _dateBegin , _MonthStart )
VAR _EndDate = IF ( _dateEnd <= _MonthEnd , _dateEnd , _MonthEnd )

RETURN

IF ( MONTH ( _EndDate ) <> MONTH ( _MonthStart ) , 0 , INT ( _EndDate - _StartDate ) )

 

The outputs are below.  In terms of the Billable Hours, I just did Days * 7.5 so please ensure to adjust to your columns.  Hopefully the variables make sense. I find them to be far simpler than when they're not used so hopefully they help you 🙂 

TheoC_0-1634084031314.png

 

I hope this helps! 🙂

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

@TheoC 

Wow just wow!!! Okay that's a lot closer than I was three days ago! Is there anyway to modify it so that it excludes the weekends and Holidays from a Holiday table?  Thanks!

 

LyonsBI_BRL_0-1634102275655.png

 

@LyonsBI_BRL, haha champion! Yes, definitely. Where are you based? What are the public holidays you want to be filtered? 

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

@TheoC 

 

It's these holidays listed below would be the ones that need to be observed and filtered out of the calculation.

 

LyonsBI_BRL_0-1634104707722.png

 

@LyonsBI_BRL 

 

Alright, finally.... apparently a little more challenging than "<>" LOL!

 

I have updated the formula. However, you will also need to add a Dates table & Public Holidays table.  

 

Dates table is as follows and no relationship is needed to other tables.

TheoC_1-1634110320850.png

Once you have the Dates table, add the following Calculated Column for the Weekday:

TheoC_2-1634110370144.png

Then you can add the Public Holidays table (make sure you convert your dates to Date). I simply used the "Enter Data" function and pressed Ctrl C and Ctrl V (copy / paste) shortcuts, then converted Date Type to "Date".

 

TheoC_3-1634110418189.png

Once this is in, you can create a Calculated Column in your Date table using LOOKUPVALUE like below:

TheoC_4-1634110507007.png

 

And finally, you can then update the below! 🙂  

 

TheoC_0-1634110251800.png

Enjoy mate! And make sure to give me a high five and tick as solved LOL!!!! 😄

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 

I have followed the DAX formula here however the issue in my case is if the employee has a start date of 01/01/2024 and the ned date is of 31/12/2024 the no of working days are only being counted in the month of december. I want to count the total no. of days per employe per month for the year of 2024. 

Also is there a way to deduct holidat entitlement which is 20 days a year (this is excluding * public holidays which is alredat present in my base logice just need to find a way to deduc 20 leave per year this is only if the eploye has worked the whole year if they have worked ifr 6 months then that should be 10days and so on. 

 

Desierd Outcome

Month employeeStart dateEnddateNo of working days
Janx01-01-202428-01-202422
Jany02-01-202431-12-202422
Janz02-02-202404-04-20240
Febx01-01-202428-01-20240
feby02-01-202431-12-202419
Febz02-02-202404-04-202418
Marchx01-01-202428-01-20240
March y02-01-202431-12-202422
Marchz02-02-202404-04-202422
Aprilx01-01-202428-01-20240
Aprily02-01-202431-12-202422
Aprilz02-02-202404-04-20242


Thanks

Vaish

Hi @VaishnaviGandhi 

 

If you want to apply the DAX formula I have created in this post, you need to create your data to be in the same structure.  In your data, you do not need to write down each month and each employee on different rows.  Just have a Start Date column and an End Date column in a single row for a single employee.  Below is an exampe of the structure you should have. When you do this, you can then apply the DAX formula I've put together.  You will also need to create a Date table and a Public Holiday Date table as I have done in the seperate comment on this post.  All of this information you need is in this post but most importantly simplify the structure of your data to match the below:

 

Employee IdStart DateEnd Date
X1/01/202331/12/2023
Y3/03/202325/12/2023
Z8/08/202311/12/2023

 

All the best.

 

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 

 

Thank you for your quick response I have followed everything as per the post however if you see the below ss instead of the working days distributed throughout all the months an employee is working it only appears in the December month as per the start and the end date. 

the outcome i.e., no. of working days; should spread out through all the months Mr. X is employed. thsi is required to show the revenue forecast per month for contract base employees, which is why it would be helpfull if it was be ditributed in months. 

VaishnaviGandhi_1-1705430326977.png

VaishnaviGandhi_3-1705430578884.png

 

Please help if you can, the distrubution is to be across the months 
jan - 22 days, feb -20 days still the date the employee is working. 

Looking forward to hearing form you.

Regards
Vaish

@VaishnaviGandhi unfortunately, that is a different requirement to what is in this post. I suggest posting a new thread and adding example data. From here, I or another Community member can jump on to assist with a solution.

 

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

HELL YES! IT WORKED! @TheoC thank you so much! I can't tell you how much I've been going back and forth trying to solve this and the number of YouTube videos I've been watching trying to figure this one out. Thank you so much this is awesome! 

LyonsBI_BRL_0-1634113181818.png

 

@LyonsBI_BRL it is a pleasure big fella! All part of why this amazing Power BI Community exists!

 

All the best with your Power BI journey mate!

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

I got you mate. I will update formula as soon as I get in front of the computer.  On phone atm but it's a very quick update. All we do is create a VAR _HolidaysDates referencing the holidays and use <> to exclude. Give me an hour k! 🙂

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

@TheoC Hey Theo!

 

Got a question, instead of having this be a calculated column, would there be any chance this could be a measure instead? So that I could have a measure I could attach to each month. Otherwise I'd have to do this for 12 months out, plus a bunch of other column calculations for this as well. 

 

I've tried using the same formula here, but am not getting anywhere. 

 

Thanks!

Hi @LyonsBI_BRL 

 

Thanks for reaching out.  I'm not sure I am understanding the issue that the Calculated Column is causing regarding months etc. However, in saying that, I haven't tried (and am on phone), you should be able to copy and paste the formula as a new measure. 

 

Give it a go and let me know if it achieves the outcome you're wanting and if not I can help you get to where you need 😀

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

@TheoC Think I figured it out. 

So first I needed to know the number of workable days each person is working on multiplied by 8 gives me their total number of hours. Giving my _monthStart Date being 01/01/2021 as to how many days / hours they have worked or will be working for the year.

 

Forecast Workable Hours = VAR _MonthStart = DATE ( 2021, 01, 01 )
VAR _MonthEnd = EOMONTH ( _MonthStart , 12 )
VAR _dateBegin = [dateBegin]
VAR _dateEnd = [dateEnd]
VAR _StartDate = IF ( _dateBegin >= _MonthStart , _dateBegin , _MonthStart )
VAR _EndDate = IF ( _dateEnd <= _MonthEnd , _dateEnd , _MonthEnd )
VAR _NumberWorkDays = CALCULATE( COUNTROWS('Date'), DATESBETWEEN('Date'[Date], _StartDate, _EndDate), 'Date'[Weekday] = TRUE || 'Date'[Public Holidays] <> 0)

RETURN 

IF ( ISBLANK (_NumberWorkDays), 0, _NumberWorkDays) * 8

 

 

What I need to do here break this out by Month to month within a measure instead of a column. 

When I attempt to break this out into a measure, it doesn't know what [dateBegin] or [dateEnd] is so I'm assuming I'm missing some key piece here. 

LyonsBI_BRL_0-1635355035929.png

See this calculation is what leads into calculating the other fields here for Finance 

LyonsBI_BRL_1-1635355243463.png

Thank you again @TheoC please let me know what I need to change here. 

 

Thanks! 

 

Hi @LyonsBI_BRL 

 

I've always thought measures were recognisable. I'm unsure why they're not in your instance.  I recommend just copying and pasting whatever the [dateBegin] and [dateEnd] measures are that you have created into the VAR _dateBegin and VAR _dateEnd space.  Basically, rather than using a separate measure, use the measure formula in the VAR slots and do not worry about using the separate measures.

 

Let me know if that makes sense?

 

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

@TheoC 

Okay one step closer to getting this working like I need it to. I created two measures for getting the dateBegin and dateEnd date for each person. Made the beginning of the year 

 

 

DateEnd2 = VAR EndDate = CALCULATE(MIN([dateEnd]))
RETURN
    EndDate
DateBegin2 = 
VAR BeginDate = CALCULATE(MIN([dateBegin]))
RETURN
    BeginDate

 

 

Then modified the measure to include the datebegin and dateEnd

 

Forcast Number of Hours = 

VAR _MonthStart = DATE ( 2021, 01, 01 )
VAR _MonthEnd = EOMONTH ( _MonthStart , 12 )
VAR _dateBegin = [DateBegin2]
VAR _dateEnd = [DateEnd2]
VAR _StartDate = IF ( _dateBegin >= _MonthStart , _dateBegin , _MonthStart )
VAR _EndDate = IF ( _dateEnd <= _MonthEnd , _dateEnd , _MonthEnd )
VAR _NumberWorkDays = CALCULATE( COUNTROWS('Date'), DATESBETWEEN('Date'[Date], _StartDate, _EndDate), 'Date'[Weekday] = TRUE || 'Date'[Public Holidays] <> 0)

RETURN 

IF ( ISBLANK (_NumberWorkDays), 0, _NumberWorkDays) * 8

 

This in turn gave me the correct number of hours for each person through the measure

LyonsBI_BRL_0-1635394154942.png

The weird part is when I try to break it down by each month it returns to this

LyonsBI_BRL_1-1635394216853.png

Ideally it should be if I filter on October, November and December from the measure it should show me the number of possible working hours for each of those months. 

 

Any suggestions?

 

Hi @LyonsBI_BRL 

 

In all honesty, I'm not sure what is going on but I think it's to do with this:

TheoC_0-1635395116168.png

It is a bit hard without seeing the actual file.  Would you be okay if we had a Teams catch up to go over the item?  If so, send me a Private Message and we can go from there?

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

@TheoC Check your inbox just sent it over

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.