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
verowin
Frequent Visitor

Calculate difference in running hours for an asset over a period of time

need a way to calculate the difference between two running hours values over a period of time

The period of time will be controlled with a slicer (which is no problem)

here is the data

Capture.PNG

1 ACCEPTED SOLUTION


@verowin wrote:

I want a table that calculates the difference in running hours for a period of time for a list of counter-keys

The period of time is set dynamicly with a slicer

does that make more sense?


Yes. that helps. See if the following works for you:

 

Hours Difference Between Dates = 
VAR TheFirstAmount =
    CALCULATE(
        MIN(Metrics[Running Hours]),
        FILTER(
            'Date',
            MIN(Metrics[Reading Date]) >= FIRSTDATE(ALLSELECTED('Date'[Date]))
        )
    )
VAR TheLastAmount =
    CALCULATE(
        MAX(Metrics[Running Hours]),
        FILTER(
            'Date',
            MAX(Metrics[Reading Date]) <= LASTDATE(ALLSELECTED('Date'[Date]))
        )
    )
RETURN
TheLastAmount - TheFirstAmount

 

You can see the PBIX file I worked with here. As far as I can tell, when I move the slicer it pics the dates between the dates in the slicer and calculates the difference. I added a date table since you are working with dates, which you can also see in the PBIX file.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

7 REPLIES 7
edhans
Super User
Super User

I don't see any hours. Just days. Can you clairify, and post data that we can copy into Power BI Desktop to play with vs an image that would require us to retype a lot of data?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Counter_keyRunning HoursReading Date
01STSL_CNT48413.31Monday, June 10, 2019
01RTGL_CNT1231Monday, June 10, 2019
01RTGL_CNT1753.7Saturday, August 24, 2019
01STSL_CNT49309.12Thursday, August 29, 2019
01RTGL_CNT1852Thursday, August 29, 2019
01RTGL_CNT1852Thursday, August 29, 2019
01RTGL_CNT1852Sunday, September 15, 2019
01STSL_CNT49521.02Wednesday, September 25, 2019
01RTGL_CNT2214Wednesday, September 25, 2019
01STSL_CNT49953.39Tuesday, October 29, 2019
01RTGL_CNT2482Tuesday, October 29, 2019
01RTGL_CNT2785Monday, December 2, 2019
01RTGL_CNT2786Saturday, December 7, 2019
01STSL_CNT50454.85Tuesday, December 10, 2019
01STSL_CNT50784Thursday, January 2, 2020
01RTGL_CNT2992Thursday, January 2, 2020

need a way to calculate the difference between two running hours values over a period of time for a counter key.

I have a file with 100+ counter keys running hours over 2 years time.

Ok, I see the hours. I still am not clear on what you want. 

 


@verowin wrote:

need a way to calculate the difference between two running hours values over a period of time for a counter key.

I have a file with 100+ counter keys running hours over 2 years time.


Let's take the 01RTGL_CNT key.

 

Counter_keyRunning HoursReading Date

01RTGL_CNT12316/10/2019
01RTGL_CNT1753.78/24/2019
01RTGL_CNT18528/29/2019
01RTGL_CNT18528/29/2019
01RTGL_CNT18529/15/2019
01RTGL_CNT22149/25/2019
01RTGL_CNT248210/29/2019
01RTGL_CNT278512/2/2019
01RTGL_CNT278612/7/2019
01RTGL_CNT29921/2/2020

 

What is your expected result? What are the "two running hours values" you are referring to?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

I want a table that calculates the difference in running hours for a period of time for a list of counter-keys

The period of time is set dynamicly with a slicer

does that make more sense?


@verowin wrote:

I want a table that calculates the difference in running hours for a period of time for a list of counter-keys

The period of time is set dynamicly with a slicer

does that make more sense?


Yes. that helps. See if the following works for you:

 

Hours Difference Between Dates = 
VAR TheFirstAmount =
    CALCULATE(
        MIN(Metrics[Running Hours]),
        FILTER(
            'Date',
            MIN(Metrics[Reading Date]) >= FIRSTDATE(ALLSELECTED('Date'[Date]))
        )
    )
VAR TheLastAmount =
    CALCULATE(
        MAX(Metrics[Running Hours]),
        FILTER(
            'Date',
            MAX(Metrics[Reading Date]) <= LASTDATE(ALLSELECTED('Date'[Date]))
        )
    )
RETURN
TheLastAmount - TheFirstAmount

 

You can see the PBIX file I worked with here. As far as I can tell, when I move the slicer it pics the dates between the dates in the slicer and calculates the difference. I added a date table since you are working with dates, which you can also see in the PBIX file.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Please find the formula. For each Asset , taken  Min and Max date. Found there reading. And taken Diff. Not tested. Please, do necessary changes on formula pad

 

Total value = Calculate(
    sumx(  
        ADDCOLUMNS( 
            SUMMARIZE(
                data,
                data[Counter_key],
				"max_date",
				 MAX( 'Date'[Reading Date]),
				 "Min_date",
				 Min( 'Date'[Reading Date]),
                "Max_ID", max(data[Counter_key])
            ),
            "MaXValue",
            calculate(
                Max(data[Running Hours]),
                filter(
                    all(data),
                    data[Counter_key]= [Max_ID] && data[Reading Date]= ([max_date]) )
                ),
			"MinValue",
            calculate(
                Max(data[Running Hours]),
                filter(
                    all(data),
                    data[Counter_key]= [Max_ID] && data[Reading Date]= ([min_date]) )
                )
            )
        ,[MaXValue] -[MinValue]
    )
)

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
https://community.powerbi.com/t5/Community-Blog/Winner-Topper-on-Map-How-to-Color-States-on-a-Map-wi...
https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...
https://community.powerbi.com/t5/Community-Blog/Power-BI-Working-with-Non-Standard-Time-Periods/ba-p...
https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601

 

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.