cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
UB400
Level 10

Loops + Date

I'm building an "Event" App, and in this App, I need to show the Agenda for each day. I would like to be able to add "Buttons" in a row at the top of the page for each day (one "Button" for each day). The OnSelect value for the button will show the Agenda items for the day in a Vertical text Gallery using Filter.

 

At the time of creating the "Event", I want the "Event" Creator, to simply select the Start date and End date for the event, and I would like to create a Table  that will Have Columns such as "Day" (values  for the rows in this column would be "Day 1", "Day 2" etc. The next column would have the date value. So the the table would look like

 

Day          Date

Day1       27-02-2017

Day2       28-02-2017

 

Using this table I can then use Date calculations to determine if the Current Date = "Day" so that i can automatically select the correct "Agenda" Items to display in the gallery on the page.

 

So essentially, I can calculate (EndDate - StartDate)+1 to get number of days including the Start Date, I then need put them into a Collection / Table with Two columns as described above, I can then patch them into the DataSource.

 

Where I'm getting un-stuck is iterating through the number of days and capturing them into the Collection.

 

Any help would be greatly appreciated. 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Loops + Date

PowerApps doesn't have the concept of loops at this point - but you can use a trick with the FirstN function to create such "loop". Here's an app that I'll use to show that:

ForumPost-001.png

The app has four main controls: a couple of date pickers (for the date range), a text box showing the number of days, and a gallery that shows all days within those days.

 

The textbox is what you said that you already have calculated:

TextBox1.Text: "Days: " & (DateDiff(DatePicker1.SelectedDate, DatePicker2.SelectedDate, Days) + 1)

Now, for the gallery, we need to create a table with as many items as the number calculated there. That's when the FirstN function comes into play - we take the first N items of a constant array of increasing values, and that gives us an "index" that can be used to create new columns. In the example below the maximum number of days that it can use is 30, but you can easily extend it to cover a wider range.

Gallery1.Items:
    AddColumns(
        FirstN(
            [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29],
            DateDiff(DatePicker1.SelectedDate, DatePicker2.SelectedDate, Days) + 1),
        "Day",
            "Day " & (Value + 1),
        "Date",
            DateAdd(DatePicker1.SelectedDate, Value, Days))

Now the gallery has a table with three columns: "Value" (which you can use or not), "Day" (the string value "Day <day number>") and "Date" (the actual date in your range).

 

Hope this helps. Also, feel free to create a new item in the PowerApps Ideas page if you think that we should add more "native" support for this scenario.

View solution in original post

3 REPLIES 3
PowerApps Staff CarlosFigueira
PowerApps Staff

Re: Loops + Date

PowerApps doesn't have the concept of loops at this point - but you can use a trick with the FirstN function to create such "loop". Here's an app that I'll use to show that:

ForumPost-001.png

The app has four main controls: a couple of date pickers (for the date range), a text box showing the number of days, and a gallery that shows all days within those days.

 

The textbox is what you said that you already have calculated:

TextBox1.Text: "Days: " & (DateDiff(DatePicker1.SelectedDate, DatePicker2.SelectedDate, Days) + 1)

Now, for the gallery, we need to create a table with as many items as the number calculated there. That's when the FirstN function comes into play - we take the first N items of a constant array of increasing values, and that gives us an "index" that can be used to create new columns. In the example below the maximum number of days that it can use is 30, but you can easily extend it to cover a wider range.

Gallery1.Items:
    AddColumns(
        FirstN(
            [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29],
            DateDiff(DatePicker1.SelectedDate, DatePicker2.SelectedDate, Days) + 1),
        "Day",
            "Day " & (Value + 1),
        "Date",
            DateAdd(DatePicker1.SelectedDate, Value, Days))

Now the gallery has a table with three columns: "Value" (which you can use or not), "Day" (the string value "Day <day number>") and "Date" (the actual date in your range).

 

Hope this helps. Also, feel free to create a new item in the PowerApps Ideas page if you think that we should add more "native" support for this scenario.

View solution in original post

UB400
Level 10

Re: Loops + Date

@CarlosFigueira thank you so much. That works for me. It's these "tricks" that make the difference between an "average" App and a "great" App.Now I can have great navigation in my App, thanks to you.

 

I'll come back and make a Post in the Ideas section, for now this work around does the trick for me. 

Highlighted
AmitSaraswat
Level: Powered On

Re: Loops + Date

start date and end date fix for 29 days - can we have the dynamic start date and end date selection and calculated the weekend (sat +sun) calculated as 1(one) weekend.

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors (Last 30 Days)
Users online (5,447)