Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

Average By Columns with Filter and Dates

Hello, I have many columns and I would like to average using a filter and certain columns. So I want the "Value" field to be the average by Analyte, but only when AnalyteType = "Lab", and also only for dates within the past 1.5 months (ideally, I want it to only use the last 12 months, but I didn't make a large enough sample dataset here).

 

I was trying to use the ALLEXCEPT, but I couldn't figure out how to select the AnalyteType = "Lab" nor could I figure out how to keep the date updated everytime I reload the data (which will happen once per week).

 

AnalyteType   Analyte       Value   DateEntered   Average
LabBalloons512-02-20225
FieldBalloons612-03-20225
OutsideBalloons412-04-20225
LabFaces211-05-20212
FieldFaces311-06-20212
OutsideFaces111-07-20212
LabChairs1110-01-202115
FieldChairs1210-02-202115
OutsideChairs1710-03-202115
LabChairs1512-02-202215
FieldChairs1212-03-202215
OutsideChairs1812-04-202215

14 Replies

  • Avg = calculate(AVERAGE('Table'[Value]),'Table'[AnalyteType]="Lab",'Table'[DateEntered]>TODAY()-45)

    Note that your sample result of "2" is not valid.

    • Anonymous's avatar
      Anonymous
      Not applicable

      This is the simplest formula. What if I need to add one more column to average by? So not a specific value, but I also want to average by color.

      • Anonymous's avatar
        Anonymous
        Not applicable

        I can easily make these all in a table on the report, but then I don't know how to create a plot from a table in the report area. That might be the easiest thing to do. Is that possible?

    • Anonymous's avatar
      Anonymous
      Not applicable

      This doesn't have "Analyte" in the formula, even though we want the final values averaged by analyte (as shown in the "Avg" column in my example.

       

      I tried this equation and it isn't giving the correct values. Thanks.

       

  • daXtreme's avatar
    daXtreme
    Icon for Solution Sage rankSolution Sage

    Are you talking about a measure or a calculated column? Also, what does it mean "only for dates within the past 1.5 months"? For a group that you'd use for the calc of average, there may be many different dates. Which dates are you then talking about for a group of rows? And what happens if at least one of the rows in the group that you'd like to calc the average for has a date that's not within 1.5 months from now?

    • Anonymous's avatar
      Anonymous
      Not applicable

      I don't know the difference between a measured and calculated column. I actually want the data to be averaged from the past year.

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        calculated column:  is computed once during dataset refresh, and stored as part of the data model. Is not impacted by user choices.

        measure: is computed on the fly whenever the user interacts with the report in a way that includes that measure. Is not stored in the data model. Is impacted by user choices/filters.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  Anonymous ,

    I created some data:

    Here are the steps you can follow:

    1. Create measure.

    Measure =
    AVERAGEX(FILTER(ALL('Table'),
    'Table'[AnalyteType]="Lab"&&'Table'[Analyte]=MAX('Table'[Analyte])&&
    'Table'[DateEntered]>=
    DATE(YEAR(MAX('Table'[DateEntered]))-1,MONTH(MAX('Table'[DateEntered])),DAY(MAX('Table'[DateEntered])))&&
    'Table'[DateEntered]<=MAX('Table'[DateEntered]))
    ,[Value])

    2. Result:

     

    Best Regards,

    Liu Yang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

    • Anonymous's avatar
      Anonymous
      Not applicable

      The final column does not look correct. Within each "Analyte", there should be the same value. So "Balloons" should have only the "Lab" values averaged in the final column, and it should only be one value there, just repeated, like how I have it in the example in the column "Average".

  • Anonymous's avatar
    Anonymous
    Not applicable

    For the final plot, I would like a clustered column chart, but since each Analyte might have really different scales, the plot would be too hard to see if the "lab" value is any different than the "outside" value. So I want to scale everything so the lab value is set to 100% and the "outside" and "field" values then show the percent difference compared with the "lab" values. So if I have a column of the average of "lab" values, then I can divide the actual values by that column and create the plot based on averages by "AnalyteType".

     

    In the "table" function of the report, I can create averages by AnalyteType with the Standard Deviation as well, even applying the filter of which years to select from the page. I just don't know how to translate that to a plot. I would still need to divide the average by the "lab" average though.

    • Anonymous's avatar
      Anonymous
      Not applicable

      I was able to get this working. I created two columns. One column has the "Lab" averages, and one has the group average, then I divide the group by the lab to get the scaled difference. Here is the code I used to create the "lab" average:

      Column1 = calculate(average(Main[Value]),filter(Main,Main[Analyte]=EARLIER(Main[Analyte])),filter(Main,Main[Color]=EARLIER(Main[Color])),filter(Main,Main[AnalyteType]="Lab"),filter(Main,Main[DateEnter]>today()-365))
       
      Thanks a bunch for your help.