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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Art
Frequent Visitor

How to calculate a new column to plot the number of hours elapsed Vs Fuel consumed

Capture1.PNG

 

I have table with the above two columns. I want to calculate the time elapsed in terms of number of hours between two time stamps and also the diffrence in fuel consumed during those times and store them as two new columns and then plot a line chart of hours Vs Fuel consumed

4 REPLIES 4
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @Art

 

You could refer to the two calculated column below.

 

Before creating the calculated colum, you need to create the index column in Query Editor.

 

Then you could create the calculated column below.

 

difference for time =
VAR prevous =
    CALCULATE (
        MIN ( 'Table1'[FuelUseddatetime] ),
        FILTER ( 'Table1', 'Table1'[Index] > EARLIER ( 'Table1'[Index] ) )
    )
RETURN
    IF (
        ISBLANK ( prevous ),
        'Table1'[FuelUseddatetime],
        DATEDIFF ( [FuelUseddatetime], [Previous], HOUR )
    )
difference for furlused =
VAR NextIndex = Table1[Index] + 1
RETURN
    - (
        Table1[FuelUsed]
            - CALCULATE (
                VALUES ( Table1[FuelUsed] ),
                FILTER ( ALL ( Table1 ), Table1[Index] = NextIndex )
            )
    )

Then you could get the differences.

 

Capture.PNG

 

If you need additional help, please share your desired output, so that we can help further investigate on it?

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Can the last row with negative value be eliminated?

HI! cherry,

Thanks for the reply. The difference in time DAX is not working. You have used the spelling prevous in two places. in the DATEDIFF function you have used Previous. Shouldn't all three have the same spelling? I tried all three with the same spelling but I get an error saying the prevous in the DATEDIFF function cannot be found or may not be used in this expression. Can you please copy paste this Block of coded from your power BI expression window. I wonder why its giving error even if I make them all the same spelling.

Art
Frequent Visitor

should the [Previous] in the DATEDIFF function be just prevous?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.