Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. 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
anderseriksson
Partner - Specialist
Partner - Specialist

What is the problem?

([CurrentYearSales] - [LastYearSales]) / [LastYearSales]

0 Likes
919 Views
jerifortune
Creator III
Creator III

Thank you Andres.

The values on both LastYearSales and CurrentYearSales were derived using set analysis.

0 Likes
919 Views
anderseriksson
Partner - Specialist
Partner - Specialist

And?
That is common praxis and does not change the percentage calculation.

The expression: ([CurrentYearSales] - [LastYearSales]) / [LastYearSales]

references the cell values of the two other expression columns by expression name.

Alternatively you can use: (Column(2) - Column(1)) / Column(1)

but then you are dependent on the column order not changing or you must update your expression.

For Jan this will give: (50 - 45) / 45 = (5) / 45 = 0.11111... ~ 11.1%

919 Views
pradeepsagwal
Contributor II
Contributor II

I want YTD for Fiscal year instead of normal calendar year . I have used your blog Fiscal year script .

Thanks in advance

0 Likes
919 Views
jerifortune
Creator III
Creator III

Hi Pradeep,

To get Fiscal Year, it would require you to create a master calendar with fiscal year column and then use it to create similar YTD like the normal calendar.

919 Views
pradeepsagwal
Contributor II
Contributor II

‌I am confused how to use vFM variable in fiscal YTD.

0 Likes
919 Views
hic
Former Employee
Former Employee

If you want to create a fiscal YTD, using the vFM found on the blog post Fiscal Year, you can use the same logic as in

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

but with a slightly different way to calculate the day number of the year:

   If( Date - YearStart(Date,0,$(vFM)) <= Today() - YearStart(Today(),0,$(vFM)), 1, 0 ) as IsInYTD,

HIC

1,106 Views
Anonymous
Not applicable

Thank you Henric! This is helpful~ Happy new year!

0 Likes
1,106 Views
pradeepsagwal
Contributor II
Contributor II

Thankyou so much. It is working. I am happy that you replied hic

0 Likes
1,106 Views
Olip
Creator
Creator

Can you share more details or the sample app?

0 Likes
1,106 Views