Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Ross1991
Contributor III
Contributor III

How to get Avg function by Selected Month

 

Ross1991_0-1716236746962.png                Ross1991_1-1716236799426.png

=sum(Aggr(avg([Pieces Produced]),[2024 Jan Feb Prod.Part#]))

Above formula gives me average of Jan and Feb data.  (450+500+800)/3 = 583 (which is great).

However, can someone tell me how to modify above formula if I just want to see month of January Average?  It should be (450+500)/2= 475.  Thank you.   

 

Labels (1)
3 Solutions

Accepted Solutions
TauseefKhan
Creator III
Creator III

You can use the Month() Function: Month(Yourdatefield) or calendar in your script.

Table_GetFieldselect:
LOAD
    Date(Date#(Date,'M/D/YYYY'),'YYYY-MM-DD') as Date,
    Resource,
    [Part#],
    [Pieces Produced],
    Month(Date(Date#(Date,'M/D/YYYY'),'YYYY-MM-DD')) as Month
INLINE [
    Date, Resource, Part#, Pieces Produced
    01/05/2024, B106, E, 450
    01/29/2024, B106, E, 500
    02/23/2024, B106, E, 800
    03/05/2024, B106, A, 050
    04/29/2024, B106, A, 200
    03/23/2024, B106, B, 100
 
];

View solution in original post

Ross1991
Contributor III
Contributor III
Author

You are amazing. It worked. Used Filter. Thank you. I used lot more data set for my practice. It is working. Did not know about Date in the filter like = Month()



[cid:image001.png@01DAB14E.065B40A0]


View solution in original post

Ross1991
Contributor III
Contributor III
Author

@TauseefKhan Thanks again.   I removed DISTINCT from the formula because I had lot of repeating values in my data.   Now it is working when I selected individual month or all months.  It looks like below.   

 

Avg(
Aggr(
Sum(TOTAL <[Part#]> [Pieces Produced]) / Count(TOTAL <[Part#]> [Pieces Produced]),
[Part#]
)
)

View solution in original post

9 Replies
igoralcantara

Try something like this:

Avg(
     Aggr(
          Sum({<Month = {'Jan'}>} [Pieces Produced]),
          [Part#]
     )
)

Check out my latest posts at datavoyagers.net
Ross1991
Contributor III
Contributor III
Author

Hello  @igoralcantara ,    Instead of giving me an average of either January or February,   formula gave me sum of each part number for both months combined.  For example, it gave Part A  Quantity Produced sum of January and February combined.  Regardless of month I put in the formula, it gives sum of two month. This is what i wrote as a formula,

Avg(Aggr(Sum({<Month={'Jan'}>}[Pieces Produced]),[Part#]))

 

TauseefKhan
Creator III
Creator III

Hi @Ross1991,

Check this one:

Avg(Aggr(Sum({<Month = {"$(=GetFieldSelections(Month))"}>}[Pieces Produced]), [Part#]))


**********Hope this resolve your issue.
If the issue is solved please mark the answer with Accept as Solution & like it.********

TauseefKhan
Creator III
Creator III

To dynamically calculate the average production of each part while handling month selections, use the following expression:

Avg(
Aggr(
Sum(TOTAL <[Part#]> [Pieces Produced]) / Count(TOTAL <[Part#]> DISTINCT [Pieces Produced]),
[Part#]
)
)

Required output :
When you selected the month of January: (450+500)/2= 475.

TauseefKhan_1-1716871734929.png

**********Hope this resolve your issue.
If the issue is solved please mark the answer with Accept as Solution & like it.********

 

TauseefKhan
Creator III
Creator III

To calculate the average production of each part while handling month selections, use the following expression:

Avg(
Aggr(
Sum(TOTAL <[Part#]> [Pieces Produced]) / Count(TOTAL <[Part#]> DISTINCT [Pieces Produced]),
[Part#]
)
)

Required output:
When you selected the month of January : (450+500)/2= 475

TauseefKhan_0-1716872661476.png

 

 

**********Hope this resolve your issue.
If the issue is solved please mark the answer with Accept as Solution & like it.********

 

Ross1991
Contributor III
Contributor III
Author

Avg(
Aggr(
Sum(TOTAL <[Part#]> [Pieces Produced]) / Count(TOTAL <[Part#]> DISTINCT [Pieces Produced]),
[Part#]
)
)

above formula looks promising.   I don't know where to or how to select month of "Jan" or "Feb" because my date format is 1/5/2024 for example.  I was able to use Date Picker and get the desired result by simple Avg [Pieces Produced] formula.   Can you tell me or send me the screen shot how did you select Jan?  I do see you got 475 above.   Thank you.

TauseefKhan
Creator III
Creator III

You can use the Month() Function: Month(Yourdatefield) or calendar in your script.

Table_GetFieldselect:
LOAD
    Date(Date#(Date,'M/D/YYYY'),'YYYY-MM-DD') as Date,
    Resource,
    [Part#],
    [Pieces Produced],
    Month(Date(Date#(Date,'M/D/YYYY'),'YYYY-MM-DD')) as Month
INLINE [
    Date, Resource, Part#, Pieces Produced
    01/05/2024, B106, E, 450
    01/29/2024, B106, E, 500
    02/23/2024, B106, E, 800
    03/05/2024, B106, A, 050
    04/29/2024, B106, A, 200
    03/23/2024, B106, B, 100
 
];
Ross1991
Contributor III
Contributor III
Author

You are amazing. It worked. Used Filter. Thank you. I used lot more data set for my practice. It is working. Did not know about Date in the filter like = Month()



[cid:image001.png@01DAB14E.065B40A0]


Ross1991
Contributor III
Contributor III
Author

@TauseefKhan Thanks again.   I removed DISTINCT from the formula because I had lot of repeating values in my data.   Now it is working when I selected individual month or all months.  It looks like below.   

 

Avg(
Aggr(
Sum(TOTAL <[Part#]> [Pieces Produced]) / Count(TOTAL <[Part#]> [Pieces Produced]),
[Part#]
)
)