Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
chrisevans_cj
Contributor III
Contributor III

Difference between Aggregated Fields

Hi everyone ... looking for a bit of help here:

Sales PersonDateTotal SalesTotal FruitTotal Veg
Bob22 09 20181105555
Bob23 09 20181206060
Bob24 09 201817565115
Jimmy22 09 201820515
Jimmy23 09 201820515
Jimmy24 09 201820515
Sally22 09 201817011555
Sally23 09 201818012060
Sally24 09 201819012565

 

my dataset returns aggregated values per day and i would like in my load statement add 3 new column which shows the difference of the Total Sales, Total Veg and Total Fruit on that day. So something like this:

Sales PersonDateTotal SalesTotal FruitTotal VegDiff SalesDiff FruitDiff Veg
Bob22 09 201811055551055
Bob23 09 201812060601055
Bob24 09 20181756511555555
Jimmy22 09 201820515000
Jimmy23 09 201820515000
Jimmy24 09 201820515000
Sally22 09 2018170115551055
Sally23 09 201819012565201010
Sally24 09 2018200130701055

What is the best practice for this?

1 Solution

Accepted Solutions
rubenmarin

Hi, you can use Peek() to access previous loaded record, you only need to do a sorted load and calculates the differences:

Data:
LOAD [Sales Person],	Date(Date#(Date, 'DD MM YYYY')) as Date,	Sales,	Fruit,	Veg Inline [
Sales Person,	Date,	Sales,	Fruit,	Veg
Bob,	22 09 2018,	110,	55,		55
Bob,	23 09 2018,	120,	60,		60
Bob,	24 09 2018,	175,	65,		115
Jimmy,	22 09 2018,	20,		5,		15
Jimmy,	23 09 2018,	20,		5,		15
Jimmy,	24 09 2018,	20,		5,		1,
Sally,	22 09 2018,	170,	115,	55
Sally,	23 09 2018,	180,	120,	60
Sally,	24 09 2018,	190,	125,	65
];

DataWithDiff:
LOAD *,
	If(Peek([Sales Person])=[Sales Person], Sales-Peek(Sales), Sales) 	as [Sales Diff],
	If(Peek([Sales Person])=[Sales Person], Fruit-Peek(Fruit), Fruit) 	as [Fruit Diff],
	If(Peek([Sales Person])=[Sales Person], Veg-Peek(Veg), Veg) 		as [Veg Diff]
Resident Data
Order By [Sales Person], Date;

DROP Table Data

View solution in original post

4 Replies
rubenmarin

Hi, if you have a table with Sales Person an Date as dimension you can use an expression like:

If(Above([Sales Person])=[Sales Person], Sum(Sales)-Above(Sum(Sales)))

What I don't know is how you get a diffference for the first value of each sales person as it doen't have any value to make to comparison.

chrisevans_cj
Contributor III
Contributor III
Author

Hi,

 

i am looking to use this in the Data Extraction / Script Load portion. Your solution works in a graph however i need the data itself modified during ETL so i can do analysis on these daily figures, interact with those data fields and find abnormal values/behaviors

 

To answer your question "What I don't know is how you get a difference for the first value of each sales person as it doesn't have any value to make to comparison." 

This would be the difference from the previous day's data - at some point in time it would start with 0 as the Total Sales, it is because this was just a sample dataset - These new columns would basically show the difference between each day

rubenmarin

Hi, you can use Peek() to access previous loaded record, you only need to do a sorted load and calculates the differences:

Data:
LOAD [Sales Person],	Date(Date#(Date, 'DD MM YYYY')) as Date,	Sales,	Fruit,	Veg Inline [
Sales Person,	Date,	Sales,	Fruit,	Veg
Bob,	22 09 2018,	110,	55,		55
Bob,	23 09 2018,	120,	60,		60
Bob,	24 09 2018,	175,	65,		115
Jimmy,	22 09 2018,	20,		5,		15
Jimmy,	23 09 2018,	20,		5,		15
Jimmy,	24 09 2018,	20,		5,		1,
Sally,	22 09 2018,	170,	115,	55
Sally,	23 09 2018,	180,	120,	60
Sally,	24 09 2018,	190,	125,	65
];

DataWithDiff:
LOAD *,
	If(Peek([Sales Person])=[Sales Person], Sales-Peek(Sales), Sales) 	as [Sales Diff],
	If(Peek([Sales Person])=[Sales Person], Fruit-Peek(Fruit), Fruit) 	as [Fruit Diff],
	If(Peek([Sales Person])=[Sales Person], Veg-Peek(Veg), Veg) 		as [Veg Diff]
Resident Data
Order By [Sales Person], Date;

DROP Table Data
chrisevans_cj
Contributor III
Contributor III
Author

Thank you kind Sir. Have a great day - take care, wash your hands regularly, stay home and be safe out there