qwertyh
Hola,
Estoy tratando de agregar una columna de fecha de vigencia que será la fecha que impulsará los cálculos de esta tabla. Esto expandirá la tabla duplicando cada fila para cada mes-año único que se encuentre entre la fecha de inicio y finalización inclusive y siempre será el primero de cada mes.
Por ejemplo, con esta tabla,
ID de política | Fecha de inicio | Fecha final | Otros Columna etc.. |
ABC523 | 1/5/2020 | 5/1/2021 | |
BAT325 | 2/1/2021 | 20/05/2022 | |
CAT542 | 3/6/2019 | 3/6/2020 |
Me gustaría tener esto:
ID de política | Fecha de inicio | Fecha final | Fecha de vigencia | Otros Columna etc.. |
ABC523 | 1/5/2020 | 5/1/2021 | 1/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 2/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 3/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 4/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 5/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 01/06/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 7/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 8/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 9/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 10/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 11/1/2020 | |
ABC523 | 1/5/2020 | 5/1/2021 | 12/1/2020 | |
ABC523 | 1/1/2020 | 1/1/2021 | 1/1/2021 | |
BAT325 | 2/1/2021 | 20/05/2022 | 2/1/2021 | |
BAT325 | 2/1/2021 | 20/05/2022 | 3/1/2021 |
etc..
Gracias por la ayuda.
Jakintá
Puedes probar esto…
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
DateType = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
DateList = Table.AddColumn(DateType, "Inforce Date", each List.Distinct(List.Transform({Number.From([Start Date]) .. Number.From([End Date])}, each Date.StartOfMonth(Date.From(_))))),
FINAL = Table.ExpandListColumn(DateList, "Inforce Date")
in
FINAL
edhans
Prueba esto @qwertyh
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
#"Added Date List" =
Table.AddColumn(
#"Changed Type",
"Date List",
each
let
varStartDate = Date.StartOfMonth([Start Date]),
varEndDate = Date.StartOfMonth([End Date]) + #duration(1,0,0,0),
varDates = List.Dates(varStartDate, Duration.TotalDays(varEndDate - varStartDate), #duration(1,0,0,0))
in
List.Select(varDates, each _ = Date.StartOfMonth(_))
),
#"Expanded Date List" = Table.ExpandListColumn(#"Added Date List", "Date List")
in
#"Expanded Date List"
Hace esto:
- Obtiene su fecha de inicio y busca el primer día del noveno.
- Obtiene su fecha de finalización y encuentra el primer día de ese mes, luego agrega 1 día.
- Crea una lista de todas las fechas intermedias.
- Mantiene solo aquellas fechas que son el inicio del mes.
- Los expande a una nueva fila.
Estos son algunos de los datos:
Cómo usar el código M proporcionado en una consulta en blanco:
1) En Power Query, seleccione Nueva fuente, luego Consulta en blanco
2) En la cinta Inicio, seleccione el botón «Editor avanzado»
3) Elimina todo lo que ves, luego pega el código M que te he dado en ese cuadro.
4) Presiona Listo
5) Consulte este artículo si necesita ayuda para usar este código M en su modelo.
Jakintá
Puedes probar esto…
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
DateType = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
DateList = Table.AddColumn(DateType, "Inforce Date", each List.Distinct(List.Transform({Number.From([Start Date]) .. Number.From([End Date])}, each Date.StartOfMonth(Date.From(_))))),
FINAL = Table.ExpandListColumn(DateList, "Inforce Date")
in
FINAL
qwertyh
En respuesta a Jakintá
Tanto esta respuesta como la respuesta de Ed Han resuelven mi pregunta. Elegí este por su sencillez y belleza. La respuesta de EdHan es mucho más fácil de seguir y me encantan las instrucciones y explicaciones que se brindan para ayudar a comprender el código.
¡Gracias a los dos!
edhans
En respuesta a qwertyh
Genial @qwertyh: me alegro de que hayas encontrado una solución que funcione.
Para referencia futura, puede marcar múltiples respuestas como «la solución» a un hilo si encuentra que más de una respuesta funciona.
Tiendo a optar por un código un poco más detallado y formateado, que ocupa unas pocas líneas más, pero me resulta más fácil de recorrer. 😁