Forum Discussion
Find the first latest date and second latest date based on quarter selection dynamically
Team,
I have a date (which doesn't have continues dates) and quarter fields in my table (mentioned below). My quarter will go as slicer/filter and I want to find the first latest date and the second latest date based on my quarter slicer selection.
| Date (DD/MM/YY) | Quarter | Flag | Comment |
| 02/05/24 | 24-Q2 | 1 | When I selected 24-Q2 |
| 18/04/24 | 24-Q2 | 2 | When I selected 24-Q2 |
| 11/04/24 | 24-Q2 | ||
| 31/03/24 | 24-Q1 | 1 | When I selected 24-Q1 |
| 29/03/24 | 24-Q1 | 2 | When I selected 24-Q1 |
| 26/03/24 | 24-Q1 | ||
| 25/03/24 | 24-Q1 | ||
| 21/03/24 | 24-Q1 | ||
| 19/03/24 | 24-Q1 | ||
| 14/03/24 | 24-Q1 | ||
| 07/03/24 | 24-Q1 | ||
| 29/02/24 | 24-Q1 | ||
| 15/02/24 | 24-Q1 | ||
| 31/12/23 | 23-Q4 | 1 | When I selected 23-Q4 |
| 14/12/23 | 23-Q4 | 2 | When I selected 23-Q4 |
Tomorrow when data gets updated and a new date gets added (as mentioned below), my latest and second latest date flag should automatically update
| Date (DD/MM/YY) | Quarter | Flag | Comment |
| 10/05/24 | 24-Q2 | 1 | New date added and the flag should show this as latest date When I selected 24-Q2 |
| 02/05/24 | 24-Q2 | 2 | Automatically this should be my second latest date When I selected 24-Q2 |
| 18/04/24 | 24-Q2 | ||
| 11/04/24 | 24-Q2 | ||
| 31/03/24 | 24-Q1 | 1 | When I selected 24-Q1 |
| 29/03/24 | 24-Q1 | 2 | When I selected 24-Q1 |
| 26/03/24 | 24-Q1 | ||
| 25/03/24 | 24-Q1 | ||
| 21/03/24 | 24-Q1 | ||
| 19/03/24 | 24-Q1 | ||
| 14/03/24 | 24-Q1 | ||
| 07/03/24 | 24-Q1 | ||
| 29/02/24 | 24-Q1 | ||
| 15/02/24 | 24-Q1 | ||
| 31/12/23 | 23-Q4 | 1 | When I selected 23-Q4 |
| 14/12/23 | 23-Q4 | 2 | When I selected 23-Q4 |
Can someone please help me with this requirement?
Thanks!
Hi AD_SNOW
To get the desired result you can apply an index of every date according to the quarter from PQ .The guide of how to get this is here :
https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query
(Row Number By Group)
I also attached a pbix , you can follow my steps
From the moment that you have an index this is easy, you can create a simple dax measure for the flag :
Flag = if(max('Table'[Index]) in {1,2}, MAX('Table'[Index]), " " )as I mentioned Pbix is attached 🙂
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
3 Replies
- Ritaf1983
Super User
Hi AD_SNOW
To get the desired result you can apply an index of every date according to the quarter from PQ .The guide of how to get this is here :
https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query
(Row Number By Group)
I also attached a pbix , you can follow my steps
From the moment that you have an index this is easy, you can create a simple dax measure for the flag :
Flag = if(max('Table'[Index]) in {1,2}, MAX('Table'[Index]), " " )as I mentioned Pbix is attached 🙂
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
- KeyurPatel14
Responsive Resident
Hello AD_SNOW ,
Hope you are doing well.
Could you please try the below measure:
(Please know that, I have considered the QTR from the same table, if you have a date dimension table, kindly make required change in the measure)
Max QTR Date =VAR maxdate =CALCULATE(MAX('Table (2)'[Date (DD/MM/YY)]),ALLEXCEPT('Table (2)','Table (2)'[Quarter]))VAR curdate =SELECTEDVALUE('Table (2)'[Date (DD/MM/YY)])VAR secondmaxdate =CALCULATE(MAX('Table (2)'[Date (DD/MM/YY)]),ALLEXCEPT('Table (2)','Table (2)'[Quarter]),'Table (2)'[Date (DD/MM/YY)] < maxdate)RETURNIF(maxdate = curdate,1,IF(curdate = secondmaxdate,2,0))
If this solution helps, then kindly give it a kudos and mark it as a solution. - AD_SNOW
Helper I
Thanks Both Ritaf1983 KeyurPatel14 . Both the solutions are working as expected. As Ritaf1983 posted first, I have accepted her solution.
Thanks both again, really appreciated!!