Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

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

 

diafecha inicialfecha finaldía
1374291112/08/202212/08/20221
35951421/12/202122/12/20212
35951422/12/202122/12/20212
35951417/01/202219/01/20223
35951418/01/202219/01/20223
35951419/01/202219/01/20223
  • 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 _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

5 Replies

  • 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 _ kalyj

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Solo te pude dar un like, jajajaja
      me ha servido demasiado muchas muchas gracias

  • miguel's avatar
    miguel
    Icon for Community Admin rankCommunity 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?

    • Anonymous's avatar
      Anonymous
      Not applicable

      En la foto de color azul, la que dice dias reales

  • Anonymous's avatar
    Anonymous
    Not 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 Sub

     

    Adjunto pantalla de un ejemplo de los datos que manejo