Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
selracir
Contributor II
Contributor II

Filters with a month and previous month view

I'm trying to show in a visualization the current month value and the previous month. I have some filters for year/month

I've created an example simple dataset to recreate the issue I have. The data is as follows:

YearMonthYearMonthNameMonthNumValue
2020012020Jan0110
2020022020Feb0220
2020032020Mar0330
2020042020Apr0440
2020052020May0550
2020062020Jun0660
2020072020Jul0770
2020082020Aug0880

 

What I want to visualize is a chart with the value of the Current Month (CM) and the Previous Month (CM-1) and using the filters month/year to update accordingly. I used the following formulas:

CM = sum({$<[YearMonth]={'$(=Max(Num#(YearMonth)))'}>} Value)

CM-1 = sum({$<[YearMonth]={'$(=date(date#(Date(MakeDate(Max(Num#(Year)), right(Max(num#(YearMonth)),2), 1)-1),'MM/DD/YYYY'),'YYYYMM'))'}>} Value)

I've added below the filter of the YearMonth and the table to help to understand what is happening

QS1.jpg

 

When I select a specific YearMonth, for example 202003, the visualization works correctly

QS2.jpg

The problem appears when I select the month instead as it filters out the data for the previous month and shows 0 as CM-1

QS3.jpg

I don't understand why it works with YearMonth selection, but not with Month selection. I'm guessing that the problem is whit the formula I've used, but I can not figure it out how to resolve it.

Any ideas?

Labels (3)
1 Solution

Accepted Solutions
sunny_talwar

First of all load YearMonth field as a date field

Date(MonthStart(Date#(YearMonth, 'YYYYMM')), 'YYYYMM') as YearMonth

and then try these expressions

CM:
Sum({$<[YearMonth] = {"$(=Date(Max(YearMonth), 'YYYYMM'))"}>} Value)

CM-1:
Sum({$<[YearMonth] = {"$(=Date(MonthStart(Max(YearMonth), -1), 'YYYYMM'))"}, MonthName, MonthNum, Year>} Value)

View solution in original post

4 Replies
sunny_talwar

First of all load YearMonth field as a date field

Date(MonthStart(Date#(YearMonth, 'YYYYMM')), 'YYYYMM') as YearMonth

and then try these expressions

CM:
Sum({$<[YearMonth] = {"$(=Date(Max(YearMonth), 'YYYYMM'))"}>} Value)

CM-1:
Sum({$<[YearMonth] = {"$(=Date(MonthStart(Max(YearMonth), -1), 'YYYYMM'))"}, MonthName, MonthNum, Year>} Value)
selracir
Contributor II
Contributor II
Author

Thanks @sunny_talwar 

I've created dimension (S_YearMonth) and measures like: 

S_YearMonth: Date(MonthStart(Date#(YearMonth, 'YYYYMM')), 'YYYYMM')

S_CM = Sum({$<[S_YearMonth] = {"$(=Date(Max(S_YearMonth), 'YYYYMM'))"}>} Value)

S_CM-1 = Sum({$<[S_YearMonth] = {"$(=Date(MonthStart(Max(S_YearMonth), -1), 'YYYYMM'))"}, MonthName, MonthNum, Year>} Value)

Using these, I get the following:

QS4.jpg

I need to show only the current month and the previous month, but not all months are shown. Also it seems like it is not picking up the previous month value in S_CM-1. 

selracir
Contributor II
Contributor II
Author

Just to add also that with the initial formula I could see the YearMonth in the formula

QS5.jpg

With the new formula it seems like it is blank:

QS6.jpg

 

selracir
Contributor II
Contributor II
Author

@sunny_talwar 

I managed to get it sorted with what you sent me

S_CM = Sum({$<YearMonth= {"$(=Date(Max(Date(MonthStart(Date#(YearMonth, 'YYYYMM')), 'YYYYMM')), 'YYYYMM'))"}>} Value)

S_CM-1 = Sum({$<[YearMonth] = {"$(=Date(MonthStart(Max(Date(MonthStart(Date#(YearMonth, 'YYYYMM')), 'YYYYMM')), -1), 'YYYYMM'))"}, MonthName, MonthNum, Year>} Value)

Thanks for your guidance!!