Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
jasonfu
Contributor
Contributor

Compare AGGR Expression to Embedded AGGR

Given the dataset below, I've been trying (and failing) to calculate the number of bicycle transactions with a total price above the average of all bicycle transactions for each state. I would also like to be able to calculate the total dollars above the average.

It seems like the AGGR used in my inequality condition doesn't evaluate properly, but if I use the exact same AGGR Avg expression in its own expression in the straight table to check its results it calculates properly.  So I feel like something funky is going on once it gets placed into the outer AGGR.  Any help would be greatly appreciated!

Results I would like 
I can't get Transactions  > Avg or Dollars Over Avg to work

StateBicycle TransactionsBicycle Transaction PriceAvg Bicycle Transaction PriceTransactions > AvgDollars Over Avg
CA49202302240
OR124524500
WA120020000

 

Existing Expressions that work

Bicycle Transactions =COUNT(DISTINCT {$<Item={'Bicycle'}>} TransactionID)

Bicycle Transaction Price =SUM({$<Item={'Bicycle'}>}Price)

Avg Bicycle Transaction Price =AVG(AGGR(SUM({$<Item={'Bicycle'}>}Price), TransactionID))

Existing Expressions that don't work

Transactions > Avg
=SUM(
   AGGR(
      IF(
         SUM({$<Item={'Bicycle'}>}Price)
         > AVG(AGGR(SUM({$<Item={'Bicycle'}>}Price), TransactionID, State))
         , 1
         , 0
      )
      , TransactionID
   )
)

Dollars Over Avg

=SUM(
   AGGR(
      IF(
         SUM({$<Item={'Bicycle'}>}Price)
         > AVG(AGGR(SUM({$<Item={'Bicycle'}>}Price), TransactionID, State))
         , SUM({$<Item={'Bicycle'}>}Price)
           - AVG(AGGR(SUM({$<Item={'Bicycle'}>}Price), TransactionID, State))
         , 0
      )
      , TransactionID
   )
)

Dataset

TransactionIDStateItemPartPrice
1CABicycleWheel100
1CABicycleHelmet200
2CABicycleHandle200
2CABicycleBrake200
3CABicycleBrake200
4CABicycleVest20
5CASkateboardWheel300
5CASkateboardBoard50
6CASkateboardWheel400
7WABicycleWheel200
8WASkateboardWheel50
9WASkateboardBoard600
9WASkateboardShoes100
9WASkateboardHelmet200
10WASkateboardBoard200
11ORBicycleWheel50
11ORBicycleHelmet75
11ORBicycleBrake120

 

Table:
Load * inline
[
TransactionID, State,Item,Part,Price
1, CA, Bicycle, Wheel, 100
1, CA, Bicycle, Helmet, 200
2, CA, Bicycle, Handle, 200
2, CA, Bicycle, Brake, 200
3, CA, Bicycle, Brake, 200
4, CA, Bicycle, Vest, 20
5, CA, Skateboard, Wheel, 300
5, CA, Skateboard, Board, 50
6, CA, Skateboard, Wheel, 400
7, WA, Bicycle, Wheel, 200
8, WA, Skateboard, Wheel, 50
9, WA, Skateboard, Board, 600
9, WA, Skateboard, Shoes, 100
9, WA, Skateboard, Helmet, 200
10, WA, Skateboard, Board, 200
11, OR, Bicycle, Wheel, 50
11, OR, Bicycle, Helmet, 75
11, OR, Bicycle, Brake, 120
];

Labels (1)
  • aggr

1 Reply
Brett_Bleess
Former Employee
Former Employee

Jason, have a look at the following Design Blog post, it may be helpful on this one:

https://community.qlik.com/t5/Qlik-Design-Blog/Pitfalls-of-the-Aggr-function/ba-p/1463275

Here is the base URL to the site if you want to do some further searching for other posts on AGGR too, I grabbed the most likely one to help, but there are others too!

https://community.qlik.com/t5/Qlik-Design-Blog/bg-p/qlik-design-blog

Regards,
Brett

To help users find verified answers, please do not forget to use the "Accept as Solution" button on any post(s) that helped you resolve your problem or question.
I now work a compressed schedule, Tuesday, Wednesday and Thursday, so those will be the days I will reply to any follow-up posts.