Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
midnight7699
Contributor
Contributor

Calculate weighted average for a group

Hi there,

I have a dataset like this (lots more rows and columns)

IDStatus (N or U)AgeInscription Date
1N192020/12/03
2U582020/09/07
3N452018/07/09
4U402019/01/02
    

 

I want to calculate the weighted average of age for each group of N or U (group in status) and for each period (2018-2019-2020), also the weighted average of age for each group of N or U during each quarter since 2018.

I'm newbie in QlikView so it'd be really grateful if someone can leave some comments here.

Thanks!!

Labels (1)
1 Reply
QFabian
Specialist III
Specialist III

HI @midnight7699 , is this what you want? please check the script:

Data:
LOAD * INLINE [
ID, Status, Age, InscriptionDate
1, N, 19, 2020/12/03
2, U, 58, 2020/09/07
3, N, 45, 2018/07/09
4, U, 40, 2019/01/02
];

B:
Load
ID,
Status,
Age,
InscriptionDate,
QuarterName(InscriptionDate) as Quarter,
year(InscriptionDate) as Year
Resident Data;


Status:
Load
Status,
avg(Age) as AgeAvgStatus
Resident B
group by Status;


Quarter:
Load
Quarter,
avg(Age) as AgeAvgQuarter

Resident B
group by Quarter;

Year:
Load
Year,
avg(Age) as AgeAvgYear

Resident B
group by Year;

drop table Data;

 

that script produces the following data model :

QFabian_0-1611178877844.png

 

QFabian