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
mwenski
Regular Visitor

Duty Cycle from Date table

This is a PowerBI / Power Query "how to approach" question. I'm really interested in learning what terms I should be throwing into google to find this answer and want to thank those that read this in advance.  If I have a table of dates showing that a light on a room was turned on and off, what's the best way to approach this from a data modeling perspective using Power Query or DAX functions in Power BI?

 

I have solved this problem in the past using different tools (Tableau and Excel), but I wanted to ask this community to see if I'm following best practices and I'm not trying to shove a round peg in a square hole.

 

I'm trying to represent a summary of the attached table filtered to a single ID (column A) that has 36 rows (entry order indicated by "CHGNR" or Change Number). It is showing dates a particular status was set ("INACT" is Blank) and reset ("INACT" has an X value, indicating the status has been set to inactive). The UDATE column is an 8 digit integer representing the date in YYYYMMDD format, and I made an excel formula to parse that date if it'll help give an answer.  I'm unclear on how to attach an excel file, I hope the below table format is ok.

 

Lower Date bound should be 1/1/2011 and the desired upper bound would be the date of the extract, or 4/7/2020.  For background, there are ~20k unique objects in the table, ~77k total entries that meet this criteria, and there exists a second fact table with 1 row per object that contains attributes of interest.

 

ObjectCHGNRINACTUDATEFormatted Date
?01000000000000808981 201009139/13/2010
?01000000000000808982X2010100710/7/2010
?01000000000000808983 201105045/4/2011
?01000000000000808984X201105195/19/2011
?01000000000000808985 201203053/5/2012
?01000000000000808986X201203303/30/2012
?01000000000000808987 201205045/4/2012
?01000000000000808988X201206016/1/2012
?01000000000000808989 201301031/3/2013
?010000000000008089810X201301301/30/2013
?010000000000008089811 201309049/4/2013
?010000000000008089812X2013100110/1/2013
?010000000000008089813 2014110311/3/2014
?010000000000008089814X2014112411/24/2014
?010000000000008089815 201501091/9/2015
?010000000000008089816X201502032/3/2015
?010000000000008089817 201503043/4/2015
?010000000000008089818X201504024/2/2015
?010000000000008089819 201507027/2/2015
?010000000000008089820X201601061/6/2016
?010000000000008089821 201605115/11/2016
?010000000000008089822X201606076/7/2016
?010000000000008089823 201701051/5/2017
?010000000000008089824X201701121/12/2017
?010000000000008089825 201703023/2/2017
?010000000000008089826X201703273/27/2017
?010000000000008089827 201801101/10/2018
?010000000000008089828X201801311/31/2018
?010000000000008089829 201803053/5/2018
?010000000000008089830X201804024/2/2018
?010000000000008089831 201805025/2/2018
?010000000000008089832X201805245/24/2018
?010000000000008089833 201809059/5/2018
?010000000000008089834X2018100210/2/2018
?010000000000008089835 20181102

11/2/2018

?010000000000008089836X2018123112/31/2018

 

I'll include the excel reference here in case it's relevant:

Excel: Prep this table by sorting by Object and Change number (smallest number for each functional location first)

Calculate the days in each state by subtracting date of current row with prior row (if same Object). If Status has "X" in "Indicator: Status is Inactive" it represents the date the prior status ended, and the date difference is how long the Object had "Lights out" so to speak. If the last record for the Object has no X (eg, the status was Set to "Lights out") then the presumed end date of that state would be the date of the data extraction.

 

Another table summarized each functional location with a traditional SUMIF statement, using the number mentioned above as what's being added and the condition of Object row value = Object Column. (There was another formula to see if the status was active at the end of the state and add in those days using the extract date.)

 

 

Tableau:

Wrote a formula that basically asked a user for a date (or used Today() if they didn't provide any) as an day to look from (by default it would be today's date, but they could look at the start of the year if desired).

 

Calculated Date Diff between the date in the table and the user entered date on a per row basis.

Made an integer that is -1 when the Indicator: Status is Inactive = "X" and positive 1 when there is not an X.

Multiplied the prior two values together and summed them per functional location to calculate the total days spent with the status set (EG, total days the "Lights Out").

 

 

I did some further refinements to ensure that if a user wanted to focus on a particular range of dates that the calculation only counted dates within that data range, and I don't want to distract from my main question here with those.

3 REPLIES 3
Greg_Deckler
Super User
Super User

If I understand this correctly, and I am not saying this is the "best" way, but one way is: See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks Greg! That's really close to a perfect example.  Right now I don't have Down and Up on the same row, but split across two rows.  I'll poke around at the PowerQuery tutorials on how to manipulate the data to the point that I have 1 row per down/up cycle.

 

I imagine I could use the odd change number data as "Down" and even as "Up" and perhaps filter even change numbers out, add one to the odd values, join on Object and the modified change number as key. 

 

I book marked your link for future reference after giving you kudos. I'm interested in seeing if there are any other approaches and then I'll march forth. 

dax
Community Support
Community Support

Hi @mwenski , 

I am not clear about your requirement, if possible could you please inform me more detailed information(such as your expected output and your sample data (by OneDrive for Business))? Then I will help you more correctly.

Please do mask sensitive data before uploading.

Thanks for your understanding and support.
Best Regards,
Zoe Zhi

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

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.