Forum Discussion
adding row
- 1 year ago
Hi wsindharta,
Thank you for reaching out to the Microsoft fabric community forum. Thank you SundarRaj, ZhangKun, BeaBF, for your inputs on this issue.
After thoroughly reviewing the details you provided, I was able to reproduce the scenario, and it worked on my end. I have used it as sample data on my end and successfully implemented it.
M Query:let // Sample source table Source = Table.FromRows({ {"A123789", "06181819", "0120", "23/01/2022", #datetime(2025, 5, 2, 20, 25, 0), #datetime(2025, 5, 5, 8, 45, 35)} }, {"WorkCenter", "WorkOrder", "Operation", "ProductName", "StartTime", "EndTime"}), // Add duration in seconds AddDuration = Table.AddColumn(Source, "DurationSec", each Duration.TotalSeconds([EndTime] - [StartTime]), type number), // Generate list of dates for each row AddDateList = Table.AddColumn(AddDuration, "DateList", each List.Dates( Date.From([StartTime]), Duration.Days([EndTime] - [StartTime]) + 1, #duration(1, 0, 0, 0) )), // Expand date list into rows ExpandedDates = Table.ExpandListColumn(AddDateList, "DateList"), // Add start and end time for each day AddDayTimes = Table.AddColumn(ExpandedDates, "Start_End", each let currentDate = [DateList], startTime = if Date.From([StartTime]) = currentDate then [StartTime] else #datetime(Date.Year(currentDate), Date.Month(currentDate), Date.Day(currentDate), 0, 0, 0), endTime = if Date.From([EndTime]) = currentDate then [EndTime] else #datetime(Date.Year(currentDate), Date.Month(currentDate), Date.Day(currentDate), 23, 59, 59) in [StartTime = startTime, EndTime = endTime] ), // Expand start/end time record RemoveOriginalTimes = Table.RemoveColumns(AddDayTimes, {"StartTime", "EndTime"}), ExpandedTimes = Table.ExpandRecordColumn(RemoveOriginalTimes, "Start_End", {"StartTime", "EndTime"}), // Recalculate duration RecalcDuration = Table.AddColumn(ExpandedTimes, "Duration", each Duration.TotalSeconds([EndTime] - [StartTime]), type number), // Add date column Final = Table.RenameColumns(Table.SelectColumns(RecalcDuration, {"WorkCenter", "WorkOrder", "Operation", "ProductName", "StartTime", "EndTime", "Duration", "DateList"}), {"DateList", "Date"}) in Final
Output: Go to the table view see this output:
If this post helps, then please give us ‘Kudos’ and consider Accept it as a solution to help the other members find it more quickly.
Thank you for using Microsoft Community Forum.
You can download the attachment and see if it solves your problem.
hi ZhangKun
this is great and work fine ! however when i try to paste the code into my original query. i got error
"Expression.Error: Evaluation resulted in a stack overflow and cannot continue."
please help
below is the original query :
let
Source = Sql.Database("MLXSNGXVWPOPDB99", "STDS", [Query=****),
#"Removed Columns" = Table.RemoveColumns(Source,{"Department", "Operation", "Tool", "Location", "Area"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "Downtime Duration", each if [batchid]=null then [Duration] else [Duration]/2),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Downtime Duration", type number}}),
#"Removed Columns1" = Table.RemoveColumns(#"Changed Type",{"Duration"}),
#"Changed Type11" = Table.TransformColumnTypes(#"Removed Columns1",{{"StartTime", type number}, {"EndTime", type number}}),
#"Added Custom5" = Table.AddColumn(#"Changed Type11", "Custom", each if [EndTime]= null then [StartTime]+[Downtime Duration]/(60*24) else [EndTime]),
#"Changed Type12" = Table.TransformColumnTypes(#"Added Custom5",{{"Custom", type datetime}, {"StartTime", type datetime}, {"EndTime", type datetime}}),
#"Removed Columns7" = Table.RemoveColumns(#"Changed Type12",{ "EndTime"}),
#"Renamed Columns4" = Table.RenameColumns(#"Removed Columns7",{{"Custom", "EndTime"}}),
fxDatesToList = (r) =>
let
// simplify variable references
s = r[StartTime],
e = r[EndTime],
sd = Date.From(s),
ed = Date.From(e)
in
List.Transform(
List.Dates(sd, Duration.Days(ed - sd) + 1, #duration(1, 0, 0, 0)),
each r & [
StartTime = if _ = sd then s else _ & #time(0, 0, 0),
EndTime = if _ = ed then e else _ & #time(23, 59, 59),
// due to the date system problem, the full day should be specified as 1440 minutes.
Duration = if _ = sd or _ = ed then Duration.TotalMinutes(EndTime - StartTime) else 1440
]
),
result = Table.FromRecords(
List.Accumulate(
Table.ToRecords(#"Renamed Columns4"),
{},
(s, v) =>
if Date.Day(v[StartTime]) <> Date.Day(v[EndTime]) then s & fxDatesToList(v) else s & {v}
),
// Convert records to table and restore column types
Value.Type(#"Renamed Columns4")
)
in
result