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

weighted average with set analysis/if statements

I want to calculate a monthly weighted average using a price field called CYCLE_AVG and volume field called Volume. However, both fields have conditions. For prices, I need only prices each day that are for Spot/spot and for volumes I only want daily Bulk volumes.

 

I have tried the formula below in a pivot table but it says the expression has an error. Also do I need to aggregate the total volumes by the monthly dimension to get total volumes for the month?

sum(Avg({$<GRIDPT_LABEL_TXT={'Spot/spot'}>} CYCLE_AVG)*Sum({$<[Bulk Rack]={'Bulk'}>} Volume))/Sum({$<[Bulk Rack]={'Bulk'}>} Volume)

1 Solution

Accepted Solutions
Kushal_Chawda

@sarahshong  you may need to use aggr function here to work your expression as expected. Try below

sum(aggr((Avg({$<GRIDPT_LABEL_TXT={'Spot/spot'}>} CYCLE_AVG)*Sum({$<[Bulk Rack]={'Bulk'}>} Volume))/Sum({$<[Bulk Rack]={'Bulk'}>} Volume), Month))

 

If you have more dimension is pivot table you may need to add that as well in your aggr function

or you can simply try removing outer sum function as below

(Avg({$<GRIDPT_LABEL_TXT={'Spot/spot'}>} CYCLE_AVG)*Sum({$<[Bulk Rack]={'Bulk'}>} Volume))/Sum({$<[Bulk Rack]={'Bulk'}>} Volume)

 

View solution in original post

2 Replies
edwin
Master II
Master II

Hi just looking at the syntax, the expression appears to be correct.  make sure all fields are correct.  If the expression is correct, you dont need to AGGR by month, jut add month as the dimension and this as the expression - then it means it is aggregated by month.
the expression does not give anyone unfamiliar with your data model enough information to answer your question so a lot of assumptions have to be made.  if this does not help maybe providing more info would help someone else help you.

Kushal_Chawda

@sarahshong  you may need to use aggr function here to work your expression as expected. Try below

sum(aggr((Avg({$<GRIDPT_LABEL_TXT={'Spot/spot'}>} CYCLE_AVG)*Sum({$<[Bulk Rack]={'Bulk'}>} Volume))/Sum({$<[Bulk Rack]={'Bulk'}>} Volume), Month))

 

If you have more dimension is pivot table you may need to add that as well in your aggr function

or you can simply try removing outer sum function as below

(Avg({$<GRIDPT_LABEL_TXT={'Spot/spot'}>} CYCLE_AVG)*Sum({$<[Bulk Rack]={'Bulk'}>} Volume))/Sum({$<[Bulk Rack]={'Bulk'}>} Volume)