Forum Discussion
Get latest records between two dates
Hi everybody,
first of all thank you for your support in helping people and me in getting better in PowerBI.
I tried to search for similar posts but can't figure out how to solve this.
I'm building a report in which I need the latest values for each Store in which the start date is the beginning of the activity I'm tracking, and the end date is the Sunday of a given week number.
I've built a table with the matrix of the weeks and the period of competence of that week
| YearWeek | Week | First day date | Last day date |
| 2022.52 | 52 | 01/01/1900 | 01/01/2023 |
| 2023.01 | 01 | 01/01/1900 | 08/01/2023 |
| 2023.02 | 02 | 01/01/1900 | 15/01/2023 |
| 2023.03 | 03 | 01/01/1900 | 22/01/2023 |
| 2023.04 | 04 | 01/01/1900 | 29/01/2023 |
| 2023.05 | 05 | 01/01/1900 | 05/02/2023 |
| 2023.06 | 06 | 01/01/1900 | 12/02/2023 |
| 2023.07 | 07 | 01/01/1900 | 19/02/2023 |
| … | … | … | … |
This is the dataset of the records:
| Store | Value | Date |
| ST01 | 111 | 05/04/2023 |
| ST02 | 112 | 05/04/2023 |
| ST03 | 113 | 06/04/2023 |
| ST01 | 114 | 12/04/2023 |
| ST02 | 115 | 19/04/2023 |
| ST03 | 116 | 26/04/2023 |
| ST01 | 117 | 26/04/2023 |
| ST02 | 118 | 27/04/2023 |
| ST03 | 119 | 28/04/2023 |
I need to add a filter by Yearweek in which:
If I select Yearweek 2023.14 the dataset shows me the records for each store that are between the First day date and the Last day date
| Store | Value | Data |
| ST01 | 111 | 05/04/2023 |
| ST02 | 112 | 05/04/2023 |
| ST03 | 113 | 06/04/2023 |
and so on for 2023.15
| Store | Value | Data |
| ST01 | 114 | 12/04/2023 |
| ST02 | 112 | 05/04/2023 |
| ST03 | 113 | 06/04/2023 |
2023.16
| Store | Value | Data |
| ST01 | 114 | 12/04/2023 |
| ST02 | 115 | 19/04/2023 |
| ST03 | 113 | 06/04/2023 |
and 2023.17
| Store | Value | Data |
| ST01 | 117 | 26/04/2023 |
| ST02 | 118 | 27/04/2023 |
| ST03 | 119 | 28/04/2023 |
please note that in the dataset for store ST03 there are 2 records that belong to week 2023.17 (one in date 26/04/2023, the other one 28/04/2023). The filter should take into consideration the latest in the period, so 28/04/2023.
I have no clues to do this in this specific dynamic way.
Hope you can help me
Thank you!
Hi,
Please check the below picture and the attached pbix file.
Value measure: = VAR _latestwkdate = MAX ( 'Calendar'[Wkenddate] ) VAR _latestdatadate = MAXX ( FILTER ( Data, Data[Date] <= _latestwkdate ), Data[Date] ) VAR _result = CALCULATE ( SUM ( Data[Value] ), Data[Date] = _latestdatadate ) RETURN IF ( HASONEVALUE ( Store[Store] ), _result )Date measure: = VAR _latestwkdate = MAX ( 'Calendar'[Wkenddate] ) VAR _latestdatadate = MAXX ( FILTER ( Data, Data[Date] <= _latestwkdate ), Data[Date] ) RETURN IF ( HASONEVALUE ( Store[Store] ), _latestdatadate )
3 Replies
- Jihwan_KimSuper User
Hi,
Please check the below picture and the attached pbix file.
Value measure: = VAR _latestwkdate = MAX ( 'Calendar'[Wkenddate] ) VAR _latestdatadate = MAXX ( FILTER ( Data, Data[Date] <= _latestwkdate ), Data[Date] ) VAR _result = CALCULATE ( SUM ( Data[Value] ), Data[Date] = _latestdatadate ) RETURN IF ( HASONEVALUE ( Store[Store] ), _result )Date measure: = VAR _latestwkdate = MAX ( 'Calendar'[Wkenddate] ) VAR _latestdatadate = MAXX ( FILTER ( Data, Data[Date] <= _latestwkdate ), Data[Date] ) RETURN IF ( HASONEVALUE ( Store[Store] ), _latestdatadate )- AnonymousNot applicable
Hi Kim,
Thank you very much for the descriptions, screenshots and PBIX example! 🙏
This is EXACTLY what I was looking for! You gave me a big hand in this!
Now I just need to link this behaviour to some measures I've created in order to take just the data related to the latest "Value Measure" shown in the filter...
Thanks again!
Have a nice day!
- AnonymousNot applicable
Hi again Kim,
sorry if I return on this but can't make it further.
Unfortunately getting the latest Value with a dedicated measure is not enough for my purposes, as I need to analyze answers to some questions just for the latest Value in the filter that all belong to Data table.
This is the "extended" dataset:
Store Value Date Question 1 Question 2 Question 3 Question 4 ST01 111 05/04/2023 Yes Yes Yes Yes ST01 114 12/04/2023 No Yes Yes Yes ST01 117 26/04/2023 Yes Yes Yes Yes ST02 112 05/04/2023 Yes Yes Yes Yes ST02 115 19/04/2023 Yes No Yes Yes ST02 118 27/04/2023 Yes Yes Yes Yes ST03 113 06/04/2023 Yes Yes Yes Yes ST03 116 26/04/2023 Yes No Yes Yes ST03 119 28/04/2023 Yes Yes Yes Yes I think I have asked you support without considering the fact that all the analysis that follow must be taken just for the rows that have the latest Value measure: that comes from the YearWk filter.
I also need to obtain with this filter is to calculate a Compliance column that gives me "Yes" if all the questions 1, 2, 3 and 4 are "Yes". If at least one of the Questions is "No", the measure should give me "No" as a result.
The column should be quite easy to do:
Compliance = IF(Data[Question 1]="Yes" && Data[Question 2]="Yes" && Data[Question 3]="Yes" && Data[Question 4]="Yes" ,"Yes","No")If I create a pie chart with the Compliance column in it, it takes all the values of the dataset (9 rows in the example) and not just the result of the Compliance for just the Value measure: result that comes with your suggested solution.If I link the Data[Date] and Calendar[Date] together, it just gives me the results of the rows that belong to that week and not the latest results for each store that are between Wkstartdate and Wkenddate
2023.14
2023.15
2023.16
2023.17
Completely wrong 😭At Table Visual point of view also this is what happens.
When I add these to the table and filter to 2023.14, everything looks fine
Store Date measure: Value measure: Question 1 Question 2 Question 3 Question 4 ST01 05/04/2023 111 Yes Yes Yes Yes ST02 05/04/2023 112 Yes Yes Yes Yes ST03 06/04/2023 113 Yes Yes Yes Yes If I filter to 2023.15, the row with Value measure: 111 is still there, but it should not be there as I need just to take the latest one, so it should show just the Value measure: 114 for ST01
Store Date measure: Value measure: Question 1 Question 2 Question 3 Question 4 ST01 12/04/2023 114 No Yes Yes Yes ST01 05/04/2023 111 Yes Yes Yes Yes ST02 05/04/2023 112 Yes Yes Yes Yes ST03 06/04/2023 113 Yes Yes Yes Yes Maybe it's correct from a Table Visual point of view because the Question 1 is different between Value 114 and Value 111.
It gets more complicated when filtering 2023.16.
Store Date measure: Value measure: Question 1 Question 2 Question 3 Question 4 ST01 12/04/2023 114 No Yes Yes Yes ST01 05/04/2023 111 Yes Yes Yes Yes ST02 19/04/2023 115 Yes No Yes Yes ST02 05/04/2023 112 Yes Yes Yes Yes ST03 06/04/2023 113 Yes Yes Yes Yes Values 111 and 112 should not be there. But also here the Question 1 is different between Value 114 and Value 111 and Question 2 is different between Value 115 and Value 112.
What I need to obtain is something like this:
2023.14
2023.15
2023.16
2023.17
I hope my goal is clearer now. Maybe the solution is easier than I can expect but honestly it's been 3 days I'm beating my head against a brick wall without finding a satisfying solution.
Hope you can help with this!
I attach the pbix as well
https://drive.google.com/file/d/116C9igi6QuQMwudVnlIZr_-IW_zRw0Ne/view?usp=share_link
Thank you so much in advance for your great support!