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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Prior Row Calculation in Measure

I have two tables:

  1. Student. This table joins to the Exam table on [id], 1:*
  2. Exam.

I am trying to create a measure to calculate the average datediff between each students exam dates.  Unfortunately, I am struggling to create a table for AVERAGEX to iterate over because I cannot create a "Prior_Exam_Date" column.  See my progress below:

 

Average Days Between Exams:=
AVERAGEX (
    SUMMARIZECOLUMNS (
        Student[id],
        Exam[Exam_Name],
        Exam[Exam_Date],
        "Prior_Exam_Date", ??? // this measure should return the most recent available Exam[Exam_Date] for the current Student[id]. 
    ),
    DATEDIFF ( [Prior_Exam_Date], [Exam_Date], DAY )
)

 

I understand that I can easily add a physical "Prior_Exam_Date" column to my dataset to accomplish this however in this case I must calculate it on the fly.  Can you please help me fill in the ??? above?


Thanks,
Simon

1 ACCEPTED SOLUTION
v-haibl-msft
Employee
Employee

@Anonymous

 

Without using calculated column, I create some measures to get the expected result in attached sample file.

 

Prior_Exam_Date = 
CALCULATE (
    MAX ( Exam[Exam_Date] ),
    FILTER (
        ALLEXCEPT ( Exam, Exam[id] ),
        Exam[Exam_Date] < MAX ( Exam[Exam_Date] )
    )
)
DayDiff = 
DATEDIFF ( [Prior_Exam_Date], CALCULATE ( MAX ( Exam[Exam_Date] ) ), DAY )
Average_DayDiff = 
AVERAGEX ( ALLEXCEPT ( Exam, Exam[id] ), [DayDiff] )

Prior Row Calculation in Measure_1.jpg

 

Best Regards,
Herbert

View solution in original post

2 REPLIES 2
v-haibl-msft
Employee
Employee

@Anonymous

 

Without using calculated column, I create some measures to get the expected result in attached sample file.

 

Prior_Exam_Date = 
CALCULATE (
    MAX ( Exam[Exam_Date] ),
    FILTER (
        ALLEXCEPT ( Exam, Exam[id] ),
        Exam[Exam_Date] < MAX ( Exam[Exam_Date] )
    )
)
DayDiff = 
DATEDIFF ( [Prior_Exam_Date], CALCULATE ( MAX ( Exam[Exam_Date] ) ), DAY )
Average_DayDiff = 
AVERAGEX ( ALLEXCEPT ( Exam, Exam[id] ), [DayDiff] )

Prior Row Calculation in Measure_1.jpg

 

Best Regards,
Herbert

Interkoubess
Solution Sage
Solution Sage

Hi @Anonymous,

 

Could you please send ( share)  dummy data with expected results and then we can test different approaches.

 

Thank you.

 

Ninter

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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