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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
imyashy
Frequent Visitor

Count all hierarchy nodes including sum of items based on parent

I have created hierarchy structure like below and I need to create a visualisation table that displays

1) list of parent nodes

2) total count of all nodes in the hierarchy for the parent (count should include the parent node too).

3) sum of other elements belonging to the child nodes (i.e. total users etc).

 

Any help would be greatly appreciated.

 

Expected result:

 

Parent NodeTotal nodes Total Users

PA

12120
PB649
PC521
PD322
PF312

 

Diagram:

            PA
         /        \
       PB          PC
     /  |  \         /  \
  D  PD  E     G    PF
       /\                 /\
      I   J              K   L

 

Table structure:

 

childorg        Total usersparentorg  path   length  
PA50 1  1
PB5PAPA|PB2
PC7PAPA|PC2
D10PBPA|PB|D3
E12PBPA|PB|E3
PD1PBPA|PB|PD3
G2PCPA|PC|G3
PF3PCPA|PC|PF3
I6PDPA|PB|PD|I4
J15PDPA|PB|PD|J4
K1PFPA|PC|PF|K4
L8PFPA|PC|PF|L     4
1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @imyashy 

 

Ad a new path column where 1 is replaced by the main parent

danextian_0-1752993019979.png

Create the following measures:

User count = 
VAR _parentOrg =
    SELECTEDVALUE ( tbl[parentorg] )
RETURN
    SUMX (
        FILTER (
            SUMMARIZECOLUMNS (
                tbl[path2],
                REMOVEFILTERS (),
                "@users", CALCULATE ( SUM ( tbl[Total users] ) )
            ),
            CONTAINSSTRING ( [Path2], _parentOrg )
        ),
        [@users]
    )

---------------------------------------------------------------------------

Node count = 
VAR _parentOrg =
    SELECTEDVALUE ( tbl[parentorg] )
RETURN
    COUNTROWS (
        (
            FILTER (
                SUMMARIZECOLUMNS (
                    tbl[path2],
                    REMOVEFILTERS (),
                    "@users", CALCULATE ( SUM ( tbl[Total users] ) )
                ),
                CONTAINSSTRING ( [Path2], _parentOrg )
            )
        )
    )

danextian_1-1752993079349.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

12 REPLIES 12
imyashy
Frequent Visitor

Hi

 

I have managed to solve the problem by creating a new table and then using the following DAX measures:

total orgs in hierarchy = 
CALCULATE(
    COUNTROWS(orgs_levels),
    FILTER(
        orgs_levels,
        PATHCONTAINS(orgs_levels[Path], [hosting orgs])
            && orgs_levels[total users] <> 0
    )
) + 0

total users in hierarchy = 
CALCULATE(
    SUM(orgs_levels[total users]),
    FILTER(
        orgs_levels,
        PATHCONTAINS(orgs_levels[Path],[hosting orgs])
    )
)+0


For the second scenario (count parent and child orgs where category is not hosted) I created 2 seperate tables and then a 3rd via a Union (joining table 1 and 2).

 





imyashy
Frequent Visitor

@lbendlin Apologies - I was preparing the above post late last night and realised a slight confusion between parentorg and hosting org.

I have corrected the above post including the pbix file and updated the wording regarding the logic for the expected outcome. The zip file contains:
- pbix file
- source data
- expected outcome

lbendlin
Super User
Super User

lbendlin_0-1753041756468.png

 

@Ibendlin, Thank you - That works. Can you provide the measure for the non-host user count please?

 

Again, The measure works fine with a small list of parents however I have a total of 732 parents to run the measure on which results in a insufficient resources error..

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

@lbendlin 
No problem. Here is a link to a zip file containing the data, pbix file and example of expected outcome. I've provided as much detail as possible and tried to remain concise.

 

Many thanks for helping with this. The goal here is for the totals to appear for 731 1404 hosting orgs without facing a insufficient resources error (for reference I am using PowerBI on a laptop).

danextian
Super User
Super User

Hi @imyashy 

 

Ad a new path column where 1 is replaced by the main parent

danextian_0-1752993019979.png

Create the following measures:

User count = 
VAR _parentOrg =
    SELECTEDVALUE ( tbl[parentorg] )
RETURN
    SUMX (
        FILTER (
            SUMMARIZECOLUMNS (
                tbl[path2],
                REMOVEFILTERS (),
                "@users", CALCULATE ( SUM ( tbl[Total users] ) )
            ),
            CONTAINSSTRING ( [Path2], _parentOrg )
        ),
        [@users]
    )

---------------------------------------------------------------------------

Node count = 
VAR _parentOrg =
    SELECTEDVALUE ( tbl[parentorg] )
RETURN
    COUNTROWS (
        (
            FILTER (
                SUMMARIZECOLUMNS (
                    tbl[path2],
                    REMOVEFILTERS (),
                    "@users", CALCULATE ( SUM ( tbl[Total users] ) )
                ),
                CONTAINSSTRING ( [Path2], _parentOrg )
            )
        )
    )

danextian_1-1752993079349.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

CONTAINSSTRING would need to include the pipe symbol to avoid mismapping, for example if you had nodes PC and PCB.

 

CONTAINSSTRING("|" & [path2] & "|","|" & [_parentorg] & "|")

 

Better use PATHCONTAINS.

@lbendlin 

Good point. @danextian's Code works well with a small dataset however if there there is a child node which shares the same string of a parent node, it is included in the counts. i.e. Parent Node = PB, Child node = PBLPU. I updated the code with your recommendation which solved that issue.

How would the code be adapted to use PATHCONTAINS? If I replace CONTAINSSTRING with PATHCONTAINS the counts don't return any values.

 

As mentioned earlier, the code works well with a small dataset however I have to run the measures for 732 Parent nodes which result in an insufficient resources error (using my laptop). If I filter on approx 20 parent nodes, results are successfully returned. 

 

Can the code in the measures be optimised to use less resources?

 

 

 

As per testing, PATHCONTAINS is faster than CONTAINSSTRING. Using the same  pbix with parent not related to org_levels, try these:

User Count - PATHCONTAINS = 
VAR _parentOrg =
    SELECTEDVALUE ( Parents[parentorg] )
RETURN
    CALCULATE (
        SUM ( orgs_levels[total users] ),
        PATHCONTAINS ( orgs_levels[Path], _parentOrg )
    )


Record Count - PATHCONTAINS = 
VAR _parentOrg =
    SELECTEDVALUE ( Parents[parentorg] )
RETURN
    CALCULATE (
        COUNTROWS ( orgs_levels ),
        PATHCONTAINS ( orgs_levels[Path], _parentOrg )
    )

 

Faster but still not too fast. The only thing I can think of to make the calculations a lot faster is by precomputing a the value for each  path item - having a separate row for each path item.

PathItems = 
VAR _parents =
    DISTINCT ( orgs_levels[parentorg] )
RETURN
    FILTER (
        GENERATE (
            orgs_levels,
            VAR maxLen = orgs_levels[pathlength]
            RETURN
                ADDCOLUMNS (
                    GENERATESERIES ( 1, maxLen, 1 ),
                    "PathItem", PATHITEM ( orgs_levels[Path], [Value], TEXT )
                )
        ),
        [PathItem] IN _parents
    )

danextian_0-1753186555111.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

I tried the updated DAX formulas with PATHCONTAINS. It works but not for all 1404 values.

 

What DAX formula are you using to create the 'value' column? 

 

When I try to create the PathItems column I receive the following error 'the expression refers to multiple columns. Multiple columns cannot be convered to a scalar value'

Pre-computing is done with either a column or a table. That formula is meant to create a calculated table.

GENERATESERIES creates a series of numbers with a default  column name called Value.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors