Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Soy muy nuevo en PowerBi y estoy aprendiendo a hacer las cosas que quiero al encontrar respuestas a problemas similares planteados en los foros y en blogs / videos.
Necesito calcular un período (fecha de inicio y fecha de finalización, en columnas calculadas) basado en datos de diferentes filas y columnas. Puedo hacer lo que estoy después en anidados if instrucciones en Excel, pero estoy luchando para averiguar cómo hacer lo mismo en PowerBI.
Escenario:
Los datos son para los niños, donde se producen reuniones periódicas. Necesito identificar un período (Safer Me CIN) basado en un resultado de esas reuniones y las fechas de esas reuniones.
Estructura de datos:
Tabla - Los detalles de la reunión provienen de respuestas rellenadas en formularios traídos a PowerBi en 'tbl_007SaferMeCIN – Detalles del formulario'. Esta tabla está vinculada (muchos - uno) a la tabla Persona 'tbl_004_PersonView'
Relación - tbl_004_PersonView[Eclipse ID] = tbl_007SaferMeCIN – Detalles del formulario[PERid]
Nombres de columna = [PERid], [CALC_001 fecha de reunión], [número de reunión de CIN], [CALC_002 CIN safer me], [número de reunión de CIN]
2 columnas deseadas:[Fecha de inicio], [Fecha de finalización]
Nota: 3 de estas columnas son columnas calculadas: 2 según sea necesario para obtener los datos de la base de datos de origen y 1 para clasificar las reuniones.
lógica:
Fecha de inicio = la fecha de la primera reunión (CALC_001 fecha de la reunión) donde el resultado (CALC-002 Safer Me CIN) = "Sí". El niño sigue siendo "Safer Me CIN" cuando el resultado de la próxima reunión sigue siendo "Sí".
Fecha de finalización = la fecha de la reunión menos 1 donde el resultado es "No".
Cuando el resultado de la reunión es "No", el niño no se clasifica como en un período de Safer Me CIN (en blanco ()).
Resultado deseado:
Originalmente, quería solo 2 columnas calculadas agregadas en la misma tabla que los datos de la reunión, pero pensando de manera más lógica, realmente me gustaría que estos datos generaran una nueva tabla que se pueda vincular al elemento secundario individual (tabla PersonView).
Nuevo efecto deseado de tabla
POR ID | fecha de inicio | fecha final |
1001 | 19/08/2020 | 17/09/2020 |
1002 | 07/12/2020 | 21/04/2021 |
1004 | 01/10/2020 | 21/10/2020 |
1004 | 16/11/2020 | 05/01/2021 |
1005 | 26/05/2021 | En blanco() |
1006 | 29/07/2020 | En blanco() |
Efecto deseado si hay nuevas columnas calculadas en la tabla original
POR ID | CALC_001 fecha de reunión | Número de reunión de CIN | CALC_002 Safer Me CIN | fecha de inicio | fecha final |
1001 | 19/08/2020 | 1 | Sí | 19/08/2020 | 17/09/2020 |
1001 | 18/09/2020 | 2 | No | En blanco() | En blanco() |
1001 | 28/10/2020 | 3 | No | En blanco() | En blanco() |
1002 | 07/12/2020 | 1 | Sí | 07/12/2020 | 21/04/2021 |
1002 | 13/03/2021 | 2 | Sí | 07/12/2020 | 21/04/2021 |
1002 | 22/04/2021 | 3 | No | En blanco() | En blanco() |
1003 | 02/09/2020 | 1 | No | En blanco() | En blanco() |
1004 | 01/10/2020 | 1 | Sí | 01/10/2020 | 21/10/2020 |
1004 | 22/10/2020 | 2 | No | En blanco() | En blanco() |
1004 | 16/11/2020 | 3 | Sí | 16/11/2020 | 05/01/2021 |
1004 | 06/01/2021 | 4 | No | En blanco() | En blanco() |
1005 | 27/04/2021 | 1 | No | En blanco() | En blanco() |
1005 | 26/05/2021 | 2 | Sí | 26/05/2021 | En blanco() |
1005 | 16/06/2021 | 3 | Sí | 26/05/2021 | En blanco() |
1006 | 29/07/2020 | 1 | Sí | 29/07/2020 | En blanco() |
1006 | 26/08/2020 | 2 | Sí | 29/07/2020 | En blanco() |
1006 | 22/09/2020 | 3 | Sí | 29/07/2020 | En blanco() |
1006 | 09/03/2021 | 4 | Sí | 29/07/2020 | En blanco() |
Fórmula utilizada en Excel:
Fecha de inicio - =IF(AND(A3<>A2,D3="Yes"),B3,IF(AND(A3=A2,D2="No", D3="Yes"), B3,IF(A3=A2, D2="Yes", D3="Yes"),H2,IF(D3="No","","Check"))))
Fecha de finalización - =IF(AND(A3=A4, D3="Yes",D4="No"),(B4)-1,IF(AND(A3<> A4, D3="Yes"),"",IF(AND(A3=A4, D3="Yes", D4="Yes"),I4,IF(D3="No","","Check"))))
Nota : "comprobar" solo allí para detectar un error en la fórmula.
Solved! Go to Solution.
Esta solución utiliza columnas calculadas y una tabla calculada.
1. Crear columna calculada [Fecha de inicio]:
Start Date =
VAR vPerID = 'tbl_007SaferMeCIN – Form Details'[PER ID]
VAR vMeetingDate = 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting]
VAR vFirstMeetingDateOutcomeYes =
CALCULATE (
MIN ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "Yes"
)
VAR vPrevMeetingDate =
CALCULATE (
MAX ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] < vMeetingDate
)
VAR vPrevMeetingDateOutcome =
CALCULATE (
MAX ( 'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] = vPrevMeetingDate
)
VAR vCountOutcomeNo =
COUNTROWS (
FILTER (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID] = vPerID
&& 'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No"
&& 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] > vFirstMeetingDateOutcomeYes
&& 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] < vMeetingDate
)
)
VAR vResult =
SWITCH (
TRUE (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No", BLANK (),
ISBLANK ( vCountOutcomeNo ), vFirstMeetingDateOutcomeYes,
vPrevMeetingDateOutcome = "No", vMeetingDate
)
RETURN
vResult
2. Crear columna calculada [Fecha de finalización]:
End Date =
VAR vMeetingDate = 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting]
VAR vNextMeetingDateOutcomeNo =
CALCULATE (
MIN ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No",
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] > vMeetingDate
)
VAR vResult =
SWITCH (
TRUE (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No"
|| ISBLANK ( vNextMeetingDateOutcomeNo ), BLANK (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "Yes", vNextMeetingDateOutcomeNo - 1
)
RETURN
vResult
3. Crear tabla calculada:
New Table =
CALCULATETABLE (
SUMMARIZE (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID],
'tbl_007SaferMeCIN – Form Details'[Start Date],
'tbl_007SaferMeCIN – Form Details'[End Date]
),
NOT ( ISBLANK ( 'tbl_007SaferMeCIN – Form Details'[Start Date] ) )
)
Esta solución utiliza columnas calculadas y una tabla calculada.
1. Crear columna calculada [Fecha de inicio]:
Start Date =
VAR vPerID = 'tbl_007SaferMeCIN – Form Details'[PER ID]
VAR vMeetingDate = 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting]
VAR vFirstMeetingDateOutcomeYes =
CALCULATE (
MIN ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "Yes"
)
VAR vPrevMeetingDate =
CALCULATE (
MAX ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] < vMeetingDate
)
VAR vPrevMeetingDateOutcome =
CALCULATE (
MAX ( 'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] = vPrevMeetingDate
)
VAR vCountOutcomeNo =
COUNTROWS (
FILTER (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID] = vPerID
&& 'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No"
&& 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] > vFirstMeetingDateOutcomeYes
&& 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] < vMeetingDate
)
)
VAR vResult =
SWITCH (
TRUE (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No", BLANK (),
ISBLANK ( vCountOutcomeNo ), vFirstMeetingDateOutcomeYes,
vPrevMeetingDateOutcome = "No", vMeetingDate
)
RETURN
vResult
2. Crear columna calculada [Fecha de finalización]:
End Date =
VAR vMeetingDate = 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting]
VAR vNextMeetingDateOutcomeNo =
CALCULATE (
MIN ( 'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] ),
ALLEXCEPT (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID]
),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No",
'tbl_007SaferMeCIN – Form Details'[CALC_001 Date of Meeting] > vMeetingDate
)
VAR vResult =
SWITCH (
TRUE (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "No"
|| ISBLANK ( vNextMeetingDateOutcomeNo ), BLANK (),
'tbl_007SaferMeCIN – Form Details'[CALC_002 Safer Me CIN] = "Yes", vNextMeetingDateOutcomeNo - 1
)
RETURN
vResult
3. Crear tabla calculada:
New Table =
CALCULATETABLE (
SUMMARIZE (
'tbl_007SaferMeCIN – Form Details',
'tbl_007SaferMeCIN – Form Details'[PER ID],
'tbl_007SaferMeCIN – Form Details'[Start Date],
'tbl_007SaferMeCIN – Form Details'[End Date]
),
NOT ( ISBLANK ( 'tbl_007SaferMeCIN – Form Details'[Start Date] ) )
)
Muchas gracias por esto. Parece estar funcionando muy bien. Ahora estudiaré el código para aprender cómo funciona para poder escribir este estilo de código en el futuro. 😁
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |
User | Count |
---|---|
2 | |
2 | |
2 | |
2 | |
1 |