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
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (2,930)