While working with the Power Apps gallery, you might required to sort a gallery by month in Power Apps. In this Power Apps tutorial, I will explain how to sort a Power Apps gallery by month. Also, we will see how to sort a Power Apps gallery by the current month and by next month.
We will cover:
- Sort Power Apps Gallery By Next ‘N’ Months
- How to Sort Power Apps Gallery By Previous Month
- Working with Power Apps sort Gallery By Last ‘N’ Months
Power Apps Sort Gallery By Month
I will show you now how to sort a Power Apps gallery by month with examples related to SharePoint.
Let’s take a simple scenario: I have a SharePoint Online list, i.e., [Company Attachments]. This list contains the below fields.
Column Name | Data Type |
ID | Default number column |
Title | Default single line of text |
Attachment Type | Choice |
Attachment Cost | Currency |
Attachment Created Date | Date and Time |
In Power Apps, a Dropdown menu filters a gallery by MONTH, and it sorts the gallery based on the “Attachment Created Date”.
Refer to the below screen:
To achieve it, follow the steps below.
1. Create a Blank Canvas app and connect it to the respective SharePoint Online list -> Select the App object [from left navigation] and set its OnStart property as:
OnStart = ClearCollect(collMonths, Table(
{ ID:1, Name:"Jan"}, { ID:2, Name:"Feb"}, { ID:3, Name:"Mar"},
{ ID:4, Name:"Apr"}, { ID:5, Name:"May"}, { ID:6, Name:"Jun"},
{ ID:7, Name:"Jul"}, { ID:8, Name:"Aug"}, { ID:9, Name:"Sep"},
{ ID:10, Name:"Oct"}, { ID:11, Name:"Nov"}, { ID:12, Name:"Dec"}
));
Where,
- collMonths = Power Apps Collection name
- ID, Name = These are the headers of that specific collection
- “1“, “2“, and so on = These are the records that will be stored under the ID column
- “JAN“, “FEB“, and so on = These are the records that will store under the Name column
2. Then, click on the Apps’s Run OnStart button to get the collection as shown below.
3. Next, on the Power Apps Screen -> Insert a Dropdown control and set its Items property as:
Items = collMonths
4. Then, insert a Gallery control and set its Items property to the code below.
Items = SortByColumns(
Filter(
'Company Attachments',
Month('Attachment Created Date') = drp_Month.Selected.ID
),
"AttachmentCreatedDate",
SortOrder.Ascending
)
Where,
- ‘Company Attachments’ = SharePoint list Date and time field
- drp_Month = Power Apps Dropdown name
5. Save, Publish, and Review the app. The Power Apps gallery will sort and filter each record from the SharePoint list based on the dropdown selected month as in the screenshot below.
This is how we can work with Power Apps Sort Gallery by Month.
Power Apps Sort Gallery By Current Month
Here, we will see how to sort a Power Apps gallery by the current month with a simple example.
Example:
I have a SharePoint Online List named “Training Courses Demos“. This list contains the below fields with various data types. Such as:
- Course Name = Default single line of text
- Description = Multiple lines of text
- Demo Start Date = Date and time
- Demo End Date = Date and time
In Power Apps, there is a Gallery control. This gallery sorts and displays each record from the SharePoint list based on the current month.
To work around this example, follow the below steps.
1. On the Power Apps Screen -> Insert a Gallery control [gal_CurrentMonthDemos] and set its Items property to the code below.
Items = With(
{
StartDate: Date(
Year(Today()),
Month(Today()),
1
),
EndDate: Date(
Year(Today()),
Month(Today()) + 1,
1
) - 1
},
SortByColumns(
Filter(
'Training Courses Demos',
'Demo Start Date' >= StartDate,
'Demo End Date' <= EndDate
),
"DemoStartDate",
SortOrder.Ascending
)
)
Where,
- With() = This function can help us to evaluate a formula for a single record
- StartDate, EndDate = Power Apps Scope Variables
- Today() = Power Apps Today() helps to get the current date
- Year(Today()) = This function returns the year component of a Date/Time value
- Month(Today()) = This function returns the month component of a Date/Time value
- ‘Training Courses Demos’ = SharePoint Online List
- “DemoStartDate = SharePoint Date and time field
2. Save, Publish, and Preview the app. The gallery will display sorted records based on the current month, as in the screenshot below.
This is how to sort a Power Apps gallery by current month.
Power Apps Sort Gallery By Next Month
Next, we will discuss how to sort a Power Apps gallery by next month.
Example:
I will also take the same SharePoint Online list [Training Courses Demos] for this example. In Power Apps, there is a Horizontal gallery control. This gallery sorts and displays each record from the SharePoint list based on the next or upcoming month.
Refer to the below image:
To do so, follow the below steps.
1. On the Power Apps Screen -> Insert a Gallery control and set its Items property as:
Items = With(
{
StartDate: Date(
Year(Today()),
Month(Today()) + 1,
1
),
EndDate: Date(
Year(Today()),
Month(Today()) + 2,
1
) - 1
},
Sort(
Filter(
'Training Courses Demos',
'Demo Start Date' >= StartDate,
'Demo End Date' <= EndDate
),
'Demo Start Date',
SortOrder.Ascending
)
)
Where,
- Month(Today())+1 = Power Apps Month() helps to get the current month, and “+1” is for next month
2. Save, Publish, and Preview the app. The gallery will display the sorted records, i.e., [Next Month Records], as shown below.
This is how to sort a Power Apps gallery by next month.
Power Apps Sort Gallery By Previous Month
I will show you how to sort a Power Apps gallery control by the previous month.
Example:
I will also use the above SharePoint list as an example. Now, I want to sort the Power Apps gallery by the previous or last month. I have used the code below in the Gallery’s Items property.
Items = With(
{
StartDate: Date(
Year(Today()),
Month(Today()) - 1,
1
),
EndDate: Date(
Year(Today()),
Month(Today()),
1
) - 1
},
SortByColumns(
Filter(
'Training Courses Demos',
'Demo Start Date' >= StartDate,
'Demo End Date' <= EndDate
),
"DemoStartDate",
SortOrder.Descending
)
)
Where,
- Month(Today())-1 = This function returns the month component of a Date/Time value, and ‘1’ is used for the previous/last month
Then, Save, Publish, and Preview the app. This gallery will display the sorted records [Descensing order] based on the previous month, as in the screenshot below.
This is how to sort a Power Apps gallery by last/previous month.
Power Apps Sort Gallery By Previous ‘N’ Months
Similarly, we will discuss how to sort a Power Apps gallery by the previous 2 months. Here, I will take the same SharePoint list for this example.
Now, I want to sort the Power Apps Gallery control by the last 2 months. To do so, use the below code to set Gallery’s Items property.
Items = With(
{
StartDate: Date(
Year(Today()),
Month(Today()) - 2,
Day(Today())
) + 1,
EndDate: Date(
Year(Today()),
Month(Today()),
Day(Today())
)
},
Sort(
Filter(
'Training Courses Demos',
'Demo Start Date' >= StartDate,
'Demo End Date' <= EndDate
),
'Demo Start Date',
SortOrder.Ascending
)
)
Where,
Month(Today())-2 = This function returns the month component of a Date/Time value, and ‘2’ is the number of previous months // You can change the number of months
Next, Save, Publish, and Preview the app. This gallery will display the sorted records [Ascending Order] based on the last 2 months’ records.
This is how to sort a Power Apps gallery by the previous ‘N’ months.
Power Apps Sort Gallery By Next ‘N’ Months
In the last, we will see how to sort a Power Apps gallery by the next 3 months.
Example:
I will also take the above SharePoint online list for this example. Now, I will show you how to sort the Power Apps gallery by the next 3 months. For that, set the Items property of a Gallery as shown below.
Items = With(
{
StartDate: Date(
Year(Today()),
Month(Today()),
Day(Today())
),
EndDate: Date(
Year(Today()),
Month(Today()) + 3,
Day(Today())
) - 1
},
SortByColumns(
Filter(
'Training Courses Demos',
'Demo Start Date' >= StartDate,
'Demo End Date' <= EndDate
),
"DemoStartDate",
SortOrder.Ascending
)
)
Where,
Month(Today())+3 = This function returns the month component of a Date/Time value, and ‘3’ is the number of next months // You can change the number of months
Once your app is ready, save, publish, and preview it. The Power Apps gallery will sort and display the records based on the next three months, as shown below.
This is all about the Power Apps sort gallery by the upcoming 3 months.
Conclusion
This Microsoft Power Apps tutorial taught us all about the Power Apps sort gallery by month.
Here, I explained how to sort a Power Apps gallery by the current month and how to sort a Power Apps gallery by next month.
Also, we discussed the Power Apps sort galley by last month and how to sort a Power Apps gallery by the previous/last ‘N’ months. Last, we covered how to sort a Power Apps gallery by the next/upcoming ‘N’ months.
You may also like:
- How to Select Multiple Items in Power Apps Gallery Control?
- How to Add New Row in Power Apps Gallery Control?
- How to Sort Gallery by Person Column in Power Apps?
- How to Sort Power Apps Gallery By ID?
- Power Apps Filter Gallery By Quarter
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com