cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Clxmxnt25
Frequent Visitor

Get Dates of current week by weeknumber

I've been looking for a way to get the current days of the current weeknumber so that when I press the left arrow by where it says "Week [WKNUM]" it'll decrement the weeknumber and recalculate the days of the new week. The same for if I press the arrow to the right of the week text, it'll increment the weeknumber and change the numbers. I am unable to figure it out after trying several formulas.

Clxmxnt25_0-1621827407116.png

I have values set in place like CurrentDay, CurrentWeek, CurrentWeekDay, CurrentMonth, CurrentYear and SelectedXXX counterparts that defaults to the CurrentXXX.

1 ACCEPTED SOLUTION

Accepted Solutions
v-jefferni
Community Support
Community Support

Hi @Clxmxnt25 ,

 

I made a test for your reference:

1\ OnVisible of the Screen:

UpdateContext({CurrentWeek:RoundDown((10 + (DateDiff(Date(Year(Today()), 1, 1), Today(), Days)+1) - (Weekday(Today())-1)) / 7, 0) + 1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

2\ Left icon:

UpdateContext({CurrentWeek:CurrentWeek-1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

3\ Right icon:

UpdateContext({CurrentWeek:CurrentWeek+1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

Result:

3.gif

The result collection "coldays" is the table for using. DateValue("2021.1.1") need to be modified to like

DateValue(Concatenate(Text(CurrentYear),".1.1"))

 

Hope this helps.

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

6 REPLIES 6
v-jefferni
Community Support
Community Support

Hi @Clxmxnt25 ,

 

I made a test for your reference:

1\ OnVisible of the Screen:

UpdateContext({CurrentWeek:RoundDown((10 + (DateDiff(Date(Year(Today()), 1, 1), Today(), Days)+1) - (Weekday(Today())-1)) / 7, 0) + 1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

2\ Left icon:

UpdateContext({CurrentWeek:CurrentWeek-1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

3\ Right icon:

UpdateContext({CurrentWeek:CurrentWeek+1});
Clear(coldays);
ForAll(Sequence(7,(7-Weekday(DateValue("2021.1.1")-1))+(CurrentWeek-1)*7-6,1) As D,Collect(coldays, DateAdd(DateValue("2021.1.1"), D.Value)))

 

Result:

3.gif

The result collection "coldays" is the table for using. DateValue("2021.1.1") need to be modified to like

DateValue(Concatenate(Text(CurrentYear),".1.1"))

 

Hope this helps.

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

Thank you, that worked perfectly. Now for the issue of a new year or past year. If the current week is less than week1, I decrement the year and start at the total weeks of the previous year? And if it's greater than week52, I increment the year and reset Current week to 1. Correct?

This is just to prevent it from showing week 0, -1 or week 53, 54 etc. I could hide that info but I need it to be visible

Hi @Clxmxnt25 ,

 

I think the approach could be forbid users from decreasing to 0 or more than 52, using an If function:

Left icon:

If(CurrentWeek = 1, Notify("...",NotificationType.Error), UpdateContext({CurrentWeek:CurrentWeek-1}))

Right icon:

If(CurrentWeek = 52, Notify("...",NotificationType.Error),UpdateContext({CurrentWeek:CurrentWeek+1}))

 

Then for new years or past years, force CurrentWeek to be 1 to reset the weeks:

If(CurrentYear != Year(Today()),UpdateContext({CurrentWeek: 1}))

 

Best regards,

Community Support Team _ Jeffer Ni
If this post helps, then please consider Accept it as the solution to help the other members find it.

I ended up doing this instead. This is for the right icon, the left icon is similar but with required changes.

Set(
    totalWeeks,
    Round(
        (DateDiff(
            Date(
                SelectedYear,
                1,
                1
            ),
            Date(
                SelectedYear,
                12,
                31
            )
        ) + 1) / 7,
        0
    )
);
If(
    SelectedWeek >= totalWeeks,
    SelectedYear + 1;
    Set(SelectedWeek, 1),
    Set(SelectedWeek, SelectedWeek+1)
);
Set(SelectedMonth, Month(Date(SelectedYear, 1, 1)+SelectedWeek*7-1))
robinthakur
Frequent Visitor

This so very nearly works for my usecase which is similar but it calculates the dates wrong. This thinks Week 1 was 28/12/2020 - 03/01/2021 but in the UK at least, Week 1 was 04/01/2021-10/01/2021

Could be a timezones thing, maybe? I'm also using Sunday as the start of week when calculating

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (2,088)