# New to QlikView

Discussion board where members can get started with QlikView.

Highlighted
Contributor

## Weighted arithmetic mean avoiding duplicates

Hi all,

I need to do a weighted arithmetic mean something like: (a*weight(a)+b*weight(b) +....) / (weight(a)+weight(b) +...). I am able to do this but the problem are the duplicates. I will explain further:

Product line, Year-Quarter, Subregion, Weight, Target

Alpha, FY17-Q4, Spain, 12, 50

Alpha, FY17-Q4, Portugal, 15, 70

Alpha, FY17-Q4, Norway, 13, 80

Alpha, FY17-Q4, Dennmark, 18, 60

I want to represent the Target of the Region Europe( Iberia(Spain+Portugal) + North Europe(Norway+Dennmark) ), so i need to do: (12*50+15*70+13*80+18*60) / (12 +15+13+18).

To do this i have this expression:

sum(Target * Weight)/sum(Weight)

This is not working, i think that becose of the duplicates. I tried to use Distinct in the load script but it is not working (the scripts is huge and messy, someone made it a long time ago and left that monster as a present).

Is there a way to make the sum but only one value of Target and Weight for each SubRegion??

Thanks for the help,

I can explain further if needed.

1 Solution

Accepted Solutions
MVP

## Re: Weighted arithmetic mean avoiding duplicates

May be this

Sum(Aggr(Target * Weight, [Product line], [Year-Quarter], SubRegion))/Sum(Aggr(Weight, [Product line], [Year-Quarter], SubRegion))

8 Replies

## Re: Weighted arithmetic mean avoiding duplicates

Sum(TOTAL (Weight * Target)) / Sum(TOTAL Weight)

Before develop something, think If placed (The Right information | To the right people | At the Right time | In the Right place | With the Right context)

## Re: Weighted arithmetic mean avoiding duplicates

Or this?

Sum(TOTAL <Subregion> (Weight * Target)) / Sum(TOTAL <Subregion> Weight)

Before develop something, think If placed (The Right information | To the right people | At the Right time | In the Right place | With the Right context)
MVP

## Re: Weighted arithmetic mean avoiding duplicates

May be this

Sum(Aggr(Target * Weight, [Product line], [Year-Quarter], SubRegion))/Sum(Aggr(Weight, [Product line], [Year-Quarter], SubRegion))

Contributor

## Re: Weighted arithmetic mean avoiding duplicates

Either of them is working. I think that the problem is that for each Subregion there are around 200 duplicates of the weight. If i could only use 1 I think it would work. I also tried Sum(DISTINCT (Weight * Target)) / Sum(DISTINCT Weight) but sometimes the weights have the same value so it causes problems.

Thanks,

Lluís

## Re: Weighted arithmetic mean avoiding duplicates

Expected result from given data?

Before develop something, think If placed (The Right information | To the right people | At the Right time | In the Right place | With the Right context)
Contributor

## Re: Weighted arithmetic mean avoiding duplicates

This: (12*50+15*70+13*80+18*60) / (12 +15+13+18).

As I said the problem is that there are many duplicates of the weights for each subregion (I dont understand where they come from).

MVP

## Re: Weighted arithmetic mean avoiding duplicates

Did you give this a shot?

Sum(Aggr(Target * Weight, [Product line], [Year-Quarter], SubRegion))/Sum(Aggr(Weight, [Product line], [Year-Quarter], SubRegion))

or this

Sum(Aggr(Target * Weight, SubRegion))/Sum(Aggr(Weight, SubRegion))

Contributor

## Re: Weighted arithmetic mean avoiding duplicates

This totally worked!! Thanks!!

I will leave this link just in case someone (like i did) needs further info about Aggr Aggr() function

Thanks toboth of you!