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
vojtechsima
Memorable Member
Memorable Member

Count Week of Year from Specific Hour in Day

Hello,

I'd like to determine the Start of the Week in Year based on a Specific Hour in Day.

I want to start the Week at 08:00 AM on Friday. Meaning Friday 07:59 would be Week 13 and Friday 08:00 Week 14 already.

if [Priority] = "Critical" & [Issue Type] = "Incident" then Date.WeekOfYear([Resolved], Day.Friday+#duration(0,8,0,0)) else Date.WeekOfYear([Created], Day.Friday+#duration(0,8,0,0))

 

So far I tried to work with adding duration but I can't make it work. Could you please advise me on how to properly solve it?

Thank you

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Adding the duration to Day.Friday etc... will not work because Day.Friday or Day.Monday etc... are not DateTime values.

What you could do instead is check if your fields [Resolved] and [Created] have timezone values attached to them, if not add them, and then use the DateTimeZone.SwitchZone function to switch the timezone back by 08 hours. 08:00 AM onwards will be shifted back by 08 hours and the system will think it is midnight and then use this temporary timezone switched DateTime value to calculate the Week Number. Any time earlier than 08:00 AM will be pushed to the previous date because of the shift in timezone so that will get handled accordingly.

 

Another alternative approach would be to convert the DateTime values to decimal numbers, manipulate them, and turn those manipulated numbers back to DateTime and then calculate the Week Number. When I say, manipulate, I mean that internally a DateTime value is stored as a decimal number where the integer part is the days and the fractional/decimal part is the time value. One hour = 1/24, One Minute = 1/(24*60) and One Second = 1/(24*60*60) and so on like 1 Milli Second = 1/(24*60*60*1000) etc... Once you have the DateTime values converted to decimal numbers you can just reduce 08 hours (1/3) from that number and convert it back to a DateTime.

 

There are many possibilities.

View solution in original post

Thank you, this is how I actually ended up doing it, I subtract 8 hours from [Resolved] and [Created].

Here is the final code:

if [Priority] = "Critical" & [Issue Type] = "Incident" then Date.WeekOfYear([Resolved]-#duration(0,8,0,0), Day.Friday) else Date.WeekOfYear([Created]-#duration(0,8,0,0), Day.Friday)

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Adding the duration to Day.Friday etc... will not work because Day.Friday or Day.Monday etc... are not DateTime values.

What you could do instead is check if your fields [Resolved] and [Created] have timezone values attached to them, if not add them, and then use the DateTimeZone.SwitchZone function to switch the timezone back by 08 hours. 08:00 AM onwards will be shifted back by 08 hours and the system will think it is midnight and then use this temporary timezone switched DateTime value to calculate the Week Number. Any time earlier than 08:00 AM will be pushed to the previous date because of the shift in timezone so that will get handled accordingly.

 

Another alternative approach would be to convert the DateTime values to decimal numbers, manipulate them, and turn those manipulated numbers back to DateTime and then calculate the Week Number. When I say, manipulate, I mean that internally a DateTime value is stored as a decimal number where the integer part is the days and the fractional/decimal part is the time value. One hour = 1/24, One Minute = 1/(24*60) and One Second = 1/(24*60*60) and so on like 1 Milli Second = 1/(24*60*60*1000) etc... Once you have the DateTime values converted to decimal numbers you can just reduce 08 hours (1/3) from that number and convert it back to a DateTime.

 

There are many possibilities.

Thank you, this is how I actually ended up doing it, I subtract 8 hours from [Resolved] and [Created].

Here is the final code:

if [Priority] = "Critical" & [Issue Type] = "Incident" then Date.WeekOfYear([Resolved]-#duration(0,8,0,0), Day.Friday) else Date.WeekOfYear([Created]-#duration(0,8,0,0), Day.Friday)
Anonymous
Not applicable

Simple and Elegant 🙂

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.