# QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Announcements
New Contributor

## If-statement for Previous 12 Months

In my Master Calendar in my Edit Script I have used the following:

If(Datum <= Today() And Datum >= SetDateYear(Today(), Year(Today())-1), 1, 0) As Last12Months

Now I want to add a similar If-statement for Previous 12 Months. How do I script this (in a similar way as above)?

I am using this to be able to compare data for the last 12 months and the previous 12 months a year earlier. In a bar chart for example.

Labels (3)

• ### Scripting

2 Solutions

Accepted Solutions
MVP

## Re: If-statement for Previous 12 Months

Last12Months

`If(Datum <= Today() And Datum >= AddYears(Today(), -1), 1, 0) as Last12Months`

Previous12Months

`If(Datum <= AddYears(Today(), -1) And Datum >= AddYears(Today(), -2), 1, 0) as Previous12Months`
MVP

## Re: If-statement for Previous 12 Months

Try using a sort expression like this

`Max(MonthYear)`

If that still doesn't work, please share a sample file

13 Replies
Honored Contributor II

## Re: If-statement for Previous 12 Months

May be this
If(Datum <= SetDateYear(Today(), Year(Today())-1) And Datum >= SetDateYear(Today(), Year(Today())-2), 1, 0) As Previous12Months
Learning never stops.
Contributor II

## Re: If-statement for Previous 12 Months

try this :

If(Datum <= Today() And Datum >= AddMonths(Today(), -12), 1, 0) As Last12Months

Regards!!!

MVP

## Re: If-statement for Previous 12 Months

Last12Months

`If(Datum <= Today() And Datum >= AddYears(Today(), -1), 1, 0) as Last12Months`

Previous12Months

`If(Datum <= AddYears(Today(), -1) And Datum >= AddYears(Today(), -2), 1, 0) as Previous12Months`
New Contributor

## Re: If-statement for Previous 12 Months

Now the graph looks like this. I did use MonthYear though instead of Month. When I use Month it does give the bars for each month next to each other. But the month January should be last in row, and it shows first in row. How do I solve this?

MVP

## Re: If-statement for Previous 12 Months

May be use Month as a dimension instead of MonthYear here

Highlighted
New Contributor

## Re: If-statement for Previous 12 Months

But then January 2019 shows on the left instead of the right. It is supposed to be a dynamic graph that changes when the date changes.

MVP

## Re: If-statement for Previous 12 Months

Use a sort expression to fix this... it shouldn't be too hard to use a sort expression. If you are unable to do this, please share a sample so that we can try out and get you the right sort expression

New Contributor

## Re: If-statement for Previous 12 Months

I tried all different ways of sorting, but none gives me sort by date. I guess because it only recognizes Month when using that as a Dimension?

MVP

## Re: If-statement for Previous 12 Months

Try using a sort expression like this

`Max(MonthYear)`

If that still doesn't work, please share a sample file