Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
rishikeshtiwari
Creator
Creator

Conditional Calculation of Prior Year Sales

Hi Team,

I have a sample data set where Customer, Product & Time period wise data is available for the last few years.

There is a required scenario where we need to show the reports for

Monthly (month-wise) Current Year v/s Last Year Sales and

Customer & Product-wise Current v/s last Year Sales.

Here we have a condition for Last Year (Prior Year) Sales Calculation i.e we should consider the Sales of Customer & Products for Prior Year which Customer Products having Sales in the Current Year.

It means if there is any Sales value available for Customer&Product in the Current Year, those Customer & Products we should only take for last year's sales calculation on a particular period. This calculation should be dynamic as we have the last 4 years of data.

 

3 Replies
GaryGiles
Specialist
Specialist

You need to use the element function P() in your set analysis:

A basic structure to only include prior sales for customer-products in the current year:

sum({$<[Invoice Date]={">=01/01/19<=12/31/19"},Customer=P({$<[Invoice Date]={">=01/01/20<=12/31/20"}>} Customer),Product=P({$<[Invoice Date]={">=01/01/20<=12/31/20"}>} Product)>} [Sales Amount ($)])

I've hard coded the dates above.  It would make it easier if you created an Invoice Year field in the load script.  You could then use a variable to switch between selected current year.

sum({$<[Invoice Year]={$(vSelectedYear)},Customer=P({$<[Invoice Year]={$(=$(vSelectedYear)-1)}>} Customer)

,Product=P({$<[Invoice Year]={$(=$(vSelectedYear)-1)}>} Product)>} [Sales Amount ($)])

rishikeshtiwari
Creator
Creator
Author

Hi Gary,

Thanks for the quick reply. But output does not look accurate. We should have only 31600 $ vales Sales need to show for Monthly Prior Year. Current Year (Selected Year) should always have complete data but data should be reduced for Last Year as per Customer/Product availability in the current year. We should have the below output. Please let me know if you have any conflicts.

CustomerProductMonth2020 Sales2019 Sales
C1P1Feb10000
C1P1Apr20000
C1P1Aug27000
C1P1Jan12002000
C1P1Mar20004000
C1P1May65004500
C1P1Jul40004500
C1P1Sep58004700
C1P1Nov49005900
C2P1Oct40000
C2P1Nov30000
C2P1Jul27003000
C3P1Jul27003000
GaryGiles
Specialist
Specialist

Let's try a different approach.

Create an Invoice Month field in the load script with Month([Invoice Date]) as [Invoice Month] and use the following formula for prior year.

sum({$<[Invoice Year]={$(=$(vSelectedYear)-1)}>}
if(Match(Customer&Product&[Invoice Month],$(=Concat({$<[Invoice Year]={$(vSelectedYear)}>}distinct chr(39)&Customer&Product&[Invoice Month]&chr(39),',')))>0,
[Sales Amount ($)]))