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

Aggregate over multiple fields and condition

Hi, I have a below table structure

Id, Date, Week, Numerator, Denominator, StartDate (Calculated Field), EndDate (Calculated Field)

Attached excel with data source data and calculations required.

Now i would like to populate Sum(Numerator), Sum(Denominator), KPI =Sum (Numerator)/sum(Denominator) and Rank based on KPI within Week against each ID. However, the sum(Numerator) and sum(Numerator) has to be same for ID between StartDate and EndDate.

I tried many things, but could not get it work.

FYI. have used Qliksense before 3 years and started again very recently.

@sunny_talwar , @swuehl  - i have seen quite a bit of succesful solutions from your side for similar query in many posts, so hopeful that you would be able to help me. Apologies for direct tag.

Labels (1)
2 Solutions

Accepted Solutions
sunny_talwar

I selected a single  Employee Number, is this what you want to see?

image.png

So, right now I used this

=Aggr(RangeSum(Above(Sum(Chats), 0, 2)), [Person(Person - who) : Employee_Number], [WC Week])

but you can drive the 3rd argument with Above() function using  a variable which can be a user input

View solution in original post

Dipak
Contributor III
Contributor III
Author

Just in case it helps someone looking for similar solution.

i coult not find asoftable way to work as i need both weekly and monthly summary in my analysis.

as i said,  solution provided by @sunny_talwar  works well if there are no missing days, so I created a simple script to impute missing days;

1) Delete all tables (since i needed to run the script many times and with many intermediate tables, i was difficult to keep a track of which table is created when, so i simply delete all tables before data load to flush out all unnecessary tables)

LET vL.NumTabelle = NoOfTables();

FOR j=0 to $(vL.NumTabelle)-1

LET vL.nomeTabella = TableName(0);

  DROP Table [$(vL.nomeTabella)];

NEXT j;

2) Load data from source

[Table1]:

LOAD

               ID,

               Date(Date#(DailyDate, 'DD/MM/YYYY') ) AS DailyDate,

               Date(DateWeek, 'DD/MM/YYYY') ) AS [WC Week],

               Value1,

               Value2

 FROM [lib://(Connection_name)/(file_name)]

(ooxml, embedded labels, table is Sheet1);

3) Get max and min value by each ID

[ALLID]:

LOAD

               ID,

               Min(Date(Date#(DailyDate, 'DD/MM/YYYY') )) AS MinDate,

// for records with only 1 dailyDate, keep max date as max date +1 for next step //             

IF((Min(Date(Date#(DailyDate, 'DD/MM/YYYY') )))=(Max(Date(Date#(DailyDate, 'DD/MM/YYYY') ))),1,0)+Max(Date(Date#(DailyDate, 'DD/MM/YYYY') )) AS MaxDate

               Resident [Table1]

    Group by ID;

4) Iterate through each record and create copy of each record till max date is reached. For record with only 1 date, min date  = max date (if not added one) and corresponding record will not get created, thats' why 1 is added if mindate and maxdate are same (which can only happen if there is only 1 record for a given ID)

this table is left joined on original table by ID and DailyDate

[ALLIDDates]:

Load ID,

Date(iterno()+MinDate) as DailyDate,

//Create Week New column here //

WeekStart(Date(iterno()+MinDate)) as [Week New]

resident [ALLID]

While iterno()+MinDate <= MaxDate;

 

left join

load * resident [Table1];

 

5) delete unnecessary tables

drop Table [ALLID];

drop table [Table1];

Exit Script;

this works perfectly well with Rangesum(above )formula that @sunny_talwar  provided.

 

View solution in original post

22 Replies
Dipak
Contributor III
Contributor III
Author

slight change, unfortunately can't use StartWeek and EndWeek columns as they are based on different logic.

they can be replaced with expression [Week+5] as EndDate and Week-(vWeekNum-1)*7 as StartDate where vWeekNum is a defined variable.

Appreciate if anyone can help as stuck with this since sometime. I know i am missing something very basic, but can't figure out exactly what. 

I am using below expression, but it returns value only for current week

Sum( <ID,Week> Numerator)

Not able to figure out how to compare Week Variable to StartDate and EndDate.

Dipak
Contributor III
Contributor III
Author

 

Spoiler
Got it down to this two expressions.


1) aggr(Sum({<[Time(day - when)]={">=$(=min([Time(day - when)]))<=$(=Max([Time(day - when)]))"}>} Value),[Person Number]).

This expression works, but give sum of Value at person level for all dates. Numbers are fine.


2) However, when i replace the Min and Max with Weekend expression as below

aggr(Sum({<[Time(day - when)]={">=$(=date(weekend([Time(day - when)])-14,'DD/MM/YYYY'))<=$(=date(weekend([Time(day - when)]),'DD/MM/YYYY'))"}>} Value),[Person Number]).

nothing comes.

What'w wrong here? I need to sum 2 weeks of sum of values against each Person Number and Weeknumber.





 

sunny_talwar

Would you be able to share a qvf sample where you are trying this out? It would be easier to see what you already have and take it forward from there.

Dipak
Contributor III
Contributor III
Author

@sunny_talwar I have qvf file as i am working on qliksense. would that work?

sunny_talwar

qvf is what I asked for 🙂

Dipak
Contributor III
Contributor III
Author

My bad. my brain is fried right over this and i read it as qvw :-).

Here it is. the last column where i need Current + Last Week numbers is where i am unable to work it out. 

Ideally the Number of Weeks should be a variable and thus last column should represent aggregation over selected number of weeks (a variable is defined in the Enterprise app as vWeekNum with values being 1,2,3,4,5,6,7 and  8).

 

sunny_talwar

I selected a single  Employee Number, is this what you want to see?

image.png

So, right now I used this

=Aggr(RangeSum(Above(Sum(Chats), 0, 2)), [Person(Person - who) : Employee_Number], [WC Week])

but you can drive the 3rd argument with Above() function using  a variable which can be a user input

Dipak
Contributor III
Contributor III
Author

@sunny_talwar Thanks buddy. It worked perfectly.

Just out of curiosity, any idea on why only first row of week shows value and not rest of the days. Though i don't need week column in my final summary, but would be good to know. Ideally first 5 rows should show 114 for last 2 columns, then next 5 rows should show 122 in second last column and 290 in last column.

Thank you once again. and apologies in advance as i am going to bug you in future whenever i am in trouble 🙂

 

sunny_talwar

To do that, you can do this

=Aggr(NODISTINCT RangeSum(Above(Sum(Chats), 0, 2)), [Person(Person - who) : Employee_Number], [WC Week])