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
Gianluigi
Helper II
Helper II

calculate the number of hours between the rows

Hello,

Hello
How can I calculate the number of hours between the rows in the following table?

Example between the second line and the first should be about 48 hours.

Thank you 

 

Gianluigi_0-1708418398479.png

 

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @Gianluigi ,

You can create a calculated column as below to get it, please find the details in the attachment.

Column = 
VAR _predatetime =
    CALCULATE (
        MAX ( 'Table'[dataEvento] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[dataEvento] < EARLIER ( 'Table'[dataEvento] )
        )
    )
RETURN
    DATEDIFF ( _predatetime, 'Table'[dataEvento], HOUR )

vyiruanmsft_0-1708502317276.png

In addition, you can also create a measure as below to get it.

Number of hours between the rows = 
VAR _datetime =
    SELECTEDVALUE ( 'Table'[dataEvento] )
VAR _predatetime =
    CALCULATE (
        MAX ( 'Table'[dataEvento] ),
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[dataEvento] < _datetime )
    )
RETURN
    DATEDIFF ( _predatetime, _datetime, HOUR )

Best Regards

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

View solution in original post

8 REPLIES 8
v-yiruan-msft
Community Support
Community Support

Hi @Gianluigi ,

You can create a calculated column as below to get it, please find the details in the attachment.

Column = 
VAR _predatetime =
    CALCULATE (
        MAX ( 'Table'[dataEvento] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[dataEvento] < EARLIER ( 'Table'[dataEvento] )
        )
    )
RETURN
    DATEDIFF ( _predatetime, 'Table'[dataEvento], HOUR )

vyiruanmsft_0-1708502317276.png

In addition, you can also create a measure as below to get it.

Number of hours between the rows = 
VAR _datetime =
    SELECTEDVALUE ( 'Table'[dataEvento] )
VAR _predatetime =
    CALCULATE (
        MAX ( 'Table'[dataEvento] ),
        FILTER ( ALLSELECTED ( 'Table' ), 'Table'[dataEvento] < _datetime )
    )
RETURN
    DATEDIFF ( _predatetime, _datetime, HOUR )

Best Regards

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

And then this mistake too

Error Message:
The template does not include a table named Column in the eventi table

Hello
FYI, the new column doesn't work. Just save after a few minutes power bi reports this error

Error Message:
There is not enough memory to complete this operation. Please try again later when there may be more memory available.

Great!

 Thank you very much!

Jonvoge
Solution Supplier
Solution Supplier

Hi Gianluigi.

 

If you update to the latest version of Power BI, you can try out the brand new Visual Calculation feature to calculate row-by-row: Exploring Power BI Visual Calculations – Data – Marc (data-marc.com).

 

If you use the DATEDIFF() DAX function together with a Visual Calculation, you should be able to achieve what you want.
_____________________________________________________
I hope my comment was helpful.
If your question was answered, please mark your post as 'Solved' and consider giving me a 'Thumbs Up'.

Thank you !

Arul
Super User
Super User

@Gianluigi ,

In which visual, Do you want this?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


LinkedIn


In the table.

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.