Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dc7669
Helper II
Helper II

How to summary data into current year and compared to previous 5-year in PowerBI Table?

Hi, 

 

I have a table like this: 

 

CYNumber
20212768
20203355
20193762
20182857
20173521
20162711
20153323
20143877
20133546
20123891
20115227

 

I would like to report the above as a PowerBI Report.  I would like the user to be able to choose the Year to compare with and show the average of the previous 5 years.  

CYNumber
20212726
2016-20203241 (Averages of 2016-2020)

 

Any idea on how I can achieve this? 

 

One idea I got is to "Transform" the table into this format... but, then, how can I put the "correct" number in each cell using Power Query?    

CYNumberCY-1CY-2CY-3CY-4CY-5
20212768     
20203355     
20193762     
20182857     

 

Any suggestions or pointers? 

 

Thanks! 

 

dchan

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @dc7669 

 

If you want to calculate the average of previous 5 years in Power Query Editor directly, you can enter below step mode in the formula bar. This will add a new column which returns the average numbers. Change previous step name according to your query. In my example, the previous step is named as "Changed Type".

= Table.AddColumn(#"Changed Type", "Average of Previous 5 Years", each List.Average(Table.SelectRows(#"Changed Type",(x)=> x[CY]>=[CY]-5 and x[CY]<[CY])[Number]))

082604.jpg

 

Another method is to create a measure in the report.

Average = AVERAGEX(FILTER(ALL('Table'),'Table'[CY] >= SELECTEDVALUE('Table'[CY])-5 &&'Table'[CY] < SELECTEDVALUE('Table'[CY])),'Table'[Number])

082605.jpg

 

From above screenshot, you can see that both methods get the average results. I didn't format them so the digits after decimal point are slightly different. You can format the results then. If you want users to select a year, you can add a slicer into the report and drag CY column into it for users to pick a year from.

 

Download the attachment for details.

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

View solution in original post

4 REPLIES 4
dc7669
Helper II
Helper II

I finally figure out how to do this (Getting an average from previous 5 years) in Power Query formula.

1. Open up Power Query Editor by clicking on "Transform Data" in PowerBI Desktop

2. In Power Query Editor, Select the table of concern

3. Select "Add Column - Custom Column" at the end of the "Applied Steps" of that table.

4. The default "New Column name" is "Custom".  Change it to whatever you want.  I used "Pre5yrNum"

5. In the "Custom Column formula" section, type in:

 List.Average(
Table.SelectRows(
#"Source",
(C) => C[CY] < [CY] and (C[CY]>=([CY]-5))  
)[Number]
)

 

The meaning of this is to do a List Average from a list of [Number] where CY is less than the current row CY, but more than (current CY - 5). 

#"Source" is the name of the step before this step.   

 

I think the advantage of using Power Query is that if I were to select based on more than CY, but also, Region or other fields, they can show up in a graph in Power BI.  

 

dc7669

v-jingzhang
Community Support
Community Support

Hi @dc7669 

 

If you want to calculate the average of previous 5 years in Power Query Editor directly, you can enter below step mode in the formula bar. This will add a new column which returns the average numbers. Change previous step name according to your query. In my example, the previous step is named as "Changed Type".

= Table.AddColumn(#"Changed Type", "Average of Previous 5 Years", each List.Average(Table.SelectRows(#"Changed Type",(x)=> x[CY]>=[CY]-5 and x[CY]<[CY])[Number]))

082604.jpg

 

Another method is to create a measure in the report.

Average = AVERAGEX(FILTER(ALL('Table'),'Table'[CY] >= SELECTEDVALUE('Table'[CY])-5 &&'Table'[CY] < SELECTEDVALUE('Table'[CY])),'Table'[Number])

082605.jpg

 

From above screenshot, you can see that both methods get the average results. I didn't format them so the digits after decimal point are slightly different. You can format the results then. If you want users to select a year, you can add a slicer into the report and drag CY column into it for users to pick a year from.

 

Download the attachment for details.

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

Syndicate_Admin
Administrator
Administrator

Hi @dc7669 

 

You want users to choose the Year to compare, so it is a slicer, it is a more DAX question to me. If you can use this format, Last 5 Year and AVG are all measures -  I did in Excel, you can adjust in Power BI

 

Vera_33_0-1629766999473.png

Last 5 Year:=
VAR CurYear =IF(HASONEVALUE(Table1[CY]),VALUES(Table1[CY]),MAX(Table1[CY]))
VAR MinYear =MINX(ALL(Table1[CY]),Table1[CY])
VAR StartYear = IF(CurYear-5>MinYear,CurYear-5,MinYear)
RETURN
IF(CurYear=MinYear,CurYear,StartYear&"-"&CurYear-1)

AVG_NUMBER:=
VAR CurYear =IF(HASONEVALUE(Table1[CY]),VALUES(Table1[CY]),MAX(Table1[CY]))
VAR MinYear =MINX(ALL(Table1[CY]),Table1[CY])
VAR StartYear = IF(CurYear-5>MinYear,CurYear-5,MinYear)
RETURN
AVERAGEX(FILTER(ALL(Table1),Table1[CY]>=StartYear&&Table1[CY]<CurYear),Table1[Number])

 

Vera_33
Resident Rockstar
Resident Rockstar

Hi @dc7669 

 

You want users to choose the Year to compare, so it is a slicer, it is a more DAX question to me. If you can use this format, Last 5 Year and AVG are all measures -  I did in Excel, you can adjust in Power BI

 

Vera_33_0-1629766999473.png

Last 5 Year:=
VAR CurYear =IF(HASONEVALUE(Table1[CY]),VALUES(Table1[CY]),MAX(Table1[CY]))
VAR MinYear =MINX(ALL(Table1[CY]),Table1[CY])
VAR StartYear = IF(CurYear-5>MinYear,CurYear-5,MinYear)
RETURN
IF(CurYear=MinYear,CurYear,StartYear&"-"&CurYear-1)

AVG_NUMBER:=
VAR CurYear =IF(HASONEVALUE(Table1[CY]),VALUES(Table1[CY]),MAX(Table1[CY]))
VAR MinYear =MINX(ALL(Table1[CY]),Table1[CY])
VAR StartYear = IF(CurYear-5>MinYear,CurYear-5,MinYear)
RETURN
AVERAGEX(FILTER(ALL(Table1),Table1[CY]>=StartYear&&Table1[CY]<CurYear),Table1[Number])

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors