Last week I wrote about how the Above() function can be used for calculating rolling averages and other accumulations. There is however also an alternative method for doing the same thing:
The As-Of table.
When you use the Above() function, you fetch a number from other rows in a chart or Aggr() table. The As-Of table is slightly different in this respect: It is not a transient table created by an object or an expression – instead it is a real table in the data model.
The idea is to create a secondary month field – the AsOfMonth - that links to multiple real months.
In the example above, you can see that ‘2015 Oct’ links to several preceding months, and each Month in turn links to several rows in a fact table. This means that a specific transaction will be linked to several AsOfMonths.
In the data model, the As-Of table should appear as a separate calendar table that links to the existing primary calendar table:
One way to create this table is the following:
First, make sure that you in your master calendar have a field “Month” that is defined as the first date of the month, e.g.
Date(MonthStart(Date),'YYYY MMM') asMonth,
Then add the following lines at the end of the script:
// ======== Create a list of distinct Months ======== tmpAsOfCalendar: LoaddistinctMonth Resident [Master Calendar] ;
I have made the Set Analysis expressions based on two fields: YearDiff and MonthDiff. However, for clarity it could be a good idea to add flags in the As-Of table, so that the Set Analysis expressions become even simpler, e.g.
And again it could be done entirely with set analysis without the need of an As-Of table as long as you have control over your calendar dimensions. The suggestion using Above can be tricky because it depends on the sorting order of the table. With a customizable table you might not have control over that.
Do you happen to have an example qvw with Sales and PM Sales by Month in a pivot? I understand that I can get Current Month and Previous Month sales in an expression, what I'm having difficulty with is showing it by Month.
With only set analysis it's a lot harder. A set analysis expression set is calculated at the chart level. If you want different sets for each value of a month dimension like Cory needs you need a rather big nested if or pick-match combination to calculate the correct set for each month value. See for example Evaluating "sets" in the context of a dimension for more information
maybe I have overseen it, but I try to use the as of table for a rolling n month chart with filter on the current Year. Due to the usability I want to do the Selection by the Year field of my Master Calendar and not by the AsOfYear field from my as of table. But in that case I get only the Month value for January displayed and not the sum of December and January for the first displayed Month 2018/01.
Is there a possibility to solve this by expression like in that example?
To calculate MonthDiff you need to divide the number of days between the two monthstarts with the average month length, which is 365.2425/12. Hence this number.
In my example I have an as-of table based on months. If you instead want a finer granularity, e.g. days, you can do this by loading dates instead of months:
tmpAsOfCalendar:
LoaddistinctDate
Resident [Master Calendar] ;
and define AsOfDate, AsOfWeek, and AsOfMonth accordingly. To define WeekDiff is then straightforward:
(WeekStart(AsOfDate)-WeekStart(Date))/7 as WeekDiff,
Thank you HIC for this post. This is easily the best way I have found to do accumulations. Above etc is just too limited.
But I'm unsure if there is a better way to do what I have done below. It works but its certainly not straight forward. Or intuitive.
My view is Qlik should allow users to link to dimensions in measures as say PBI does. I can see that the Qlik approach is different to PBI (overall the user outcome is better in Qlik but it can be confuisng to get the required outcome) but this is one area where IMHO Qlik should have a look at how PBI does it. Accumulations are easy in PBI. Its very confusing in Qlik because the measure can't link to the dimension using set analysis. Either within set analysis or to ignore a selection(s).
Anyway I was asked to set up a debtors report. Where the user selected one MonthYear and the table would show the closing balances for the last say 6-7 Months.
Initially to get the Months closing balance for a period was easy enough. I could use MonthsAgo and a selected MonthYear
But then I set up AsOfMonth (to do a table with Month Ends of closing balances) so planned to use only AsOfMonth filter selections. To overcome this drawback
"However, it has one drawback: When a user clicks in the chart, a selection will be made in the AsOfMonth. But you don’t want the user to make selections here: You want month selections to be made in the real month field. So I usually make my charts read-only if they use the AsOfMonth as dimension." Howeevr in Sense it is not possible to make a chart read only
Easy enough with the closing balance only with this measure
I'm unsure why Qlik has been set up in this way. I guess it works but I'm still confused why Qlik has designed this to be so confusing. When everything else about Qlik is logical and often when understood straight forward. Some of these accumulation work around should really not be needed. Its a common requirement and Qlik should handle this area much easier. Using 'above' and AsOfMonth etc are work arounds that do work well when finally understood but should be a built in feature.
After further thought I am going to move on from select one monthYear and automatically populating say the previous 6 months (as it seems like in this situation it can't be done using AsofMonth). To getting the user to select the AsOfMonth (or AsOfMthYr that I will rename this) monthYears required
And converting the KPIs to a latest AsOfMonth selected as follows (for >30 days)
The way I had done it before was far too confusing. It was easy and worked well for the end user but it required sometimes using MonthYear and somethings AsOfMonth throughout the pages