Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
blueandgold
Frequent Visitor

Retrieve average of earliest and most recent values from one table based on date slicer

We have a single table of students with test scores.  Columns include createdon (the date the test is taken), testscore, and contactid.  We want to create a visual that displays the average first test score and the average most recent test score by all students, so that we can show improvements in test scores by students that take our course.  Here's a few conditions.

 

  1. Each student included in the calculation must have taken the test at least twice, so we can calculate an average first time score and an average latest time score across all students who have taken the test at least twice during the date slicer range
  2. We have a date slicer based on createdon date, so that if somone took the test once a month for 12 months in a row, but we have the date slicer start date be before their first test date and have the date slicer end date after their third test date but before their fourth, the test dates we want to compare are their first test and their third test
  3. Since we're using the createdon value to determine the date of the test, it's actually a date/time field (I think, right?)

Here's some example data to help.

 

testidcontactidcreatedontestscore
1AJanuary 2, 202160
2BJanuary 2, 202187
3CJanuary 2, 202170
4AFebruary 1, 202168
5AFebruary 3, 202196
6BFebruary 5, 202180
7BFebruary 15, 202199

 

So, if the date slicer for the page has start date of January 1, 2021 and an end date of February 14, 2021, we only want the following values to be in the calculation we run....

 

testidcontactidcreatedontestscore
1AJanuary 2, 202160
2BJanuary 2, 202187
5AFebruary 3, 202196
6BFebruary 5, 202180

 

....where we get the average of the earliest pairs of scores ((60+87)/2 = 73.5) and we get the average of the latest test scores ((96+80)/2 = 88) to be our sought after values.

 

Any hints or ideas on how I can get from here to there?  We're trying to do this with DirectQuery, but if we need to use Import, we can make the switch.  Thanks so much in advance!

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@blueandgold Hmm. Well, since there is a date slicer involved probably better be a measure. Maybe something like the following:

Measure Min Date Average =
  VAR __Contacts = 
    SELECTCOLUMNS(
      FILTER(SUMMARIZE('Table',[contactid],"__Count",COUNTROWS('Table')),[__Count] > 1),
      "__contactid",[contactid]
    )
  VAR __Table = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        SELECTCOLUMNS(
          FILTER('Table',[contactid] IN __Contacts),
          "__contactid",[contactid],
        ),
        "__Date" = MINX(FILTER('Table','Table'[contactid]=[__contactid]),[createdon])
      ),
      "__Score",MINX(FILTER('Table','Table'[contactid] = [__contactid] && [createdon] = [__Date]
    )
RETURN
  AVERAGEX(__Table,[__Score])


Measure Max Date Average =
  VAR __Contacts = 
    SELECTCOLUMNS(
      FILTER(SUMMARIZE('Table',[contactid],"__Count",COUNTROWS('Table')),[__Count] > 1),
      "__contactid",[contactid]
    )
  VAR __Table = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        SELECTCOLUMNS(
          FILTER('Table',[contactid] IN __Contacts),
          "__contactid",[contactid],
        ),
        "__Date" = MAXX(FILTER('Table','Table'[contactid]=[__contactid]),[createdon])
      ),
      "__Score",MAXX(FILTER('Table','Table'[contactid] = [__contactid] && [createdon] = [__Date]
    )
RETURN
  AVERAGEX(__Table,[__Score])



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

@blueandgold Hmm. Well, since there is a date slicer involved probably better be a measure. Maybe something like the following:

Measure Min Date Average =
  VAR __Contacts = 
    SELECTCOLUMNS(
      FILTER(SUMMARIZE('Table',[contactid],"__Count",COUNTROWS('Table')),[__Count] > 1),
      "__contactid",[contactid]
    )
  VAR __Table = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        SELECTCOLUMNS(
          FILTER('Table',[contactid] IN __Contacts),
          "__contactid",[contactid],
        ),
        "__Date" = MINX(FILTER('Table','Table'[contactid]=[__contactid]),[createdon])
      ),
      "__Score",MINX(FILTER('Table','Table'[contactid] = [__contactid] && [createdon] = [__Date]
    )
RETURN
  AVERAGEX(__Table,[__Score])


Measure Max Date Average =
  VAR __Contacts = 
    SELECTCOLUMNS(
      FILTER(SUMMARIZE('Table',[contactid],"__Count",COUNTROWS('Table')),[__Count] > 1),
      "__contactid",[contactid]
    )
  VAR __Table = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        SELECTCOLUMNS(
          FILTER('Table',[contactid] IN __Contacts),
          "__contactid",[contactid],
        ),
        "__Date" = MAXX(FILTER('Table','Table'[contactid]=[__contactid]),[createdon])
      ),
      "__Score",MAXX(FILTER('Table','Table'[contactid] = [__contactid] && [createdon] = [__Date]
    )
RETURN
  AVERAGEX(__Table,[__Score])



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler thank you so much for the detailed solution!  When implementing this, I'm getting the following error for the Measure Max Date Average:

Argument '4' in SELECTCOLUMNS function is required.

Any idea what I might be missing?

@blueandgold Likely missing an ending ) somewhere. Perhaps try running the code through daxformatter.com to find the issue.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler that was it! This has worked beautifully, thank you so much!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.