Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Shahzad_Ahsan
Creator III
Creator III

How to calculate Avg on behalf of Current year and Last Year

Hi

I need to calculate Avg on Behalf of CY and LY like in below Table.

YearAmountAverage
201652410
201751485194.5
201836584403
201974155536.5

 

The Avg of year 2017 is Sum of Amount of 2017 and 2016 divide by 2.

Labels (1)
1 Solution

Accepted Solutions
jonathandienst
Partner - Champion III
Partner - Champion III

You could try this:

Alt((Sum(Amount) + Above(Sum(Amount)))/2, 0)


If you have more than one dimension, you need to add TOTAL to the Above() clause:

Alt((Sum(Amount) + Above(TOTAL Sum(Amount)))/2, 0)

The Alt() will cause the first value to be zero.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein

View solution in original post

7 Replies
madhu_r
Partner - Contributor III
Partner - Contributor III

Hi,

Try this expression in script editor

If(len(Previous(Year))=0,0,(Amount+Previous(Amount))/2) as Average

 

miskinmaz
Creator III
Creator III

Hi,

You can try below expression:

if(rowno()<>1,
rangesum(above(Sum(Sales),0,2))/2)

Shahzad_Ahsan
Creator III
Creator III
Author

Hi @madhu_r 

Your solution is working fine for this sample data. But for live data it is not working.

My fact table has Transaction_Date, Amount any many other fields and has millions of rows.

 

madhu_r
Partner - Contributor III
Partner - Contributor III

Hi Shahzad_Ahsan,

Can you share the sample of your live data

jonathandienst
Partner - Champion III
Partner - Champion III

You could try this:

Alt((Sum(Amount) + Above(Sum(Amount)))/2, 0)


If you have more than one dimension, you need to add TOTAL to the Above() clause:

Alt((Sum(Amount) + Above(TOTAL Sum(Amount)))/2, 0)

The Alt() will cause the first value to be zero.

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Shahzad_Ahsan
Creator III
Creator III
Author

Hi @jonathandienst 

Thanks for your response. 

This is working fine. 

But I have one more question related to this.

Your expression is working fine in a Table. Suppose I have to show the current year avg as a KPI. then how is this possible??

jonathandienst
Partner - Champion III
Partner - Champion III

Please post this question as a new topic. You can refer back to this topic using the URL:

https://community.qlik.com/t5/forums/editpage/board-id/new-to-qlik-sense/message-id/133525

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein