Forum Discussion
insertar filas por fechas
Saludo cordial
Quisiera saber por medio de Power Query como se puede insertar una fila como la que está en negrilla
Es decir, que si la columna dias tiene un valor mayor a uno se inserte la cantidad de filas que indica dicha columna, en el caso de la primera fila en negrilla se duplica la fila superior y se le suma un dia por que tiene la columna dias el numero 2.
En el caso de la segunda fila en neqrilla suman un total de 3 filas ya que la primera indica un número 3 y se puede ver que se adiciona un día a cada uno
El caso es que son mas de 200.000 filas
| dia | fecha inicial | fecha final | día |
| 13742911 | 12/08/2022 | 12/08/2022 | 1 |
| 359514 | 21/12/2021 | 22/12/2021 | 2 |
| 359514 | 22/12/2021 | 22/12/2021 | 2 |
| 359514 | 17/01/2022 | 19/01/2022 | 3 |
| 359514 | 18/01/2022 | 19/01/2022 | 3 |
| 359514 | 19/01/2022 | 19/01/2022 | 3 |
There Anonymous ,
According to your description, here's my solution.
Add a custom column.
List.Dates([start date],[day],#duration(1, 0, 0, 0))Expand the custom column list.
Then remove the start date column and rename the custom column to start date, then reorder columns, get the correct result.
Here's the whole M syntax:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtK30Dc0UtJBYRsqxepAZQ2N9I0MYdIgDlgtkry+oTlcWt/QEsg2VoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"start date" = _t, #"end date" = _t, day = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"start date", type date}, {"end date", type date}, {"day", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Dates([start date],[day],#duration(1, 0, 0, 0))), #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"), #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"start date"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "start date"}}), #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"start date", "end date", "day"}) in #"Reordered Columns"I attach my sample below for your reference.
Best Regards,
Community Support Team _ kalyjIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
5 Replies
- v-yanjiang-msft
Community Support
There Anonymous ,
According to your description, here's my solution.
Add a custom column.
List.Dates([start date],[day],#duration(1, 0, 0, 0))Expand the custom column list.
Then remove the start date column and rename the custom column to start date, then reorder columns, get the correct result.
Here's the whole M syntax:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtK30Dc0UtJBYRsqxepAZQ2N9I0MYdIgDlgtkry+oTlcWt/QEsg2VoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"start date" = _t, #"end date" = _t, day = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"start date", type date}, {"end date", type date}, {"day", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Dates([start date],[day],#duration(1, 0, 0, 0))), #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"), #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"start date"}), #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "start date"}}), #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"start date", "end date", "day"}) in #"Reordered Columns"I attach my sample below for your reference.
Best Regards,
Community Support Team _ kalyjIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- AnonymousNot applicable
Solo te pude dar un like, jajajaja
me ha servido demasiado muchas muchas gracias
- miguel
Community Admin
Hola!
La tabla compartida muestra dos campos con el nombre "dia". ¿Podrías compartir otras imágenes o tablas que sirvan de referencia sobre lo que necesitarías?
- AnonymousNot applicable
En la foto de color azul, la que dice dias reales
- AnonymousNot applicable
Hola, si, mira incluso ya pude con una macro (lenguaje VBA) hacer lo que quería con el siguiente código, pero ya lo que deseo es hacerlo con lenguaje M
La siguiente macro lo que hace es inserta una fila dependiendo del valor que esté en la columna T, pero disminuyendole una (que es como lo necesito)Sub Insertar_fechas(): On Error Resume Next
Application.ScreenUpdating = False
For x = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
For f = 1 To Range("T" & x) - 1
Rows(x + 1).Insert
Next
Next
ActiveCell.Offset(0, 6).Columns("A:A").EntireColumn.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C+1"
End SubAdjunto pantalla de un ejemplo de los datos que manejo