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: 
dominic_marchan
Contributor III
Contributor III

Nested aggrgation

Hi

I am trying to calculate a KPI that will show me on a specific day the number of items below a certain level of inventory.

My data data is broken down by date, model, warehouse, inventory

datemodelwarehouseinventory
1/2/2020A1164
1/2/2020A418
1/2/2020B1153
1/2/2020B427
1/2/2020C1118
1/2/2020C484

 

My Attempt:

Count(Aggr(Count({$<date = {"1/2/2020"}, inventory = {"<=20"}>}model),model))

The result of this formula is 2, but the expected result would be 0. I want the formula to look at the model level and not the warehouse level (inventory should be summed at the model level so giving the results based on the below table)

ModelInventory
A82
B80
C102

 

Hence the number of models below 20 units should be 0

 

Thanks for your help

Labels (1)
1 Solution

Accepted Solutions
tm_burgers
Creator III
Creator III

I would use:

 

sum(if(Aggr(sum({<date={"1/2/2020"}>}inventory),model, date)<20,1,0))

 

and make the date a variable to be the date you want it, or a "today()"

View solution in original post

3 Replies
robert99
Specialist III
Specialist III

@dominic_marchan 

I'm sure there's lots of different ways to do this

I try to avoid aggr if possible. Like so

SCRIPT

LOADDATA:
Load * Inline [
date,model,warehouse,inventory,
1/2/2020, A, 11, 64,
1/2/2020, A, 4, 18,
1/2/2020, B, 11, 53,
1/2/2020, B, 4, 27,
1/2/2020, C, 11, 18,
1/2/2020, C, 4, 84,
2/2/2020, A, 11, 64,
2/2/2020, A, 4, 18,
2/2/2020, B, 11, 53,
2/2/2020, B, 4, 27,
2/2/2020, C, 11, 1,
2/2/2020, C, 4, 3,
];

Sum:
Load
date ,
model,
SUM(inventory) as SumInvScript
resident LOADDATA
GROUP BY date , model ;

or you could join date & model to avoid a synthetic join

Measure

count({<SumInvScript = {"<=20"}>} model) 

gives 1 in total. Or 0 and 1 if filtered by the various dates

 

Or try this Measure

sum( aggr(if(sum(inventory)<=20,1,0),date,model))

 

 

 

 

tm_burgers
Creator III
Creator III

I would use:

 

sum(if(Aggr(sum({<date={"1/2/2020"}>}inventory),model, date)<20,1,0))

 

and make the date a variable to be the date you want it, or a "today()"

NitinK7
Specialist
Specialist

Hi,

please refer below code that will be helpful to you for getting result

need some calculation in backend

PQR:
LOAD * INLINE [
date, model, warehouse, inventory
1/2/2020, A, 11, 64
1/2/2020, A, 4, 18
1/2/2020, B, 11, 53
1/2/2020, B, 4, 27
1/2/2020, C, 11, 18
1/2/2020, C, 4, 84
];

Left Join(PQR)
ABC:
LOAD
model,
Sum(inventory) as tot_inventory

Resident PQR
Group by model;

 

and take textbox and write expression like

Count({<tot_inventory={'>=20'}>}model).

 

Regards,

Nitin