Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in NYC Sept 4th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
kunkumnaveen
Specialist
Specialist

How to write an expression for below requirement

Hello All,

I need to design a bar graph with Dimension (B_Mode)and with a expression which accumulate last three months  avg(Count_Br ) excluding the selected month 

For Example :i have max data till 2019 Oct.

if no month and year is selected it has to  then take the avg of past three months(Sep,Avg,Jul)

if any month is selected (2019,Feb) then it should take avg of  the past three months(2019Jan,2018Dec,2018,Nov)..

Chart:Bar Chart

Dimension: B_Mode

Expression: Avg({   ?   }Count_Br)

 

Thank

 

Please find the attached sample data...

 

2 Solutions

Accepted Solutions
Kushal_Chawda

try below script

 

avg({<Year,M_Month, Date = {">=$(=monthstart(addmonths(max(Date),-3)))<=$(=monthend(addmonths(max(Date),-1)))"}>}Count_Br)

 

View solution in original post

Kushal_Chawda

this should work

=alt(avg({<Year,M_Month, Date = {">=$(=monthstart(addmonths(max(Date),-3)))<=$(=monthend(addmonths(max(Date),-1)))"}>}
aggr(sum({<Year,M_Month, Date = {">=$(=monthstart(addmonths(max(Date),-3)))<=$(=monthend(addmonths(max(Date),-1)))"}>}Count_Br), 
Year, M_Month)
),0)

View solution in original post

21 Replies
Taoufiq_Zarra

"if any month is selected (2019,Feb) then it should take avg of the past three months(2019Jan,2018Dec,2018,Nov"

not clear ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
kunkumnaveen
Specialist
Specialist
Author

Hi ,

I mean if a year 2019 and month Feb is selected from year and month  filters.

What is the AVG of past three months is:

AVG of past three months are values of 

2019 Jan

2018 Dec

2018 Nov 

should bypass the year selection 

Anil_Babu_Samineni

Perhaps this?

Avg({<Month={">=$(=AddYears(Max({<Year>}Month),-3))<=$(=Max({<Year>}Month))"}>} Sales)

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
kunkumnaveen
Specialist
Specialist
Author

Hi Anil,

I have tried your expression, but it not working ..something is missing in below bold set analysis 

Avg({<Month={">=$(=AddYears(Max({<Year>}Month),-3))<=$(=Max({<Year>}Month))"}>} Sales)

kunkumnaveen
Specialist
Specialist
Author

Hello All,

Any suggestion for the above requirement ...

 

Thanks

 

Anil_Babu_Samineni

My bad, Should be

Avg({<Month={">=$(=AddMonthsAddYears(Max({<Year>}Month),-3))<=$(=Max({<Year>}Month))"}>} Sales)

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
kunkumnaveen
Specialist
Specialist
Author

I have done a change still its not working

Avg({<Month={">=$(=Date(AddMonths(Max({<Year>}Month),-3),'MMM')<=$(=Max({<Year>}Month))"}>}sales)

Taoufiq_Zarra

@Anil_Babu_Samineni's function works very well

=Avg({<Month={">=$(=AddMonths(Max({<Year>}Month),-3))<=$(=Max({<Year>}Month))"}>} Sales)

why are you adding Date() ?

if not can you share your Qlik file ?

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
kunkumnaveen
Specialist
Specialist
Author

Hi,

Sorry to say but i am not allowed to share it,but i have attached a sample data file.