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
Anonymous
Not applicable

Jerarquía padre hijo recuento hijos

Tengo una tabla con columnas employeeID, supervisorID. Pude generar camino sin ningún problema. Estoy buscando para calcular el recuento de niños y el recuento de niños directos con el resultado como se muestra a continuación.

emploeeIDsupervisorIDCaminoconteo de niños directosconteo de niños
et01 et0128
et02et01et01-et0200
et03et01et01-et0326
et04et03et01-et03-et0400
et05et03et01-et03-et0544
et06et05et01-et03-et05-et0600
et07et05et01-et03-et05-et0700
et08et05et01-et03-et05-et0800
et09et05et01-et03-et05-et0900

Creé una fórmula. pero es muy lento. Tengo más de 10000 empleados con 7 niveles de jerarquía.


var employee_id ('Tabla'[HCI_EMPLOYEEID])
resultado de var: CALCULATE (
COUNTrows ( 'Tabla'),
IFERROR(SEARCH(employee_id,'Table'[Path]),-1) > 0,
TODO ( 'Tabla' ) )
devolución
Resultado

¿Alguien tiene una idea de cómo resolverlo rápidamente?

fórmulas en Excel fueron fáciles

Directa

•COUNTIF([supervisorID];[ @employeeID])

todo

•COUNTIF([ruta];" *"&[@emploeeID]&"*")-1
2 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hola @HoKe

Así es como escribiría estas columnas calculadas, usando PATHCONTAINS para la columna [count of children].

Tenga en cuenta que devolverán en blanco en lugar de cero en el caso de que no haya hijos, pero podría agregar cero al final si lo desea.

¿Estos funcionan mejor para ti?

saludos

Owen

count of children direct =
CALCULATE (
    COUNTROWS ( 'Table' ),
    TREATAS ( { 'Table'[employeeID] }, 'Table'[supervisorID] ),
    ALL ( 'Table' )
)

count of children =
VAR CurrentEmployeeID = 'Table'[employeeID]
RETURN
    CALCULATE (
        COUNTROWS ( 'Table' ),
        PATHCONTAINS ( 'Table'[path], CurrentEmployeeID ),
        'Table'[employeeID] <> CurrentEmployeeID,
        ALL ( 'Table' )
    )


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

amitchandak
Super User
Super User

@HoKe , Pruebe nuevas columnas como

Direct = var _1 = 'Table'[emploeeID] 
return COUNTX(FILTER('Table', 'Table'[supervisorID] =_1),'Table'[emploeeID])

Total = var _1 = 'Table'[emploeeID] 
return COUNTX(FILTER('Table', PATHCONTAINS('Table'[path],_1)),'Table'[emploeeID])-1

Medidas

Direct Count = var _1 = max('Table'[emploeeID] )
return COUNTX(FILTER(ALLSELECTED('Table'), 'Table'[supervisorID] =_1),'Table'[emploeeID])

Total measure = var _1 = max('Table'[emploeeID]) 
return COUNTX(FILTER(ALLSELECTED('Table'), PATHCONTAINS('Table'[path],_1)),'Table'[emploeeID])-1

Buscar archivo después de la firma

View solution in original post

4 REPLIES 4
CNENFRNL
Community Champion
Community Champion

@HoKe, pregunta interesante, de hecho!

En cuanto a la cuenta de niños directos, las soluciones son sencillas.

Solución de columna calculada,

# Direct = COUNTROWS ( FILTER ( 'Table', 'Table'[supervisorID] = EARLIER ( 'Table'[emploeeID] ) ) )

Solución de medida

Direct = 
COUNTROWS ( FILTER ( ALL ( 'Table' ), 'Table'[supervisorID] = MAX ( 'Table'[emploeeID] ) ) )

Calculated ColumnColumna calculadaMeasure in Table vizMedida en la tabla viz

En cuanto a la cuenta de niños, la dificultad camina exponencialmente!! Debido a que DAX carece de funciones o metodologías para crear algunas funciones recursivas sencillas. Afortunadamente, con la ruta de acceso dela columna de ayuda, la solución es así,

# All Children = 
COUNTROWS (
    FILTER (
        'Table',
        NOT ( ISERROR ( SEARCH ( EARLIER ( 'Table'[emploeeID] ), 'Table'[path] ) ) )
    )
) - 1

Screenshot 2020-11-09 033135.png

btw, mi solución PQ para enumerar la jerarquía es

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSi0xMFTSUVKK1QGzjYBssBCUb4zGN4HwjWF8UzS+GYRvCuObo/Et0PiWcH4sAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [empID = _t, supID = _t]),
    emp = List.Buffer(Source[empID]),
    sup = List.Buffer(Source[supID]),
    Custom1 = Table.AddColumn(Source, "Hierarchy",
        each
            let
                fx = (empid as text, hierarchy as list) as list => 
                let
                    supervisor = sup{List.PositionOf(emp, empid)},
                    result = if supervisor="" then hierarchy else @fx(supervisor, hierarchy & {supervisor})
                in
                    result                
            in {[empID]} & fx([empID], {})
    ),
    #"Extracted Values" = Table.TransformColumns(Custom1, {"Hierarchy", each Text.Combine(List.Transform(_, Text.From), " | "), type text})
in
    #"Extracted Values"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Muchas gracias. la columna #all childer funciona pero es muy lenta.

amitchandak
Super User
Super User

@HoKe , Pruebe nuevas columnas como

Direct = var _1 = 'Table'[emploeeID] 
return COUNTX(FILTER('Table', 'Table'[supervisorID] =_1),'Table'[emploeeID])

Total = var _1 = 'Table'[emploeeID] 
return COUNTX(FILTER('Table', PATHCONTAINS('Table'[path],_1)),'Table'[emploeeID])-1

Medidas

Direct Count = var _1 = max('Table'[emploeeID] )
return COUNTX(FILTER(ALLSELECTED('Table'), 'Table'[supervisorID] =_1),'Table'[emploeeID])

Total measure = var _1 = max('Table'[emploeeID]) 
return COUNTX(FILTER(ALLSELECTED('Table'), PATHCONTAINS('Table'[path],_1)),'Table'[emploeeID])-1

Buscar archivo después de la firma

OwenAuger
Super User
Super User

Hola @HoKe

Así es como escribiría estas columnas calculadas, usando PATHCONTAINS para la columna [count of children].

Tenga en cuenta que devolverán en blanco en lugar de cero en el caso de que no haya hijos, pero podría agregar cero al final si lo desea.

¿Estos funcionan mejor para ti?

saludos

Owen

count of children direct =
CALCULATE (
    COUNTROWS ( 'Table' ),
    TREATAS ( { 'Table'[employeeID] }, 'Table'[supervisorID] ),
    ALL ( 'Table' )
)

count of children =
VAR CurrentEmployeeID = 'Table'[employeeID]
RETURN
    CALCULATE (
        COUNTROWS ( 'Table' ),
        PATHCONTAINS ( 'Table'[path], CurrentEmployeeID ),
        'Table'[employeeID] <> CurrentEmployeeID,
        ALL ( 'Table' )
    )


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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.