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
Anonymous
Not applicable

Group measure into date ranges

I have one table with dates of a part repair and another table with operating times. Is there any way I can create a measure to find the total of operating time between repairs? I'd need a way to create a new column to show what part number we're currently on, so part 1 will be before the first repair, part 2 will be between the first and second repair, and so on and so forth. I could make a giant messy if statement except I need a way to assign it dynamically between any number of rows. Any ideas of where to start with this problem would be greatly appreciated. 

 

EDIT: attached the 2 data sets. The first shows hours of operation, the second has the dates where parts were changed.

 

Repairs.png

installs.png

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi BolaSquirrel,

You could try to use M code to see whether it work or not

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY1BDsAgCAT/wtkEAS3pW4z//0YTW6jIdbKzMwY0KMCVbiQkhlkC4ZfwRvoiZISRMtFoOfFnQYoTSTdicZfaJ/mkmyQ/4Ro31ynpmdaUVkvLRgTmfAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [rig = _t, date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"rig", Int64.Type}, {"date", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"rig"}, {{"all", each _, type table [rig=number, date=date, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"newc",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"date", "newc"}, {"date", "newc"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"all"}),
    #"Replaced Value" = Table.ReplaceValue(#"Removed Columns",each [newc],each if [rig]=1 then [newc] else null,Replacer.ReplaceValue,{"newc"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"date", Order.Ascending},{"rig", Order.Ascending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"newc"}),
    #"Filled Up" = Table.FillUp(#"Filled Down",{"newc"})
in
    #"Filled Up"

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.

View solution in original post

6 REPLIES 6
dax
Community Support
Community Support

Hi BolaSquirrel,

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)? 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.

Anonymous
Not applicable

I've editted my initial post with the data. The first data set is a collection of working times. The second data set is when parts were changed out. So for rig 1 the repair dates were 2/15, 2/17, 3/23, 5/1, 5/4, 5/5, and 6/4.  I'd like to add a new column to the first table that lists a part number. For anything before 2/15 it would be part 1, for 2/17-3/23 it would be part 2, for 3/23-5/1 it would be part 3, and so on and so forth. I'm not sure what the best way to tackle this is since there's a variable number of days between new parts. And I don't know how many rows I'll have for any particular rig.

dax
Community Support
Community Support

Hi BolaSquirrel,

Did this help you solve your issue? If so and if you'd like to, you could mark corresponding post as answer or share your solutions. That way, people who in this forum and have similar issue will benefit from it.

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.

dax
Community Support
Community Support

Hi BolaSquirrel,

You could try to use M code to see whether it work or not

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY1BDsAgCAT/wtkEAS3pW4z//0YTW6jIdbKzMwY0KMCVbiQkhlkC4ZfwRvoiZISRMtFoOfFnQYoTSTdicZfaJ/mkmyQ/4Ro31ynpmdaUVkvLRgTmfAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [rig = _t, date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"rig", Int64.Type}, {"date", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Grouped Rows" = Table.Group(#"Added Index", {"rig"}, {{"all", each _, type table [rig=number, date=date, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([all],"newc",1,1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"date", "newc"}, {"date", "newc"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"all"}),
    #"Replaced Value" = Table.ReplaceValue(#"Removed Columns",each [newc],each if [rig]=1 then [newc] else null,Replacer.ReplaceValue,{"newc"}),
    #"Sorted Rows" = Table.Sort(#"Replaced Value",{{"date", Order.Ascending},{"rig", Order.Ascending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"newc"}),
    #"Filled Up" = Table.FillUp(#"Filled Down",{"newc"})
in
    #"Filled Up"

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.

parry2k
Super User
Super User

@Anonymous Please share how your data looks like, share sample data and I'm sure we will able to help you. Read this post to get your answer quickly.

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thank you for the suggestion, I've editted my initial post and attached screenshots.

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.