Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
mwild
Frequent Visitor

Calculated Table

Hi,

I have a table in Excel (to be imported into Power Bi) which has dates down one side (running from January 2010 to way into the future)
My columns are vehicles A, B and C (but over 100 of them) and the date within the spreadsheet is cumulative miles

mwild_0-1641398235262.png


I'm looking at creating a new table which will show me the daily miles instead so A would show 232 which is the difference between 15th and 16th

Does anyone know of a way to do this please?



2 ACCEPTED SOLUTIONS
PaulDBrown
Community Champion
Community Champion

Firstly I would recommend you Unpivot the Vehicle columns. 

Unpivot.jpg

 

You can then create a calculated column to get the daily mileage for each vehicle using:

 

Daily Mileage =
VAR PrevDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date]
                    = EARLIER ( 'DataTable'[Date] ) - 1
        )
    )
VAR CurrDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date] = EARLIER ( 'DataTable'[Date] )
        )
    )
RETURN
    CurrDate - PrevDate

 

 

To get:

Daily Mielage.jpg

If you want the "Daily Mileage" for the first (minimum) date to be blank, use:

Daily Mileage =
VAR MinDate =
    MIN ( 'DataTable'[Date] )
VAR PrevDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date]
                    = EARLIER ( 'DataTable'[Date] ) - 1
        )
    )
VAR CurrDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date] = EARLIER ( 'DataTable'[Date] )
        )
    )
RETURN
    IF ( 'DataTable'[Date] = MinDate, BLANK (), CurrDate - PrevDate )

I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

does exactly as I needed - thanks for this 👍

 

View solution in original post

3 REPLIES 3
PaulDBrown
Community Champion
Community Champion

Firstly I would recommend you Unpivot the Vehicle columns. 

Unpivot.jpg

 

You can then create a calculated column to get the daily mileage for each vehicle using:

 

Daily Mileage =
VAR PrevDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date]
                    = EARLIER ( 'DataTable'[Date] ) - 1
        )
    )
VAR CurrDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date] = EARLIER ( 'DataTable'[Date] )
        )
    )
RETURN
    CurrDate - PrevDate

 

 

To get:

Daily Mielage.jpg

If you want the "Daily Mileage" for the first (minimum) date to be blank, use:

Daily Mileage =
VAR MinDate =
    MIN ( 'DataTable'[Date] )
VAR PrevDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date]
                    = EARLIER ( 'DataTable'[Date] ) - 1
        )
    )
VAR CurrDate =
    CALCULATE (
        SUM ( 'DataTable'[Mileage] ),
        FILTER (
            'DataTable',
            'DataTable'[Vehicle] = EARLIER ( 'DataTable'[Vehicle] )
                && 'DataTable'[Date] = EARLIER ( 'DataTable'[Date] )
        )
    )
RETURN
    IF ( 'DataTable'[Date] = MinDate, BLANK (), CurrDate - PrevDate )

I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






does exactly as I needed - thanks for this 👍

 

mh2587
Super User
Super User

please provide sample data and expected result for the issue


✔ Answered? Mark as solution

Muhammad Hasnain | Super User • Fabric • Power BI • Data Engineering

Let's connect on LinkedIn

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.