Forum Discussion
Tracking status changes by date
Hi, I'm working with student IDs and their statuses which change over time, with the date of the change given, where each student can have multiple rows of changes, as follows:
| Student Id | Status | Date of Status change |
| Student1 | A | 01/02/2020 |
| Student1 | B | 13/02/2020 |
| Student1 | C | 15/02/2020 |
| Student2 | A | 05/02/2020 |
| Student2 | B | 12/02/2020 |
| Student2 | C | 14/02/2020 |
| Student3 | A | 02/02/2020 |
| Student3 | B | 03/02/2020 |
I'd like to be able to count at each date how many students are on each status, so when they change status the total in one column will reduce by 1 and in the next will increase by 1, as the following example:
| Count of Status | |||
| Date | A | B | C |
| 01/02/2020 | 1 | 0 | 0 |
| 02/02/2020 | 2 | 0 | 0 |
| 03/02/2020 | 1 | 1 | 0 |
| 04/02/2020 | 1 | 1 | 0 |
| 05/02/2020 | 2 | 1 | 0 |
| 06/02/2020 | 2 | 1 | 0 |
| 07/02/2020 | 2 | 1 | 0 |
| 08/02/2020 | 2 | 1 | 0 |
| 09/02/2020 | 2 | 1 | 0 |
| 10/02/2020 | 2 | 1 | 0 |
| 11/02/2020 | 2 | 1 | 0 |
| 12/02/2020 | 1 | 2 | 0 |
| 13/02/2020 | 0 | 3 | 0 |
| 14/02/2020 | 0 | 2 | 1 |
| 15/02/2020 | 0 | 1 | 2 |
I would then use this in a line chart (date linked to existing date table) in order to show changes over time.
Any advice on how to do this would be much appreciated, thanks!
Hi Anonymous
Create a date table to be used in the axis (no relationship to the fact table) and create this measure. You can use it in a matrix visual or a chart. See it all at work in the attached file.
Measure = VAR currentDate_ = MAX ( DateT[Date] ) VAR currentStatus_ = DISTINCT ( Table1[Status] ) VAR auxT_ = CALCULATETABLE ( ADDCOLUMNS ( ALL ( Table1[Student Id] ), "@res", VAR lastDate_ = CALCULATE ( MAX ( Table1[Date of Status change] ), Table1[Date of Status change] <= currentDate_ ) RETURN CALCULATE ( DISTINCT ( Table1[Status] ), Table1[Date of Status change] = lastDate_ ) ), ALL ( Table1[Status] ) ) RETURN COUNTROWS ( FILTER ( auxT_, [@res] IN currentStatus_ ) )Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
Anonymous
Most likely it has to do with the DISTINCT ( ) in this bit:
CALCULATE ( DISTINCT ( Table1[Status] ), Table1[Date of Status change] = lastDate_ )It probably means there is more than one Status for the latest data and that student. Check if there are cases like that and see how you want to solve them. Perhaps with MAX, or perhaps you need to clean the data first
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
4 Replies
- AlB
Community Champion
Hi Anonymous
Create a date table to be used in the axis (no relationship to the fact table) and create this measure. You can use it in a matrix visual or a chart. See it all at work in the attached file.
Measure = VAR currentDate_ = MAX ( DateT[Date] ) VAR currentStatus_ = DISTINCT ( Table1[Status] ) VAR auxT_ = CALCULATETABLE ( ADDCOLUMNS ( ALL ( Table1[Student Id] ), "@res", VAR lastDate_ = CALCULATE ( MAX ( Table1[Date of Status change] ), Table1[Date of Status change] <= currentDate_ ) RETURN CALCULATE ( DISTINCT ( Table1[Status] ), Table1[Date of Status change] = lastDate_ ) ), ALL ( Table1[Status] ) ) RETURN COUNTROWS ( FILTER ( auxT_, [@res] IN currentStatus_ ) )Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
- AnonymousNot applicable
Thanks for the quick response AlB - I've added the measure in but am getting an error message that 'A table of multiple values was supplied where a single value was expected'.
Do I need to use FIRSTNONBLANK at all? There are null values in both Status and Date Change columns.
Any help you can give? Thanks
- AlB
Community Champion
Anonymous
Most likely it has to do with the DISTINCT ( ) in this bit:
CALCULATE ( DISTINCT ( Table1[Status] ), Table1[Date of Status change] = lastDate_ )It probably means there is more than one Status for the latest data and that student. Check if there are cases like that and see how you want to solve them. Perhaps with MAX, or perhaps you need to clean the data first
Please mark the question solved when done and consider giving a thumbs up if posts are helpful.
Contact me privately for support with any larger-scale BI needs, tutoring, etc.
Cheers
- AnonymousNot applicable
Thank you AlB, this works perfectly for my line chart once I cleaned the data up, and I've used it in a matrix too. Thanks for the solution!