Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi guys,
maybe it is not a right post for advanced level.
I have a line chart with year breakdown based on month and year data.
we have data as of Jan 201 for now. Rather than showing 2021 as YTD, I want to show 2021 MAT ( Feb 20 to Jan 21)
anyway to achieve this ?
Thanks
Hi @coloful_architect, I tested with some dumb data and also have added an AsOfYear field to use as dimension, and flagged the current month to filter most recent data to MAT (I'm thinking now that maybe it needs to be the month before this one, as we don't have all data of this month).
Data:
LOAD MakeDate(Year,Month) as Date, Value
INLINE [
Month, Year, Value
01,2020, 10
02,2020, 8
03,2020, 7
04,2020, 6
05,2020, 8
06,2020, 9
07,2020, 7
08,2020, 6
09,2020, 4
10,2020, 6
11,2020, 8
12,2020, 5
01,2021, 4
02,2021, 6
03,2021, 5
];
[Master Calendar]:
LOAD
Date,
Date(MonthStart(Date),'YYYY MMM') as Month,
Year(Date) as Year
Resident Data;
// ======== Create a list of distinct Months ========
tmpAsOfCalendar:
NoConcatenate Load distinct Month
Resident [Master Calendar] ;
// ======== Cartesian product with itself ========
Join (tmpAsOfCalendar)
Load Month as AsOfMonth
Resident tmpAsOfCalendar ;
// ======== Reload, filter and calculate additional fields ========
[As-Of Calendar]:
Load Month,
AsOfMonth,
If(AsOfMonth=MonthStart(Today()),1,0) as isThisMonth,
Year(AsOfMonth) as AsOfYear,
Round((AsOfMonth-Month)*12/365.2425) as MonthDiff,
Year(AsOfMonth)-Year(Month) as YearDiff
Resident tmpAsOfCalendar
Where AsOfMonth >= Month;
Drop Table tmpAsOfCalendar;
Then I've created a barchart with AsOfYear as dimension and this expression:
If(AsOfYear=Year(Today())
,Sum({$<isThisMonth={1}, MonthDiff={"<12"}>} Value) //Last 12 months
,Sum({$<YearDiff={0}>} Value) // YTD
)
Hi, maybe using an as-of table, and using an if() to apply a diffrent expression for max/current year.
https://community.qlik.com/t5/Qlik-Design-Blog/The-As-Of-Table/ba-p/1466130
Hi Rubemarin,
thanks for the sharing...I may not fully catch that as of table/if (), it seems I could only switch into one model...either avg or regular accumulation.
but how I can present fully year + MAT together. Mind if elaborating a bit?
Tricky...
Basically, you want the dimension to sometimes mean "Calendar year" and sometimes "MAT year". Which would mean that the same data point (record in the source data) sometimes belongs to 2021, and sometimes to 2020, depending on whether it is a Moving Annual Total (MAT) or a straightforward summation. For example, the sales from a date in Apr 2020 should belong to the MAT year 2021, and at the same time to the calendar year 2020.
This is not easy. It's probably possible, but would mean a very complex expression using accumulation in an Aggr(), or an As-Of table. Will it be maintainable?
I would instead do it in two charts. If you use something similar to
=Year(Today())+1+Floor((OrderDate-Today())/365)
as dimension, then your measure will automatically be the MAT. (You can make it even more elaborate so that it handles leap years, and uses the 1st of every month as cut-off date.)
But you can't do a "normal" Sum(Quantity) using this dimension - it will always be a MAT.
Hi @coloful_architect, I tested with some dumb data and also have added an AsOfYear field to use as dimension, and flagged the current month to filter most recent data to MAT (I'm thinking now that maybe it needs to be the month before this one, as we don't have all data of this month).
Data:
LOAD MakeDate(Year,Month) as Date, Value
INLINE [
Month, Year, Value
01,2020, 10
02,2020, 8
03,2020, 7
04,2020, 6
05,2020, 8
06,2020, 9
07,2020, 7
08,2020, 6
09,2020, 4
10,2020, 6
11,2020, 8
12,2020, 5
01,2021, 4
02,2021, 6
03,2021, 5
];
[Master Calendar]:
LOAD
Date,
Date(MonthStart(Date),'YYYY MMM') as Month,
Year(Date) as Year
Resident Data;
// ======== Create a list of distinct Months ========
tmpAsOfCalendar:
NoConcatenate Load distinct Month
Resident [Master Calendar] ;
// ======== Cartesian product with itself ========
Join (tmpAsOfCalendar)
Load Month as AsOfMonth
Resident tmpAsOfCalendar ;
// ======== Reload, filter and calculate additional fields ========
[As-Of Calendar]:
Load Month,
AsOfMonth,
If(AsOfMonth=MonthStart(Today()),1,0) as isThisMonth,
Year(AsOfMonth) as AsOfYear,
Round((AsOfMonth-Month)*12/365.2425) as MonthDiff,
Year(AsOfMonth)-Year(Month) as YearDiff
Resident tmpAsOfCalendar
Where AsOfMonth >= Month;
Drop Table tmpAsOfCalendar;
Then I've created a barchart with AsOfYear as dimension and this expression:
If(AsOfYear=Year(Today())
,Sum({$<isThisMonth={1}, MonthDiff={"<12"}>} Value) //Last 12 months
,Sum({$<YearDiff={0}>} Value) // YTD
)