Year

How to Use the Power Fx Year  Function in Power Apps

In this article, we will explore how to use the Power Fx Year function in Power Apps and provide examples of how it can be used in different scenarios.

The Syntax of the Year Function

The Year function in Power Fx has a simple syntax. It takes a date/time value as its parameter and returns the year as a number. Here is the syntax of the Year function:


Year(dateValue)


Where `dateValue` is the date/time value from which you want to extract the year.

Using the Year Function in Power Apps

The Year function can be used in several ways in Power Apps. Here are some examples:

Example 1: Grouping Data by Year

Suppose you have a collection of sales data with a date field, and you want to group the data by year to see the total sales for each year. You can use the Year function to extract the year from the date field and then group the data by year using the GroupBy function.

Here is an example formula that uses the Year and GroupBy functions:


GroupBy(SalesData, Year(DateField), “GroupedData”)


Where `SalesData` is the name of your collection, `DateField` is the name of the date field in the collection, and "GroupedData" is the name of the new collection that will contain the grouped data.

Example 2: Filtering Data by Year

Suppose you want to filter the sales data to show only the data for a specific year. You can use the Year function to extract the year from the date field and then use the Filter function to filter the data based on the year.

Here is an example formula that uses the Year and Filter functions:


Filter(SalesData, Year(DateField) = 2021)


Where `SalesData` is the name of your collection, `DateField` is the name of the date field in the collection, and 2021 is the year you want to filter by.

Example 3: Calculating the Age

Suppose you have a date of birth field in your data and you want to calculate the age of the person based on the current date. You can use the Year function to extract the year from the date of birth field and then subtract it from the current year.

Here is an example formula that uses the Year and Today functions to calculate the age:


Today().Year – Year(DateOfBirth)


Where `DateOfBirth` is the name of the date of birth field in your data.

The Year function in Power Fx is a powerful tool that allows you to extract the year from a date/time value. It can be used in a variety of scenarios, such as grouping data by year, filtering data by year, and calculating the age. By leveraging the Year function, you can create more powerful and efficient business applications in Power Apps.

Power Apps Training Courses by G Com Solutions (0800 998 9248)