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

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
mansi_dv
Contributor
Contributor

Decrement Month dynamically

This is with reference to

http://community.qlik.com/message/494522

Now I am able to do calculations as per the user selections. But what if I want a month which is one more or one less than the selected month?

For example, user selected may. I fetched the input using maxstring() function. Now I want the data of April (i.e. the previous month of the selected one) to do further calculations for the same selection. What should be done?

6 Replies
tresesco
MVP
MVP

try like:

=sum({<[Month] = {'$(vLastMonth)'},[Year] = {'$(=maxstring({[new]}[Year]))'}>}Data)

define variable like:

vLastMonth= Month(Addmonths(Date#('1'&maxstring({[new]} [Month]) &'2000' , 'DMMMYYYY' ) ,-1) )

Not applicable

Hi,

For this Your model contains calender fields

Script to generate calender

 

Calender:

Load Date(TempDate,'DD/MM/YYYY') AS Date,
Year(TempDate) AS Year,
Month(TempDate) AS Month,
NUM(Month(TempDate)) AS MonthNum,
If(Match(NUM(Month(TempDate)),1,2,3),'Q1',
If(Match(NUM(Month(TempDate)),4,5,6),'Q2',
If(Match(NUM(Month(TempDate)),7,8,9),'Q3',
If(Match(NUM(Month(TempDate)),10,11,12),'Q4')))) AS Quarter,
Day(TempDate) AS Day,

YearName(TempDate,0,4) AS FinYearName,
Year(AddMonths(TempDate,9)) AS FinYear,
Month(TempDate) AS FinMonth,
Num(Month(AddMonths(TempDate,9))) AS FinMonthNum,
If(Match(NUM(Month(AddMonths(TempDate,9))),1,2,3),'Q1',
If(Match(NUM(Month(AddMonths(TempDate,9))),4,5,6),'Q2',
If(Match(NUM(Month(AddMonths(TempDate,9))),7,8,9),'Q3',
If(Match(NUM(Month(AddMonths(TempDate,9))),10,11,12),'Q4')))) AS FinQuarter
Resident YourDataTable;


Create variables like vCurrentYear,vCurrentMonth and vPrevMonth

The followin are the expressions for those corresponding variables  

=Max(FinYear)

=Max(IF(FinYear=$(vCurrentYear),FinMonthNum))

=$(vCurrentMonth)-1

Now Your Chart Expression for previous month sales would be

=Sum({<FinYear={$(vCurrentYear)},FinMonthNum={$(vPrevMonth)}>}Sales)

mansi_dv
Contributor
Contributor
Author

Thanx Tresesco. But I am getting 0 as output.

tresesco
MVP
MVP

Check the variable value. Is it calculating the last month properly?

mansi_dv
Contributor
Contributor
Author

variable is also giving null data.

tresesco
MVP
MVP

Probably your variable declaration has error. Check it, else if possible try to upload your sample qvw.