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.
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 ID | Datetime | Package ID |
1 | 1/1/2017 10:38 | 12 |
1 | 1/2/2017 7:05 | 2 |
1 | 2/1/2017 11:11 | 26 |
2 | 1/1/2017 10:38 | 12 |
2 | 1/2/2017 7:05 | 2 |
3 | 1/1/2017 10:38 | 12 |
3 | 1/2/2017 7:05 | 2 |
3 | 2/1/2017 11:11 | 26 |
3 | 2/1/2017 12:06 | 15 |
Thanks for your help!
Betsy
Solved! Go to 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
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
110 | |
100 | |
80 | |
64 | |
58 |
User | Count |
---|---|
148 | |
111 | |
94 | |
84 | |
67 |