Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
abhijith28
Creator II
Creator II

YTD calculation for all the years in Qliksense Bar chart

Hi,

I have to calculate YTD for all the particular previous & current years in Qlik bar chart

Input data:

abhijith28_0-1666107026656.png

Expected output:

abhijith28_1-1666107072952.png

Calculation should be YTD , even for previous years only YTD months should be used

for ex: for 2021-  sum of values will be from Jan 2021 - Oct 2021 

              for 2020- sum of values will be from  Jan 2020 -Oct 2020

 

Labels (1)
3 Replies
RafaelBarrios
Partner - Specialist
Partner - Specialist

hi @abhijith28 

i would do this

first
create a new column in your script as your month is text and not date format

LOAD 
...
date(date#(month,'MMM'),'M') as month_number
...

 your should get something like

RafaelBarrios_0-1666166680979.png

Second, your set analysis formula for expression

sum({< month_number={"<=$(=num(month(today())))"}>} value)

 

RafaelBarrios_1-1666166843415.png

 

in your Expected output from 2021 you are excluding Oct

if so, try

sum({< month_number={"<=$(=num(month(addmonths(today(),-1))))"}>} value)

 

hope this helps.

best,

 

abhijith28
Creator II
Creator II
Author

Thanks @RafaelBarrios 

I need a output in pivot table or bar chart where one dimension year and one expression.

The expression which you have shared wont work for all the years. 

sidhiq91
Specialist II
Specialist II

@abhijith28  Could you please try something like below in the back end:

NoConcatenate
Temp:
Load Product,
Date(Date#(Year,'YYYY'),'YYYY') as Year,
Date(Date#(Month,'MMM'),'MM') as Month,
Value
inline [
Product, Year, Month, Value
A,2022, Jan, 10
A,2022, Feb, 20
A,2022, Mar, 30
B,2022, Jan, 25
B,2021, Feb, 50
B,2021, Sep, 30
B,2021, Oct, 10
C,2020, Mar, 25
C,2020, Apr, 30
C,2020, Nov, 40
];

Exit Script;

For Current Year: Sum({<Year={"$(=Year(today()))"}, Month={">=$(=Date(Yearstart(today()),'MM'))
<=$(=Date(monthstart(today()),'MM'))"}>} Value)

For previous Year: Sum({<Year={"$(=Year(today())-1)"}, Month={">=$(=Date(Yearstart(today()),'MM'))
<=$(=Date(monthstart(today()),'MM'))"}>} Value)

For Year before that: Sum({<Year={"$(=Year(today())-2)"}, Month={">=$(=Date(Yearstart(today()),'MM'))
<=$(=Date(monthstart(today()),'MM'))"}>} Value)

You can use these expression in the Pivot Chart: