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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
giovissimo
Helper I
Helper I

Problem Calculating the Kilometers driven

Hello,

first of all, sorry if my problem has been solved already; I found a similar question, but not exactly the same.

My table contains the following fields: Date (which is not relevant), Driver, Car, TotalKms, DrivenKms.

We own two cars in my family, and each of us can drive either car everyday. In each row I want to write down the total kilometers for each car, and have a column calculate the kilometers driven (DrivenKms) after each drive, but taking into account the car. 

How can I solve it ?

Thank you in advance.

 

Giovanni

22 REPLIES 22
v-yalanwu-msft
Community Support
Community Support

Hi, @giovissimo ,

 

In your data, In order to distinguish the same kilometers from the same date, you may have to use the index column as an auxiliary column. and then create a column as follows:

DrivenKms =
VAR _last =
    CALCULATE (
        MAX ( [TotalKms] ),
        FILTER ( ALLEXCEPT ( 'Table1', 'Table1'[Car] ), [Index] < EARLIER ( [Index] ) )
    )
VAR _a = 'Table1'[Index]
RETURN
    IF (
        CALCULATE (
            SUM ( [TotalKms] ),
            FILTER ( 'Table1', 'Table1'[Index] = _a - 1 || 'Table1'[Index] = _a )
        ) / 2 = [TotalKms],
        0,
        [TotalKms] - _last
    )

The final output is shown below:

v-yalanwu-msft_0-1622016417982.png

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for replying in the first place.

Unfortunately, the error message is the same as the one prieviously mentioned (i.e., that the row context EARLIER refers to actually doesn't exist).

 

Translated, it says that EARLIER refers to a row context which doesn't existTranslated, it says that EARLIER refers to a row context which doesn't exist

Hi , @giovissimo ,

 

so you have to add index column, steps as follows:

v-yalanwu-msft_0-1622018735408.pngv-yalanwu-msft_1-1622018784652.png

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I've bypassed the problem by importing the table in Access, inserting the Index (counter) column and then exporting it again in Excel, from where I imported it in PowerBI 🙂

Nonetheless, the error message remains 😞 

Thank you, nonetheless apparently I have a different menu, and when I try to add a new column, I don't get to specifiy the type of data, since as I click on to the "Tipo di Dati" menu on the left, the column disappears.

My version is the 2.92.1067.0 64-bit (April 2021). 

 

NewColumn.JPG

Ashish_Mathur
Super User
Super User

Hi,

Share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

What end result do you want to see?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

The one you see in the "DrivenKms" column:

 

https://1drv.ms/x/s!AiJ_Ge6bLlC6-U-ti0wqDRuebJ6q?e=tDwltm

 

Giovanni

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you, only I don't have any index column in my table: is it absolutely necessary to add it ?

Is it linked to any existing column ?

 

Giovanni

You are welcome.  The Index column was created in the Query Editor.  See the steps in the Query Editor. 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you again for devoting your time to the solution of my problem.

I've just taken a look at all the steps in the query editor, but unfortunately I can't understand the meaning of that Index column (it's obviously my fault). As far as I can see, it's not linked to the driver, neither to the car.

Isn't it possible to solve the problem without resorting to that Index column ? Just asking.

 

Giovanni

You are welcome.  The only reason we need the Index column is to get the 0's that you have shown in the intiial post.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

I see, but it's not an automatic counter, right ? So, when I have to add another row, what value am I supposed to write for the Index column ?

 

Giovanni

It is automatic.  You just need to add data to your raw data sheet and click on Home > Refresh All.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yalanwu-msft
Community Support
Community Support

Hi @giovissimo  , 

 

You could create a measure by the following formula:

DrivenKms =
VAR _next =
    CALCULATE (
        MAX ( [TotalKms] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Car] ), [Date ] < EARLIER ( [Date ] ) ))
RETURN
    IF ( [TotalKms] <> [TotalKms] - _next, [TotalKms] - _next )

The final output is shown below:

v-yalanwu-msft_0-1620625934323.png

Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.  

Thank you for replying, nonetheless the error shown is the following (translated from Italian):

"EARLIER/EARLIEST refers to a previous row context which doesn't exist".

I'm at a loss to understand the reason of such a message.

 

Giovanni

Hi, @giovissimo 

 

EARLER() is used for column, not measure. If you use measure, you can use Max ().as follows:

DrivenKms2 =
VAR _next =
    CALCULATE (
        MAX ( [TotalKms] ),
        FILTER ( ALLEXCEPT ( 'Table', 'Table'[Car] ), [Date ] < MAX ( [Date ] ) )
    )
RETURN
    IF (
        MAX ( [TotalKms] )
            <> MAX ( [TotalKms] ) - _next,
        MAX ( [TotalKms] ) - _next
    )

I hope it can help you.  can you share some error images or share me with your PBIX file after removing sensitive data.
Best Regards,
Community Support Team_ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks for replying.

This works just fine, except in 2 cases:

1) for each car, the initial value of the calculated column cannot be blank, but it must be set equal to the total km;

2) when the driver simply refuels, without actually increasing the number of total kms, the calculated column must return 0.

Here's the link to the data:

https://1drv.ms/x/s!AiJ_Ge6bLlC6-U-ti0wqDRuebJ6q?e=4x4off

 

Giovanni

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.