Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

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

iddatelicense platekms
112-07-2022ABC123100
212-10-2022ABC12370
312-13-2022ABC12340
412-08-2022DEF45650
512-12-2022DEF45630
612-19-2022DEF45670

 

FUEL CONSUMPTION TABLE

iddatelicense plateamount
112-01-2022ABC123120
212-10-2022ABC12365
312-13-2022DEF45660
412-17-2022DEF45635

 

COMBINED RESULT

datelicense platefuel consumptiontraveled distance
10-12-2022ABC123185210
17-12-2022DEF45695150

 

DESIRED RESULT

datelicense platefuel consumptiontraveled distance
10-12-2022ABC123185170
17-12-2022DEF4569580

 

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

  • Anonymous's avatar
    Anonymous
    3 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

  • Anonymous's avatar
    Anonymous
    Not 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

  • Anonymous's avatar
    Anonymous
    Not 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?