Forum Discussion

fyk_cz's avatar
fyk_cz
Regular Visitor
9 years ago
Solved

Calculate days between dates in rows

Hi i have a this data: Date Odo (km) Fuel (litres) 05.05.2017 34428 37 28.04.2017 33855 37 19.04.2017 33276 39,9 16.04.2017 32636 19 12.04.2017 32330 39,21 04.04.20...
  • Anonymous's avatar
    Anonymous
    9 years ago

    Hi,

    If you wish to create two new columns for days from previous row and traveled distance you can calculate them like this:

     

    First the column for days from previous date=

    var curdate='Table1'[Date]
        return
            CALCULATE(
                DATEDIFF(
                    MAX('Table1'[Date]);
                    curdate;
                    DAY
                );
                FILTER('Table1';
                    'Table1'[Date]<curdate
                )
            )

     

    Then you create next column for Distance from previous date=

    var curdist=[ODO]
        return
            IF(
                not ISBLANK([Days from previous]);
                CALCULATE(
                    curdist-MAX('Table1'[ODO]);
                    FILTER(
                        'Table1';
                        Table1[ODO]<curdist
                    )
                )
            )

     

    Make sure you substitute the table and column names in the code to make this work in your specific case.

     

    Br,

    Magnus