Forum Discussion

Syndicate_Admin's avatar
Syndicate_Admin
Icon for Administrator rankAdministrator
4 years ago
Solved

Combinando cada 2 filas

Estoy tratando de combinar cada 2 filas juntas así: filas 1 y 2, filas 3 y 4, filas 5 y 6 y así sucesivamente.

¿Cómo haría esto en power query?

  • Syndicate_Admin's avatar
    Syndicate_Admin
    4 years ago

    Hay @mhgottsc

    Descargue el archivo PBIX de ejemplo con el código siguiente

    Esta consulta hace lo que quieres.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLLTsMwEEV/ZRQ2IFVg59HHEtIiELQUWHQRdeEmQ7Hk2MEPVeHrcaIuQEWpJVuW7PG5M3emKCISjSJyTbrjkoBqUJobAqVQBqsrf3nH97BS3LSwVFZpE21HRSSdEP7tePj10jRKWye5beECaJykfdw5+FLtuEDIVd0w2YagPXkShH5m5lsoeJSVM1ZzDEs8nWR9HO24cTLNBhTypxm8Y+m0/xsCTyg9dYX24L9c3RrLBOQCmYYNs6hh7VU+OAbaT9JfVQyZ9Habvwa5TrJT1/9Jfa4O0voNK77/tKVwuyBj4nTax407bjc8QyljBQ9+DhEWXw671jIZIjIjNA4anLlGVh+UFhUspPfeMi5rlDZEZJwcK0nOqWyYrFD7hsJ9HkJO09iTtz8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Expected = _t, #"Next Activity" = _t, #"Company / Opportunity" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Expected", Int64.Type}, {"Next Activity", type text}, {"Company / Opportunity", type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if Number.Mod([Index],2) = 0 then try Text.Combine({[#"Company / Opportunity"], #"Added Index"[#"Company / Opportunity"]{[Index]+1}}, "," ) otherwise "" else ""),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Value] <> null))
    in
        #"Filtered Rows"

    mergerows.png

    Saludos

    Phil

4 Replies

  • Hay @mhgottsc

    Descargar archivo PBIX de ejemplo

    Puede transponer la tabla y, a continuación, combinar pares de columnas y, a continuación, volver a transponer la tabla (volver a las filas).

    trans1.png

    trans2.png

    trans3.png

    trans4.png

    Pero sin ver sus datos, no sé si esa es una solución viable para usted. Es posible que tenga demasiadas filas para hacer esto a mano inicialmente. Proporcione algunos datos de muestra o preferiblemente su archivo PBIX.

    Saludos

    Phil

    • Syndicate_Admin's avatar
      Syndicate_Admin
      Icon for Administrator rankAdministrator

      Aquí hay una toma de los datos si eso ayuda:

      Para la columna de oportunidad de empresa, quiero tener datos en la fila 2 anexados a la fila 1, con el mismo patrón que continúa para la fila 4 que se anexa a la fila 3, pero solo para la columna empresa / oportunidad para que pueda pasar y eliminar las filas par que tienen el valor nulo. ¡Muchas gracias!

      mhgottsc_0-1636469570122.png

      • Syndicate_Admin's avatar
        Syndicate_Admin
        Icon for Administrator rankAdministrator

        Hay @mhgottsc

        Descargue el archivo PBIX de ejemplo con el código siguiente

        Esta consulta hace lo que quieres.

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLLTsMwEEV/ZRQ2IFVg59HHEtIiELQUWHQRdeEmQ7Hk2MEPVeHrcaIuQEWpJVuW7PG5M3emKCISjSJyTbrjkoBqUJobAqVQBqsrf3nH97BS3LSwVFZpE21HRSSdEP7tePj10jRKWye5beECaJykfdw5+FLtuEDIVd0w2YagPXkShH5m5lsoeJSVM1ZzDEs8nWR9HO24cTLNBhTypxm8Y+m0/xsCTyg9dYX24L9c3RrLBOQCmYYNs6hh7VU+OAbaT9JfVQyZ9Habvwa5TrJT1/9Jfa4O0voNK77/tKVwuyBj4nTax407bjc8QyljBQ9+DhEWXw671jIZIjIjNA4anLlGVh+UFhUspPfeMi5rlDZEZJwcK0nOqWyYrFD7hsJ9HkJO09iTtz8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Expected = _t, #"Next Activity" = _t, #"Company / Opportunity" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Expected", Int64.Type}, {"Next Activity", type text}, {"Company / Opportunity", type text}}),
            #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if Number.Mod([Index],2) = 0 then try Text.Combine({[#"Company / Opportunity"], #"Added Index"[#"Company / Opportunity"]{[Index]+1}}, "," ) otherwise "" else ""),
            #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
            #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Value] <> null))
        in
            #"Filtered Rows"

        mergerows.png

        Saludos

        Phil

  • Puede crear un duplicado de la tabla. En la tabla 1, agregue una columna de índice a partir de 1; en la tabla 2, agregue una columna de índice a partir de 0. A continuación, combine ambas tablas en función de las columnas de índice