Пользователь
Сообщений: 2894
Регистрация: 13.06.2014
Анастасия , скорее всего, Вам это не совсем нужно и спокойно можно вычислить план на дату в DAX
. Но раз хотите:
Вариант с generate Скрытый текст
Код let
data = Excel.CurrentWorkbook(){[ Name = "data" ]}[Content],
typed = Table.TransformColumnTypes ( data, { { "date", type date }, { "plan_day", type number } } ),
tomorrow = Date.AddDays ( Date.From ( DateTime.LocalNow() ), 1 ),
group = Table.Group (
typed,
{ "DepartmentID", "post_id" },
{
{
"tbl",
( t ) =>
[
sort = Table.Sort ( t, { { "date", Order.Ascending } } ),
recs = List.Buffer ( Table.ToRecords ( sort ) ),
iMax = List.Count ( recs ) - 1,
generate = List.Generate (
() => [
i = 0,
result = Record.TransformFields (
recs{i},
{
{
"date",
( x ) =>
List.Dates ( x, Duration.TotalDays ( ( if i = iMax then tomorrow else recs{i + 1}[date] ) - x ), #duration ( 1, 0, 0, 0 ) )
}
}
)
],
each [i] <= iMax,
each [
i = [i] + 1,
result = Record.TransformFields (
recs{i},
{
{
"date",
( x ) =>
List.Dates ( x, Duration.TotalDays ( ( if i = iMax then tomorrow else recs{i + 1}[date] ) - x ), #duration ( 1, 0, 0, 0 ) )
}
}
)
],
each [result]
),
toTable = Table.FromRecords ( generate ),
expand = Table.ExpandListColumn ( toTable, "date" ),
type = Table.TransformColumnTypes ( expand, { { "date", type date } } )
][type],
type table
}
}
),
combine = Table.Combine ( group[tbl] )
in
combine
Вариант с join+fillDown Скрытый текст
Код let
data = Excel.CurrentWorkbook(){[ Name = "data" ]}[Content],
typed = Table.TransformColumnTypes ( data, { { "date", type date }, { "plan_day", type number } } ),
rename = Table.RenameColumns ( typed, { { "date", "planDate" } } ),
today = Date.From ( DateTime.LocalNow() ),
group = Table.Group (
rename,
{ "DepartmentID", "post_id" },
{
{
"tbl",
( t ) =>
[
clmnNames = List.RemoveItems ( Table.ColumnNames ( t ), { "planDate" } ),
sDate = List.Min ( t[planDate] ),
datesList = List.Dates ( sDate, Duration.TotalDays ( today - sDate ) + 1, #duration ( 1, 0, 0, 0 ) ),
datesTable = Table.FromColumns ( { datesList }, type table [ date = date ] ),
join = Table.NestedJoin ( datesTable, { "date" }, t, { "planDate" }, "data", JoinKind.LeftOuter ),
expand = Table.ExpandTableColumn ( join, "data", clmnNames ),
fillDown = Table.FillDown ( expand, clmnNames )
][fillDown],
type table
}
}
),
combine = Table.Combine ( group[tbl] )
in
combine
Generate будет чуть шустрее.
Изменено: surkenny - 25.05.2022 18:00:24