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

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.

Reply
Syndicate_Admin
Administrator
Administrator

Generar nueva tabla o columnas calculadas basadas en columnas y filas

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 IDfecha de iniciofecha final
100119/08/202017/09/2020
100207/12/202021/04/2021
100401/10/202021/10/2020
100416/11/202005/01/2021
100526/05/2021En blanco()
100629/07/2020En blanco()

Efecto deseado si hay nuevas columnas calculadas en la tabla original

POR IDCALC_001 fecha de reuniónNúmero de reunión de CINCALC_002 Safer Me CINfecha de iniciofecha final
100119/08/2020119/08/202017/09/2020
100118/09/20202NoEn blanco()En blanco()
100128/10/20203NoEn blanco()En blanco()
100207/12/2020107/12/202021/04/2021
100213/03/2021207/12/202021/04/2021
100222/04/20213NoEn blanco()En blanco()
100302/09/20201NoEn blanco()En blanco()
100401/10/2020101/10/202021/10/2020
100422/10/20202NoEn blanco()En blanco()
100416/11/2020316/11/202005/01/2021
100406/01/20214NoEn blanco()En blanco()
100527/04/20211NoEn blanco()En blanco()
100526/05/2021226/05/2021En blanco()
100516/06/2021326/05/2021En blanco()
100629/07/2020129/07/2020En blanco()
100626/08/2020229/07/2020En blanco()
100622/09/2020329/07/2020En blanco()
100609/03/2021429/07/2020En 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.

1 ACCEPTED SOLUTION
Syndicate_Admin
Administrator
Administrator

@lyndaGough,

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] ) )
)

DataInsights_0-1627867866210.png

View solution in original post

2 REPLIES 2
Syndicate_Admin
Administrator
Administrator

@lyndaGough,

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] ) )
)

DataInsights_0-1627867866210.png

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. 😁

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.