Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
dching
Contributor II
Contributor II

Dynamic Previous Month based on selected filter

Hi Guys - i am new to qlik sense - i have the same issue - i want previous month KPI to be based on filtered selection. e.g. if i select June i want previous month(May)figures to show - can someone please help ??    

1 Solution

Accepted Solutions
RafaelBarrios
Partner - Specialist
Partner - Specialist

Hi @dching 

there is many ways of doing this, you can use a master calendar by creating a column for previous month.

or the one i liked more  its using variables, It may seem complicated but they are variables that every project must have

so, for current or selected month

vMaxCurrentDate = date(max(DateField))
vMinCurrentDate = monthstart(date(max(DateField)))

and for previous

vMaxPrevDate = addmonths(vMaxCurrentDate,-1)
vMinPrevDate = addmonths(vMinCurrentDate ,-1)

use the equal sign when defining the expressions

 

and now in your KPIs, you can set first as:

Sum({< year,month,DateField={">=$(vMinCurrentDate )<=$(vMaxCurrentDate )"} >} Sales)

and for second

Sum({< year,month,DateField={">=$(vMinPrevDate )<=$(vMaxPrevDate )"} >} Sales)

then "year,month," in the formula is to ignore the selected year and/or month and solve everything with dates

this will work when you selected a months or not.

 

hope it works for you.

Best,

View solution in original post

5 Replies
pcv_devo
Partner - Contributor III
Partner - Contributor III

Hi Dching,

You will likely have to use set analysis on your measure to get the previous month. Could you describe your KPI in detail? So we can help you more precisely.

RafaelBarrios
Partner - Specialist
Partner - Specialist

Hi @dching 

there is many ways of doing this, you can use a master calendar by creating a column for previous month.

or the one i liked more  its using variables, It may seem complicated but they are variables that every project must have

so, for current or selected month

vMaxCurrentDate = date(max(DateField))
vMinCurrentDate = monthstart(date(max(DateField)))

and for previous

vMaxPrevDate = addmonths(vMaxCurrentDate,-1)
vMinPrevDate = addmonths(vMinCurrentDate ,-1)

use the equal sign when defining the expressions

 

and now in your KPIs, you can set first as:

Sum({< year,month,DateField={">=$(vMinCurrentDate )<=$(vMaxCurrentDate )"} >} Sales)

and for second

Sum({< year,month,DateField={">=$(vMinPrevDate )<=$(vMaxPrevDate )"} >} Sales)

then "year,month," in the formula is to ignore the selected year and/or month and solve everything with dates

this will work when you selected a months or not.

 

hope it works for you.

Best,

dching
Contributor II
Contributor II
Author

Hi thanks for this it works very well just that when opening the App its showing the total sales instead - wanted it to default to current month to date and previous month - is there any way to achieve this ?

RafaelBarrios
Partner - Specialist
Partner - Specialist

Hi @dching 

im glad it works for you !!!

i should be working as you need.

try to look the value of the variable in the var panel

RafaelBarrios_1-1664879890750.png

 


or take a look to what the expression is doing when you dont have any selection

RafaelBarrios_2-1664879980570.png

 

Best regards,

 

Shaglok
Contributor III
Contributor III

Hello!

I am using your solution, but I have a problem. Both measures seem to do not work well together.

If I put a table with DateField, Measure1 and Measure2, the second measure always return a 0.