Forum Discussion
Filter data by latest date of data
Hi folks,
a couple of months ago i posted a question where i asked how to sum values according to another column's values, the info i posted was about how many distance a determined vehicle traveled vs how many fuel it consumed, that question was solved, but now in my job i was asked a more specific task, which is like this:
Let's say A car loaded fuel 3 times during month, for example dec. 3, dec. 9, and today (dec. 19) it loaded 20 liters every time it did, and also it's distance was stored in a separated table, so, what i have to do is sum up to the latest date of fuel load of a determined car, the main issue i'm experiencing since the distance a vehicle traveled is in a separated table from the fuel consumption, is that the kms are readed up to the last day of month instead of last date of fuel load, i'll put a graphic example so you can see what i mean:
KMS TABLE
| id | date | license plate | kms |
| 1 | 12-07-2022 | ABC123 | 100 |
| 2 | 12-10-2022 | ABC123 | 70 |
| 3 | 12-13-2022 | ABC123 | 40 |
| 4 | 12-08-2022 | DEF456 | 50 |
| 5 | 12-12-2022 | DEF456 | 30 |
| 6 | 12-19-2022 | DEF456 | 70 |
FUEL CONSUMPTION TABLE
| id | date | license plate | amount |
| 1 | 12-01-2022 | ABC123 | 120 |
| 2 | 12-10-2022 | ABC123 | 65 |
| 3 | 12-13-2022 | DEF456 | 60 |
| 4 | 12-17-2022 | DEF456 | 35 |
COMBINED RESULT
| date | license plate | fuel consumption | traveled distance |
| 10-12-2022 | ABC123 | 185 | 210 |
| 17-12-2022 | DEF456 | 95 | 150 |
DESIRED RESULT
| date | license plate | fuel consumption | traveled distance |
| 10-12-2022 | ABC123 | 185 | 170 |
| 17-12-2022 | DEF456 | 95 | 80 |
what i forgot to mention that is important, is that i had to put 2 separate date filters, with only 1, since there are 2 different dates in different tables, the date is sorted by the table it belongs to, for example if i use the date of fuel consuption it only sorts the fuel consumptions and messing up the distances of travels, also, i would like to point out that the traveled distance table will always have more rows since it picks up info every day unlike fuel consumption
Any doubts about this question i'll answer it ASAP, thanks in advance for any help you could provide me
- Anonymous3 years ago
Hi Anonymous ,
I created a sample pbix file(see the attachment), please check if that is what you want. You can create the measures below to get it:
Last date of fuel load = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( MAX ( 'FUEL CONSUMPTION'[date] ), FILTER ( ALLSELECTED ( 'FUEL CONSUMPTION' ), 'FUEL CONSUMPTION'[license plate] = _lplate ) )fuel consumption = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( SUM ( 'FUEL CONSUMPTION'[amount] ), FILTER ( ALLSELECTED ( 'FUEL CONSUMPTION' ), 'FUEL CONSUMPTION'[license plate] = _lplate && 'FUEL CONSUMPTION'[date] <= [Last date of fuel load] ) )traveled distance = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( SUM ( 'KMS'[kms] ), FILTER ( ALLSELECTED ( 'KMS' ), 'KMS'[license plate] = _lplate && 'KMS'[date] <= [Last date of fuel load] ) )Best Regards
2 Replies
- AnonymousNot applicable
Hi Anonymous ,
I created a sample pbix file(see the attachment), please check if that is what you want. You can create the measures below to get it:
Last date of fuel load = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( MAX ( 'FUEL CONSUMPTION'[date] ), FILTER ( ALLSELECTED ( 'FUEL CONSUMPTION' ), 'FUEL CONSUMPTION'[license plate] = _lplate ) )fuel consumption = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( SUM ( 'FUEL CONSUMPTION'[amount] ), FILTER ( ALLSELECTED ( 'FUEL CONSUMPTION' ), 'FUEL CONSUMPTION'[license plate] = _lplate && 'FUEL CONSUMPTION'[date] <= [Last date of fuel load] ) )traveled distance = VAR _lplate = SELECTEDVALUE ( 'FUEL CONSUMPTION'[license plate] ) RETURN CALCULATE ( SUM ( 'KMS'[kms] ), FILTER ( ALLSELECTED ( 'KMS' ), 'KMS'[license plate] = _lplate && 'KMS'[date] <= [Last date of fuel load] ) )Best Regards
- AnonymousNot applicable
Hi!, thanks for your answer, it works fine, however i was told that it has to be made from the last fuel reload date of the last month, how can i do that?