cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
New Member

Not able to go to previous week in my TimeSheet App

Hi All,
I was creating a weekly time sheet in which I started week from 1st of every month and ends up with upcoming sunday and than starting from Monday to Sunday week and in last whatever days are left in that month I'm taking those as last week in that month.
means It is something dynamic approach.
for next week on button onselect I'm using this code -

Concat(
    colDayswithinaWeek,
    Trim(Day1) & "," & Trim(Day2) & "," & Trim(Day3) & "," & Trim(Day4) & "," & Trim(Day5) & "," & Trim(Day6) & "," & Trim(Day7)
);
Set(
    vPreviousweekdates,
    colWeekDateforGallery
);
Set(
    vTotalRows,
    CountRows(colWeekDateforGallery) + 1
);
Set(
    vFirstDayofMonth,
    DateAdd(
        DateValue(Last(colWeekDateforGallery).Day1),
        1,
        Days
    )
);
Set(
    vLastdayofWeek,
    If(
        Day(vFirstDayofMonth) + 7 <= Day(vLastDayofMonth),
        Day(vFirstDayofMonth) + 7,
        Day(vLastDayofMonth) + 1
    )
);
Clear(colWeekDateforGallery);
ClearCollect(
    colWeekDateforGallery,
    ForAll(
        Sequence(
            If(
                vLastdayofWeek - Day(vFirstDayofMonth) > 0,
                (vLastdayofWeek - Day(vFirstDayofMonth)),
                0
            ),
            Day(vFirstDayofMonth)
        ),
        Patch(
            colWeekDateforGallery,
            Defaults(colWeekDateforGallery),
            {
                Index: CountRows(colWeekDateforGallery) + 1,
                Day1: Text(
                    DateAdd(
                        Date(
                            Year(vFirstDayofMonth),
                            Month(vFirstDayofMonth),
                            Day(ThisRecord.Value) - 1
                        ),
                        Value,
                        Days
                    ),
                    LongDate
                )
            }
        )
    )
);
ClearCollect(
    colDayswithinaWeek,
    {
        Index: CountRows(colDayswithinaWeek) + 1,
        Project: "",
        Day1: LookUp(
            colWeekDateforGallery,
            Index = "1"
        ).Day1,
        Day2: LookUp(
            colWeekDateforGallery,
            Index = "2"
        ).Day1,
        Day3: LookUp(
            colWeekDateforGallery,
            Index = "3"
        ).Day1,
        Day4: LookUp(
            colWeekDateforGallery,
            Index = "4"
        ).Day1,
        Day5: LookUp(
            colWeekDateforGallery,
            Index = "5"
        ).Day1,
        Day6: LookUp(
            colWeekDateforGallery,
            Index = "6"
        ).Day1,
        Day7: LookUp(
            colWeekDateforGallery,
            Index = "7"
        ).Day1,
        Comments: ""
    }
);
Set(
    vFirstDayofMonth,
    DateAdd(
        DateValue(Last(colWeekDateforGallery).Day1),
        0,
        Days
    )
);


 and on previous button I'm using this code for previous week but my previous week logic is not working as expected.

Concat(
    colDayswithinaWeek,
    Trim(Day1) & "," & Trim(Day2) & "," & Trim(Day3) & "," & Trim(Day4) & "," & Trim(Day5) & "," & Trim(Day6) & "," & Trim(Day7)
);
Set(
    vNextweekdates,
    colWeekDateforGallery
);
Set(
    vTotalRows,
    CountRows(colWeekDateforGallery) + 1
);
Set(
    vFirstDayofMonth,
    Date(
        Year(DateValue(First(colWeekDateforGallery).Day1)),
        Month(DateValue(First(colWeekDateforGallery).Day1)),
        Day(1)
    )
);
Set(
    vFirstDayofPreviousWeek,
    If(
        Day(DateValue(First(colWeekDateforGallery).Day1)) - 7 >= Day(
            Date(
                Year(DateValue(First(colWeekDateforGallery).Day1)),
                Month(DateValue(First(colWeekDateforGallery).Day1)),
                Day(1)
            )
        ),
        (Day(DateValue(First(colWeekDateforGallery).Day1))) - 7,
        Day(1)
    )
);
Set(
    vLastdayofWeek,
    (Day(DateValue(First(colWeekDateforGallery).Day1)) - 1)
);
Clear(colWeekDateforGallery);
ClearCollect(
    colWeekDateforGallery,
    ForAll(
        Sequence(
            If(
                vLastdayofWeek - Day(vFirstDayofPreviousWeek) > 0,
                (vLastdayofWeek - Day(vFirstDayofPreviousWeek)+1),
                0
            ),
            Day(vFirstDayofPreviousWeek)
        ),
        Patch(
            colWeekDateforGallery,
            Defaults(colWeekDateforGallery),
            {
                Index: CountRows(colWeekDateforGallery) + 1,
                Day1: Text(
                    DateAdd(
                        Date(
                            Year(vFirstDayofMonth),
                            Month(vFirstDayofMonth),
                            Day(ThisRecord.Value) - 1
                        ),
                        Value,
                        Days
                    ),
                    LongDate
                )
            }
        )
    )
);
ClearCollect(
    colDayswithinaWeek,
    {
        Index: CountRows(colDayswithinaWeek) + 1,
        Project: "",
        Day1: LookUp(
            colWeekDateforGallery,
            Index = "1"
        ).Day1,
        Day2: LookUp(
            colWeekDateforGallery,
            Index = "2"
        ).Day1,
        Day3: LookUp(
            colWeekDateforGallery,
            Index = "3"
        ).Day1,
        Day4: LookUp(
            colWeekDateforGallery,
            Index = "4"
        ).Day1,
        Day5: LookUp(
            colWeekDateforGallery,
            Index = "5"
        ).Day1,
        Day6: LookUp(
            colWeekDateforGallery,
            Index = "6"
        ).Day1,
        Day7: LookUp(
            colWeekDateforGallery,
            Index = "7"
        ).Day1,
        Comments: ""
    }
);
Set(
    vFirstDayofMonth,
    DateAdd(
        DateValue(Last(colWeekDateforGallery).Day1),
        0,
        Days
    )
);



here is screen shot of that time sheet-

SharePointDev_0-1600224550364.png

Any work around will help me more.

 

1 REPLY 1
Highlighted
Community Support
Community Support

Re: Not able to go to previous week in my TimeSheet App

Hi @SharePointDev ,

Since you do not provided how do you set all the collections, controls, data sources, so I'm afraid I could not directly use the formula that you provided for your feather.

Based on your demands, I made a similar test, please refer my solution:
1)drop down1(for selecting year)

Items:

[2010,2011,2018,2019,2020,...]

2)drop down2(for selecting month)

Items:

[1,2,3,4,5,6,7,8,9,10,11,12]

3)drop down1's OnChange and drop down2's OnChange:

Set(var,1);        //week num variable
Clear(Colqqq);     //clear weeknum collection
ForAll(
    [
        0,
        1,
        2,
        3,
        4
    ],
    Collect(
        Colqqq,
        {
            WeekNum: "week" & Value + 1,
            SD: If(
                Value = 0,
                Date(
                    Dropdown1.Selected.Value,
                   Dropdown2.Selected.Value,
                    1
                ),
                DateAdd(
                    Date(
                        Dropdown1.Selected.Value,
                        Dropdown2.Selected.Value,
                        1
                    ),
                    7 * Value + 2 - Weekday(
                        Date(
                           Dropdown1.Selected.Value,
                            Dropdown2.Selected.Value,
                            1
                        )
                    )
                )
            ),
            ED: If(
                Month(
                    DateAdd(
                        If(
                            Value = 0,
                            Date(
                                Dropdown1.Selected.Value,
                                Dropdown2.Selected.Value,
                                1
                            ),
                            DateAdd(
                                Date(
                                   Dropdown1.Selected.Value,
                                    Dropdown2.Selected.Value,
                                    1
                                ),
                                7 * Value + 2 - Weekday(
                                    Date(
                                        Dropdown1.Selected.Value,
                                        Dropdown2.Selected.Value,
                                        1
                                    )
                                )
                            )
                        ),
                        8 - Weekday(
                            If(
                                Value = 0,
                                Date(
                                    Dropdown1.Selected.Value,
                                    Dropdown2.Selected.Value,
                                    1
                                ),
                                DateAdd(
                                    Date(
                                        Dropdown1.Selected.Value,
                                       Dropdown2.Selected.Value,
                                        1
                                    ),
                                    7 * Value + 2 - Weekday(
                                        Date(
                                            Dropdown1.Selected.Value,
                                            Dropdown2.Selected.Value,
                                            1
                                        )
                                    )
                                )
                            )
                        ),
                        Days
                    )
                ) > Dropdown2.Selected.Value,
                DateAdd(
                    DateAdd(
                        Date(
                            Dropdown1.Selected.Value,
                           Dropdown2.Selected.Value,
                            1
                        ),
                        1,
                        Months
                    ),
                    -1,
                    Days
                ),
                DateAdd(
                    If(
                        Value = 0,
                        Date(
                            Dropdown1.Selected.Value,
                            Dropdown2.Selected.Value,
                            1
                        ),
                        DateAdd(
                            Date(
                                Dropdown1.Selected.Value,
                                Dropdown2.Selected.Value,
                                1
                            ),
                            7 * Value + 2 - Weekday(
                                Date(
                                   Dropdown1.Selected.Value,
                                    Dropdown2.Selected.Value,
                                    1
                                )
                            )
                        )
                    ),
                    8 - Weekday(
                        If(
                            Value = 0,
                            Date(
                                Dropdown1.Selected.Value,
                                Dropdown2.Selected.Value,
                                1
                            ),
                            DateAdd(
                                Date(
                                    Dropdown1.Selected.Value,
                                    Dropdown2.Selected.Value,
                                    1
                                ),
                                7 * Value + 2 - Weekday(
                                    Date(
                                        Dropdown1.Selected.Value,
                                        Dropdown2.Selected.Value,
                                        1
                                    )
                                )
                            )
                        )
                    ),
                    Days
                )
            )
        }
    )
); 
//collection with starting date and ending date
ClearCollect(dates,
AddColumns(Colqqq,
"mon",If(Weekday(SD)=2,SD,DateAdd(SD,2-Weekday(SD))),
"tue",If(Weekday(SD)=3,SD,Weekday(DateAdd(SD,1))=3,DateAdd(SD,1),DateAdd(SD,3-Weekday(SD))),
"wed",If(Weekday(SD)=4,SD,Weekday(DateAdd(SD,2))=4,DateAdd(SD,2),DateAdd(SD,4-Weekday(SD))),
"thu",If(Weekday(SD)=5,SD,Weekday(DateAdd(SD,3))=5,DateAdd(SD,3),DateAdd(SD,5-Weekday(SD))),
"fir",If(Weekday(SD)=6,SD,Weekday(DateAdd(SD,4))=6,DateAdd(SD,4),DateAdd(SD,6-Weekday(SD))),
"sat",If(Weekday(SD)=7,SD,Weekday(DateAdd(SD,4))=7,DateAdd(SD,4),DateAdd(SD,7-Weekday(SD))),
"sun",If(Weekday(SD)=1,SD,Weekday(DateAdd(SD,6))=1,DateAdd(SD,6),DateAdd(SD,8-Weekday(SD)))
            )
);
//collection with detailed dates and weekday
UpdateIf(dates,Not(Month(mon)=Dropdown2.Selected.Value),{mon:Blank()});
UpdateIf(dates,Not(Month(tue)=Dropdown2.Selected.Value),{tue:Blank()});
UpdateIf(dates,Not(Month(wed)=Dropdown2.Selected.Value),{wed:Blank()});
UpdateIf(dates,Not(Month(thu)=Dropdown2.Selected.Value),{thu:Blank()});
UpdateIf(dates,Not(Month(fir)=Dropdown2.Selected.Value),{fir:Blank()});
UpdateIf(dates,Not(Month(sat)=Dropdown2.Selected.Value),{sat:Blank()});
UpdateIf(dates,Not(Month(sun)=Dropdown2.Selected.Value),{sun:Blank()})
//clear all dates that are not belong to selected month

collection1 looks like this:
9169.PNG

 

collection2 looks like this:

9160.PNG

 

4)insert a data table to display dates

set data table's Items:

Filter(dates,Text(var) in WeekNum)

5)insert two buttons to change week num 

set "<" button's OnSelect:

Set(var,var-1)

set "<" button's Visible:

var>1

set ">" button's OnSelect:

 

Set(var,var+1)

 

set ">" button's Visible:

 

var<5

 

6)set the screen's OnVisible:

Set(var,1)

916.gif

 

 

Best regards,

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
secondImage

New Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Users online (4,364)