retgeav
Me gustaría completar los valores de datos faltantes en series temporales mediante interpolación lineal
Mis datos
fecha | valor de los datos |
02-01-2019 | 127 |
06-01-2019 | 156 |
13-01-2019 | 178 |
16-01-2019 | 161 |
Resultado que me gustaría tener
fecha | valor de los datos |
02-01-2019 | 127 |
03-01-2019 | 134 |
01-04-2019 | 142 |
05-01-2019 | 149 |
06-01-2019 | 156 |
07-01-2019 | 159 |
08-01-2019 | 162 |
09-01-2019 | 165 |
10-01-2019 | 169 |
11-01-2019 | 172 |
12-01-2019 | 175 |
13-01-2019 | 178 |
14-01-2019 | 172 |
15-01-2019 | 167 |
16-01-2019 | 161 |
Jimmy801
Hola @retgeav
esto requiere bastantes pasos y seguro que hay más posibilidades. Aquí el código M
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}, "en-DE"),
ListMissingDates = Table.RenameColumns(Table.FromList(List.Difference(List.Dates(List.Min(#"Changed Type"[date]),Duration.TotalDays(List.Max(#"Changed Type"[date])-List.Min(#"Changed Type"[date])), #duration(1,0,0,0) ), #"Changed Type"[date]), Splitter.SplitByNothing(),null, null, ExtraValues.Error), {{"Column1", "date"}}),
Combine = Table.Combine({#"Changed Type", ListMissingDates}),
#"Sorted Rows" = Table.Sort(Combine,{{"date", Order.Ascending}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "data value", "GroupColumn"),
#"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "data value", "MaxValue"),
#"Filled Down" = Table.FillDown(#"Duplicated Column1",{"GroupColumn"}),
#"Filled Up" = Table.FillUp(#"Filled Down",{"MaxValue"}),
#"Grouped Rows" = Table.Group(#"Filled Up", {"GroupColumn"}, {{"AllRows", each _, type table [date=date, data value=number, GroupColumn=number, MaxValue=number]}}),
TransformGroupedTable = Table.TransformColumns
(
#"Grouped Rows",
{
{
"AllRows",
(tbl)=>
let
Index= Table.AddIndexColumn(tbl,"Index",0,1),
CalculateIncrement = (tbl[MaxValue]{1}-tbl[MaxValue]{0})/Table.RowCount(tbl),
AddColumn = Table.AddColumn
(
Index,
"New data value",
each try [GroupColumn]+([Index]*CalculateIncrement) otherwise [GroupColumn]
)
in
AddColumn
}
}
),
#"Removed Columns" = Table.RemoveColumns(TransformGroupedTable,{"GroupColumn"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "New data value"}, {"date", "New data value"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded AllRows",{{"date", type date}, {"New data value", type number}})
in
#"Changed Type1"
Copie y pegue este código en el editor avanzado en una nueva consulta en blanco para ver cómo funciona la solución.
Si esta publicación ayuda o resuelve su problema, márquelo como solución (para ayudar a otros usuarios a encontrar contenido útil y reconocer el trabajo de los usuarios que lo ayudaron)
Los kudoes también son agradables
Divertirse
Palanqueta
Alba
En respuesta a Jimmy801
Hola @retgeav
Esto sería más sencillo en DAX. Si lo necesita en PQ, pegue el siguiente código en una consulta en blanco para ver los pasos:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"date", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each try let currentValue_= Number.From([data value]), numSteps_ = Number.From(List.Select(#"Sorted Rows"[date],(inner)=>inner> [date]){0}-[date]), nextValue_ = Table.SelectRows(#"Sorted Rows",(inner)=>inner[date]> [date])[data value]{0} in Table.FromColumns({List.Numbers(Number.From([date]),numSteps_), List.Numbers(currentValue_, numSteps_, (nextValue_-currentValue_)/numSteps_)}) otherwise Table.FromColumns({{Number.From([date])},{[data value]}})),
#"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1", "Column2"}, {"Column1", "Column2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.1",{{"Column1", type date}, {"Column2", type number}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"date", "data value"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1", "date"}, {"Column2", "data value"}})
in
#"Renamed Columns"
Marque la pregunta resuelta cuando haya terminado y considere dar felicitaciones si las publicaciones son útiles.
Comuníquese conmigo en privado para obtener asistencia con cualquier necesidad de BI a gran escala, tutoría, etc.
Salud
CNENFRNL
Hola, @retgeav, puedes probar esto, solo una función List.Generate () hace el trabajo.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}, "en-GB"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added to Column" = Table.TransformColumns(
#"Added Index",
{{"Index", (row) =>
List.Generate(
() => [date=#"Added Index"[date]{row},
e=try #"Added Index"[date]{row+1} otherwise Date.AddDays(date,1),
incr=(#"Added Index"[data value]{row+1}-#"Added Index"[data value]{row})/Number.From(e-date),
value=#"Added Index"[data value]{row}],
each [date] < [e],
each [date=Date.AddDays([date],1), e=[e], value=[value]+incr, incr=[incr]],
each [[date], [value]]
)
}}
),
#"Removed Columns" = Table.RemoveColumns(#"Added to Column",{"date", "data value"}),
#"Expanded Index" = Table.ExpandTableColumn(#"Removed Columns", "Index", {"date", "value"}, {"date", "value"})
in
#"Expanded Index"
CNENFRNL
Hola, @retgeav, puedes probar esto, solo una función List.Generate () hace el trabajo.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}, "en-GB"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added to Column" = Table.TransformColumns(
#"Added Index",
{{"Index", (row) =>
List.Generate(
() => [date=#"Added Index"[date]{row},
e=try #"Added Index"[date]{row+1} otherwise Date.AddDays(date,1),
incr=(#"Added Index"[data value]{row+1}-#"Added Index"[data value]{row})/Number.From(e-date),
value=#"Added Index"[data value]{row}],
each [date] < [e],
each [date=Date.AddDays([date],1), e=[e], value=[value]+incr, incr=[incr]],
each [[date], [value]]
)
}}
),
#"Removed Columns" = Table.RemoveColumns(#"Added to Column",{"date", "data value"}),
#"Expanded Index" = Table.ExpandTableColumn(#"Removed Columns", "Index", {"date", "value"}, {"date", "value"})
in
#"Expanded Index"
Jimmy801
Hola @retgeav
esto requiere bastantes pasos y seguro que hay más posibilidades. Aquí el código M
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}, "en-DE"),
ListMissingDates = Table.RenameColumns(Table.FromList(List.Difference(List.Dates(List.Min(#"Changed Type"[date]),Duration.TotalDays(List.Max(#"Changed Type"[date])-List.Min(#"Changed Type"[date])), #duration(1,0,0,0) ), #"Changed Type"[date]), Splitter.SplitByNothing(),null, null, ExtraValues.Error), {{"Column1", "date"}}),
Combine = Table.Combine({#"Changed Type", ListMissingDates}),
#"Sorted Rows" = Table.Sort(Combine,{{"date", Order.Ascending}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "data value", "GroupColumn"),
#"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "data value", "MaxValue"),
#"Filled Down" = Table.FillDown(#"Duplicated Column1",{"GroupColumn"}),
#"Filled Up" = Table.FillUp(#"Filled Down",{"MaxValue"}),
#"Grouped Rows" = Table.Group(#"Filled Up", {"GroupColumn"}, {{"AllRows", each _, type table [date=date, data value=number, GroupColumn=number, MaxValue=number]}}),
TransformGroupedTable = Table.TransformColumns
(
#"Grouped Rows",
{
{
"AllRows",
(tbl)=>
let
Index= Table.AddIndexColumn(tbl,"Index",0,1),
CalculateIncrement = (tbl[MaxValue]{1}-tbl[MaxValue]{0})/Table.RowCount(tbl),
AddColumn = Table.AddColumn
(
Index,
"New data value",
each try [GroupColumn]+([Index]*CalculateIncrement) otherwise [GroupColumn]
)
in
AddColumn
}
}
),
#"Removed Columns" = Table.RemoveColumns(TransformGroupedTable,{"GroupColumn"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "New data value"}, {"date", "New data value"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded AllRows",{{"date", type date}, {"New data value", type number}})
in
#"Changed Type1"
Copie y pegue este código en el editor avanzado en una nueva consulta en blanco para ver cómo funciona la solución.
Si esta publicación ayuda o resuelve su problema, márquelo como solución (para ayudar a otros usuarios a encontrar contenido útil y reconocer el trabajo de los usuarios que lo ayudaron)
Los kudoes también son agradables
Divertirse
Palanqueta
Alba
En respuesta a Jimmy801
Hola @retgeav
Esto sería más sencillo en DAX. Si lo necesita en PQ, pegue el siguiente código en una consulta en blanco para ver los pasos:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDSNTDUNTIwtFTSUTI0MleK1QEKmiELmpqBBQ2NkQXNLSCCKCrNDJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, #"data value" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"data value", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"date", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each try let currentValue_= Number.From([data value]), numSteps_ = Number.From(List.Select(#"Sorted Rows"[date],(inner)=>inner> [date]){0}-[date]), nextValue_ = Table.SelectRows(#"Sorted Rows",(inner)=>inner[date]> [date])[data value]{0} in Table.FromColumns({List.Numbers(Number.From([date]),numSteps_), List.Numbers(currentValue_, numSteps_, (nextValue_-currentValue_)/numSteps_)}) otherwise Table.FromColumns({{Number.From([date])},{[data value]}})),
#"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1", "Column2"}, {"Column1", "Column2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom.1",{{"Column1", type date}, {"Column2", type number}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"date", "data value"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1", "date"}, {"Column2", "data value"}})
in
#"Renamed Columns"
Marque la pregunta resuelta cuando haya terminado y considere dar felicitaciones si las publicaciones son útiles.
Comuníquese conmigo de manera privada para obtener asistencia con cualquier necesidad de BI a gran escala, tutoría, etc.
Salud