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
req77
Helper I
Helper I

Show only most recent row for each ID

I have a table that follows this structure

ID_ClassDate of conclusion
1111-Class A   13/04/2022
1111-Class B20/04/2022
2222-Class A26/07/2022
2222-Class A30/09/2022
2222-Class B14/02/2022
3333-Class A20/03/2022

The ID_Class column combines the person's unique id and the name of the class they attended. But sometimes there are people who do the same class twice or more but on different dates.
I wanted to create a column of TRUE and FALSE that would bring me the TRUE value whenever each distinct ID_Class is the most recent (since I want to fetch only the most recent courses taken by each person. If a person took the same course twice, I want to see only the most recent of these two courses).


The expected result would be something like this:

ID_ClassDate of conclusion Most_Recent
1111-Class A   13/04/2022TRUE
1111-Class B20/04/2022TRUE
2222-Class A26/07/2022FALSE
2222-Class A30/09/2022TRUE
2222-Class B14/02/2022TRUE
3333-Class A20/03/2022TRUE
3333-Class A22/01/2022FALSE



How can I do this?

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@req77  you need a measure like following

smpa01_0-1675276539482.png

Measure = 
VAR dt =
    MAX ( 'fact'[dt] )
VAR mxDt =
    CALCULATE ( MAX ( 'fact'[dt] ), ALLEXCEPT ( 'fact', 'fact'[ID_Class] ) )
RETURN
    SWITCH ( TRUE (), dt = mxDt, TRUE (), FALSE () )

 

@Sahir_Maharaj  DAX rule of thumb,  don't create calculated columns if you can create a measure

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
smpa01
Super User
Super User

@req77  you need a measure like following

smpa01_0-1675276539482.png

Measure = 
VAR dt =
    MAX ( 'fact'[dt] )
VAR mxDt =
    CALCULATE ( MAX ( 'fact'[dt] ), ALLEXCEPT ( 'fact', 'fact'[ID_Class] ) )
RETURN
    SWITCH ( TRUE (), dt = mxDt, TRUE (), FALSE () )

 

@Sahir_Maharaj  DAX rule of thumb,  don't create calculated columns if you can create a measure

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Sahir_Maharaj
Super User
Super User

Hi @req77,

 

Yes, you're correct. The [Date of conclusion] column should be a column in your table, not a measure. In a DAX formula for a calculated column, you can reference other columns in the same table, but you can't reference measures.

 

So, in this case, you need to make sure that the [Date of conclusion] column is a column in your table and not a measure. Then, you can reference it in your DAX formula to compare the date of conclusion for the current row with the date of conclusion for other rows with the same ID_Class.

 

Let me know if this works.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
Sahir_Maharaj
Super User
Super User

This formula uses a calculated column that creates a variable CurrentIDClass that stores the value of the ID_Class column for the current row. Then, it returns TRUE if the date of conclusion for the current row is equal to the minimum date of conclusion for all rows with the same ID_Class. Otherwise, it returns FALSE.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
Sahir_Maharaj
Super User
Super User

You can use the following DAX formula to achieve the desired result:

 

Most_Recent = VAR CurrentIDClass = MIN('Table'[ID_Class]) RETURN IF(CurrentIDClass = MIN(FILTER('Table', 'Table'[ID_Class] = MIN('Table'[ID_Class]))[Date of conclusion]), TRUE, FALSE)

Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Hi, @Sahir_Maharaj  Thanks for the quick response! 🙂

I didn't quite understand the

[Date of conclusion]

part in your code. Does it use a measure for the date of conclusion? Because when I try to do it here, I can't pull the "Date of conclusion" column of my table, it only lets me bring measures.

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.