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
Anonymous
Not applicable

Days Accumulated Since Start of Month

Hi Everyone

 

I am trying to show how many days of a rental period have been accrued in the 'current' month. For example, if an ongoing rental period is on 56 days, I need to know how many days of that period have been accrued in the 'current' month.

 

My data set has the rental 'start date' and 'end date' so working out total rental length with Datediff is easy enough. Where I am struggling is with days accrued in 'current' month.

 

In my mind I thought I needed a column that worked out if a rental was live in the 'current' month so I created a column that does this. I then thought I would use this column along with another piece of DAX so I tried this: Days Sold This Month = DATEDIFF(STARTOFMONTH(),Hire_Data[Off Hire Date],DAY) but sadly it didn't.

 

Does anyone have any help/tips/a solution to this?

 

Many thanks

James

4 REPLIES 4
kentyler
Solution Sage
Solution Sage

STARTOFMONTH() needs a date as an argument 

=STARTOFMONTH(DateTime[DateKey])

 





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Anonymous
Not applicable

Hi Ken

 

Thanks for looking at this.

 

If i did that and used it in the DAX like this, =datediff(startofmonth(rental info[start date]),rental info[end date]),Day) I get an artificially increased rental length. 

 

For example, a rental starting on 15/04/20 and ending 12/05/20 has a 28 day rental length but the above DAX returns a 41 day rental. What I'm aiming for is it to show a 12 day rental length, signifying 12 of the 28 days rental were accrued in May.

 

Hope this helps clarify my issue and thanks again.

 

James

Days Sold This Month = DATEDIFF(STARTOFMONTH(),Hire_Data[Off Hire Date],DAY) 

 

Assuming you have a Date table and you are using a column from that in your visual, you could use an approach like this:

 

Hired Days =
VAR mindate =
MIN ( Date[Date] ) //get min and max dates from you visual that uses a column from Date table
VAR maxdate =
MAX ( Date[Date] )
VAR result =
SUMX (
Hire_Data, // iterate over each row of your main fact table

VAR adjmindate =
IF ( Hire_Data[On Hire Date] < mindate, mindate, Hire_Data[On Hire Date] ) //figure out which date to use as min
VAR adjmaxdate =
IF ( Hire_Data[Off Hire Date] > maxdate, maxdate, Hire_Data[Off Hire Date] ) //figure out which date to use as max
RETURN
DATEDIFF ( adjmindate, adjmaxdate, DAY ) //return difference in days so it will be summed with all the other rows
)
RETURN
result

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi Pat

Thanks for your post. It has returned "486962" for all rows. 

I have checked the relationship between the fact table and the calendar look up and it is correct.

 

Just to confirm, I need to get a calculated column that tells me how many days of a rental period have been accrued in the 'current month'.

Some rental periods will have started in a previous month so the days accrued in the previous month(s) need to be excluded. Some rentals will not have accrued any days in the 'current month' because they started and ended in previous months and so should show '0' 

 

Many thanks again

James

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