Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
miquinti
Frequent Visitor

Contar días acumulados consecutivos por persona

Necesito contar el número de días consecutivos por pesona (idPersona) :

miquinti_0-1696145847306.png

Se me ha ocurrido (es posible que no sea lo mejor) Convinar consigo misma la tabla y añadir la Fecha y el identificador del registro siguiente para poder sumar los días transcurridos:

miquinti_1-1696146278826.png

Pero no se como ir sumando, acumulando los "Días Transcurridos" para los mismosmos idPersona, reseteando la suma cuando se cambien de persona (IdPersona), resultando algo similar a los número de la primera imagen.

 

Si me pueden ayudar con cualquier sugerencia, comentario. Muchas gracias

2 ACCEPTED SOLUTIONS
Syndicate_Admin
Administrator
Administrator

Por favor, pruebe esto

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc1LCsAgDEXRvWQsIb4Ybdci7n8bph9oI8IdncntnUCJYCzGECiN1EmdpASyl3QhC5RvUxb8rF1W2VusbezY2BkMxS1beDxWPxsT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IDPERSONA = _t, Fecha = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IDPERSONA", Int64.Type}, {"Fecha", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"IDPERSONA"}, {{"Count", (x) => Table.AddColumn( x,
  "Duration_Days", each  Duration.Days([Fecha]-
                      x[Fecha]{0}), type number
)
}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"IDPERSONA", "Fecha", "Duration_Days"}, {"IDPERSONA", "Fecha", "Duration_Days"})
in
    #"Expanded Count"

View solution in original post

Syndicate_Admin
Administrator
Administrator

Por favor, pruebe esto

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdFLCsAgDATQu2Qtko9Rexbp/a9R+1kYO5DVY2BCMgYpJVLP7FlZjc40yCZxCeQf2UYeSB6zzLpYu63mOZs1YB3YEUzLNPHQ8VoNVuTbxXcrwBxYBdaAdWDH34SByd8M5AzkWMJdXlNgBqwCa8CWf5wX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IDPERSONA = _t, Fecha = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IDPERSONA", Int64.Type}, {"Fecha", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"IDPERSONA"}, {{"Count", (x) => Table.AddColumn( Table.AddIndexColumn( x,"Indx",0,1),
  "ID",  (z)=> [
                          test = Duration.Days( z[Fecha]-(
                      {x[Fecha]{0}}&x[Fecha]){z[Indx]}),
                      Result = if test > 1 then z[Indx] else null][Result],
            type number
)
}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"IDPERSONA", "Fecha", "Indx", "ID"}, {"IDPERSONA", "Fecha", "Indx", "ID"}),
    #"Filled Down" = Table.FillDown(#"Expanded Count",{"ID"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filled Down",{{"Indx", type number}, {"ID", type number}}),
    #"Grouped Rows1" = Table.Group(#"Changed Type1", {"IDPERSONA", "ID"}, {{"Count", each Table.AddColumn(_,"Duration_Days",(z)=> if z[ID] = null then z[Indx] else   z[Indx]-z[ID],type number
)
}}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Grouped Rows1",{"Count"}),
    #"Expanded Count1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Count", {"IDPERSONA", "Fecha", "Duration_Days"}, {"IDPERSONA", "Fecha", "Duration_Days"})
in
    #"Expanded Count1"

View solution in original post

8 REPLIES 8
miquinti
Frequent Visitor

Muchas gracias a todos/as por su ayuda.

 

He modificado un poco el código de @Ahmedx  ya que algunos casos de quedaban en negativo. Añadí un reseteo cada vez que estaba a 0:

 

    #"Personalizada agregada" = Table.AddColumn(#"Expanded Count", "Reset_0", each if [Indx] = 0 then 0 else [ID]),
    #"Columnas quitadas" = Table.RemoveColumns(#"Personalizada agregada",{"ID"}),
    #"Columnas con nombre cambiado" = Table.RenameColumns(#"Columnas quitadas",{{"Reset_0", "ID"}}),
    #"Filled Down" = Table.FillDown(#"Columnas con nombre cambiado",{"ID"}),

 Añadiendo una columna que marca hasta cuando hacer el Filled Down, quedan todos los casos resueltos.

Syndicate_Admin
Administrator
Administrator

Por favor, pruebe esto

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdFLCsAgDATQu2Qtko9Rexbp/a9R+1kYO5DVY2BCMgYpJVLP7FlZjc40yCZxCeQf2UYeSB6zzLpYu63mOZs1YB3YEUzLNPHQ8VoNVuTbxXcrwBxYBdaAdWDH34SByd8M5AzkWMJdXlNgBqwCa8CWf5wX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IDPERSONA = _t, Fecha = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IDPERSONA", Int64.Type}, {"Fecha", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"IDPERSONA"}, {{"Count", (x) => Table.AddColumn( Table.AddIndexColumn( x,"Indx",0,1),
  "ID",  (z)=> [
                          test = Duration.Days( z[Fecha]-(
                      {x[Fecha]{0}}&x[Fecha]){z[Indx]}),
                      Result = if test > 1 then z[Indx] else null][Result],
            type number
)
}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"IDPERSONA", "Fecha", "Indx", "ID"}, {"IDPERSONA", "Fecha", "Indx", "ID"}),
    #"Filled Down" = Table.FillDown(#"Expanded Count",{"ID"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filled Down",{{"Indx", type number}, {"ID", type number}}),
    #"Grouped Rows1" = Table.Group(#"Changed Type1", {"IDPERSONA", "ID"}, {{"Count", each Table.AddColumn(_,"Duration_Days",(z)=> if z[ID] = null then z[Indx] else   z[Indx]-z[ID],type number
)
}}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Grouped Rows1",{"Count"}),
    #"Expanded Count1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Count", {"IDPERSONA", "Fecha", "Duration_Days"}, {"IDPERSONA", "Fecha", "Duration_Days"})
in
    #"Expanded Count1"
Syndicate_Admin
Administrator
Administrator

Prueba esto

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc1LCsAgDEXRvWQsIb4Ybdci7n8bph9oI8IdncntnUCJYCzGECiN1EmdpASyl3QhC5RvUxb8rF1W2VusbezY2BkMxS1beDxWPxsT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IDPERSONA = _t, Fecha = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IDPERSONA", Int64.Type}, {"Fecha", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"IDPERSONA"}, {{"Count", (x) => Table.AddColumn( Table.AddIndexColumn( x,"Indx",0,1),
  "Duration_Days",  (z)=> [
                          test = Duration.Days( z[Fecha]-(
                      {x[Fecha]{0}}&x[Fecha]){z[Indx]}),
                         
                         Diffdays = 
                        Duration.Days(z[Fecha]-
                      x[Fecha]{0}), 
                      Result = if test > 1 then 0 else Diffdays][Result],
            type number
)
}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"IDPERSONA", "Fecha", "Duration_Days"}, {"IDPERSONA", "Fecha", "Duration_Days"})
in
    #"Expanded Count"

Muchas gracias, lo he probado y va bien en el primer caso, pero faltaría alguna condicion para resetear el contador, ya que ella siguiente sigue acumulado los días:

miquinti_0-1696163812275.png

 

miquinti
Frequent Visitor

Me ha surgido una nueva casuistica a este respecto:

miquinti_0-1696157052911.png

 

Necesito que si los dias por Persona no son consecutivos, según campo Fecha, como lo marcado en azul, también se resetee la cuenta. en el caso de la imagen, al No ser 11/05 y 21/05 (Diferenica entre ambas fechas > 1) resetear la cuenta a 0. Podrian ayudarme con esto?

Syndicate_Admin
Administrator
Administrator

Si esta publicación ayuda, entonces considere aceptarla como la solución para ayudar a otros miembros a encontrarla más rápido y dar un gran pulgar hacia arriba.

miquinti
Frequent Visitor

Es perfecto, este es el resultado:

 

miquinti_1-1696150722632.png

 

 

Muchisimas gracias.

Syndicate_Admin
Administrator
Administrator

Por favor, pruebe esto

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc1LCsAgDEXRvWQsIb4Ybdci7n8bph9oI8IdncntnUCJYCzGECiN1EmdpASyl3QhC5RvUxb8rF1W2VusbezY2BkMxS1beDxWPxsT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [IDPERSONA = _t, Fecha = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"IDPERSONA", Int64.Type}, {"Fecha", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"IDPERSONA"}, {{"Count", (x) => Table.AddColumn( x,
  "Duration_Days", each  Duration.Days([Fecha]-
                      x[Fecha]{0}), type number
)
}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Count"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Other Columns", "Count", {"IDPERSONA", "Fecha", "Duration_Days"}, {"IDPERSONA", "Fecha", "Duration_Days"})
in
    #"Expanded Count"

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors
Top Kudoed Authors