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
Betsy
Helper IV
Helper IV

Measure to calculate last datetime and message id by student id?

Hi Everyone,

 

I'm looking to calculate the last message a student received. I've done this previously using group by in queries and creating a new table, but some changes in my model relationships are making this not play well with the rest. It's also a large table with 2 million rows with relationships to 5 other tables. Curious if anyone has measure syntax that would calculate last message? I've looked through prior similar questions and have only found table solutions.

 

Here's an example of what the data look like. I would want a measure that would give me for Student ID 1: 2/1/17 11:11, Package ID 26, for Student 2 1/2/17 07:05, Package ID 2, and for Student ID 3: 2/1/17 12:06, Package ID 15. 

 

Student IDDatetimePackage ID
11/1/2017 10:3812
11/2/2017 7:052
12/1/2017 11:1126
21/1/2017 10:3812
21/2/2017 7:052
31/1/2017 10:3812
31/2/2017 7:052
32/1/2017 11:1126
32/1/2017 12:0615

 

Thanks for your help!

 

Betsy

1 ACCEPTED SOLUTION

Yes, it was designed to do that:

 

CALCULATE (
    MAX ( Table1[Package ID] ),                             <- returns package id (max can be any function)
    ALLEXCEPT ( Table1, Table1[Student ID] ),       <- filters table for just that packageid student id
    FILTER ( Table1, Table1[Datetime] = MAX ( Table1[Datetime] )   <- filters to the max date in this new table

)

 

Essentially, it is looking at the table and figuring out what the package id is for the max date, given the current context of student ids. When you add it to a table with student ids, you are giving it a row context, saying do this for each student id. When trying to do a count, you are removing this row context. You are now saying, calculate the latest package ID, but not for each student.

 

Instead, you can add a column for last package ID using the following column:

 

Last Package ID = 
       VAR studID = Table1[Student ID]
       VAR MaxDate =
CALCULATE(MAX(Table1[Datetime]),
FILTER(Table1,Table1[Student ID]=studID)) RETURN CALCULATE(
MAX(Table1[Package ID]) ,FILTER(Table1,Table1[Student ID]=studID) ,FILTER(Table1,Table1[Datetime]=MaxDate)
)

 

 

However, this will repeat the value a lot. This may be fine for you - just remember to use DO NOT SUMMARIZE when putting in a matrix. Depending on how else yoiu use your data, it may cause more problems.

 

 A better way, create a table with a unique list of students:

 

New Table:

 

Students = VALUES(Table1[Student ID])

 

Join this table to your ogirinal one on Student IDs.

 

 

Then, you can add the last id column into this new table, making a very small change:

 

Last Package ID =

VAR studID = Students[Student ID]
VAR MaxDate =
        CALCULATE (MAX ( Table1[Datetime] ),
        FILTER ( Table1, Table1[Student ID] = studID ) )

RETURN

    CALCULATE (
        MAX ( Table1[Package ID] ),
        FILTER ( Table1, Table1[Student ID] = studID ),
        FILTER ( Table1, Table1[Datetime] = MaxDate )
         )

 

 



Did I answer your question? Mark my post as a solution! Proud to be a Super User!


Connect with me!
Stay up to date on  
Read my blogs on  



View solution in original post

10 REPLIES 10

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.