Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
hic
Former Employee
Former Employee

A number alone doesn't tell you very much – you need to compare it with something. And very often you want to compare this year’s number with last year’s.

 

It is called Year-over-Year (YoY).

 

In such a comparison, you can for example compare the sales of the current month with the sales for the same month last year. Or – if you want to avoid fluctuations due to good or bad months, you instead look at the accumulated sales in the current year compared the same period last year. Then you look at the Year-to-Date (YTD) number.

 

But how do you calculate it? How do you write a simple formula that picks out a subset of transactions from last year and compares them to the corresponding transactions from the current year?

 

If you have Month as dimension and show accumulated numbers in the chart, you don’t need to do anything. The numbers are comparable as they are.

 

Line chart months.png

 

However, if you don’t use Month as dimension, the numbers will no longer be comparable since last year contains transactions from a longer period. You still may want to make the comparison, but with another first dimension and Year as the second.

 

There are several ways to do this, and they differ in how the reference date is defined. One way is to let the user define an arbitrary reference date – either through a selection or through a variable – and then use this is an advanced Set Analysis expression.

 

Another, much simpler way is to use the date of the script run as reference date. If your application is refreshed every night, this would mean that the Year-to-Date calculation always is up until today’s date.

 

Here’s how you do it:

 

In your Master Calendar you should define flags – Boolean fields – that define whether or not a specific date should be included in the calculation:

 

   If( DayNumberOfYear(Date) <= DayNumberOfYear(Today()), 1, 0 ) as IsInYTD,

 

The above formula tests whether the date falls before today’s date or not. Note that this flag will be useful also for dates belonging to other years than the current. The value of the flag will be 1 for dates in the beginning of the year irrespective of which year it is.

 

Then you can use this flag in a simple Set Analysis expression:

 

   Sum( {$<IsInYTD={1}>} Amount )

 

The Set Analysis expression will pick out the correct dates and thus the correct transactions for the comparison. Further, this expression can be combined with any dimensions.

 

Bar chart Product.png

 

Flags for a number of different time periods can be created like this, not just Year-to-Date, but also Quarter-to-Date, Month-to-Date, Current Month, Last Month, etc.

 

   If( DayNumberOfQuarter(Date) <= DayNumberOfQuarter(Today()), 1, 0) as IsInQTD,

   If( Day(Date) <= Day(Today()), 1, 0) as IsInMTD,

   If( Month(Date) = Month(Today()), 1, 0) as IsCurrentMonth,

   If( Month(AddMonths(Date,1)) = Month(Today()), 1, 0) as IsLastMonth,

 

Summary: Create the necessary flags in your Master Calendar. It will simplify your Set Analysis expressions tremendously.

 

HIC

73 Comments
sudeepkm
Specialist III
Specialist III

Hi Henric, Thanks a lot for such a nice post. It is useful to most of us as YOY is a very common requirement in dashboard development.

0 Likes
10,046 Views
Brice-SACCUCCI
Employee
Employee

Hi Peter,

I'm working on this. Please wait a little bit so that I can post something. This can be tricky to correctly apply because using the wrong selections on the "technical" fields can lead to unexpected results!

0 Likes
10,046 Views
Not applicable

Good as always!

0 Likes
10,046 Views
Not applicable

Hi HIC

Thank you for every month you are able to give us a big surprise. as every month i have sales meeting , and i am able use those new technique i learn from you to present my data , so the meeting is not so boring.

As usual i try to implement , but my chart for both 2013 and 2014 stop at march month. i see your chart for previous year is able to plot from jan till dec. where did i go wrong ?

Re: Chart not able to display till Dec for YOY compare from HIC on recent blog post

Paul

10,046 Views
Not applicable

I like these flags in the Master Calendar, however, they do limit you to YOY comparison for only current year and last year because they are static.  I prefer to use a dynamic option such as max() or addyears() in the application so when the user selects a given year, the previous year is added as comparison or multiple years can be compared.

9,984 Views
Not applicable

Henric Cronström

Hi,

Can you please advise how to get last five years YTD sum on bar chart, where the dimension is year. The problem is if I select month, then it is giving correct YTD for the last max year only and then full year sum for the last years when using 2nd formulas as below.

I am using two formulas:

1) To get last five years sum

Sum({$<Year={">=$(=Max(Year)-4)<=$(=Max(Year))"}>} Sales Amount)

2) Then to get YTD, I modified above formula as:

Sum({$<Year={">=$(=Max(Year)-4)<=$(=Max(Year))"},Month=,Quarter=,Date={"<=$(=MonthEnd(max(Date)))"} Sales Amount)

Thanks in advance!

BR,

SAK

0 Likes
9,984 Views
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi Shoaib,

I've responded to this question over on this thread: http://community.qlik.com/docs/DOC-4313

Cheers,

Steve

0 Likes
9,984 Views
Not applicable

What if my data is based on Month-Year, not a specific date? So an example data point is Apr 2011 or Apr 2012.  How do I create a year over year comparison for YTD data by month? So the bar chart would have 2011 data; the first data point would be YTD performance as of January, then YTD as of February and on through December compared to YTD for each month in 2012, 2013, and 2014 the same way.

0 Likes
9,984 Views
hic
Former Employee
Former Employee

There is really no principal difference from if you have dates. I would load the months like dates and format them as months, e.g.

     Date( Date#( [Month-Year], 'MMM YYYY'), 'MMM YYYY'), as [Month-Year]

and then you would have them as a correct date serial number (1st day of month) but formatted as Month-Year, and you could do pretty much everything as you do with dates.

HIC

0 Likes
9,984 Views
Not applicable

When I put that into my script, it still brings back ‘Apr 2011’, not a serial number. I am new to QlikView so sorry if my questions seem simple. ☺

0 Likes
9,984 Views