cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
sandeepklce250 Frequent Visitor
Frequent Visitor

DAX - Measure to get Count of child Id's for a given Parent

Hello Team,

 

We have a DB table with following fields, that have Parent-Child hierarchy. 

Capture.PNGSource TableOur reporting requirement is to come up with following tabular visualization, to display only parent Id Categories (C1,C2,C3) with count of child_id’s associated to my Parent. Something as follows:  

2.PNGFinal Output

So, when I select Id -1, the measure should provide the count of child_id’s for it, which is 2 in this case.

I’m SQL guy and very new to DAX, this can be done using a self-join for Id to ParentId in SQL.

Can anyone please provide a DAX expression for the same?

 

Thanks,
Sundeep

1 ACCEPTED SOLUTION

Accepted Solutions
Super User III
Super User III

Re: DAX - Measure to get Count of child Id's for a given Parent

hi @sandeepklce250 

 

Try this method

 

First a calculated column

 

PATH =
PATHITEM ( PATH ( TableName[Id], TableName[ParentId] ), 1, INTEGER )
Now this MEASURE
 
No. of Children =
VAR myvalue =
    SELECTEDVALUE ( TableName[Id] )
VAR mycategory =
    SELECTEDVALUE ( TableName[Category] )
RETURN
    CALCULATE (
        COUNT ( TableName[PATH] ),
        TableName[PATH] = myvalue,
        TableName[Category] <> mycategory,
        ALL ( TableName )
    )
Try my new Power BI game Cross the River

View solution in original post

3 REPLIES 3
Super User III
Super User III

Re: DAX - Measure to get Count of child Id's for a given Parent

hi @sandeepklce250 

 

Try this method

 

First a calculated column

 

PATH =
PATHITEM ( PATH ( TableName[Id], TableName[ParentId] ), 1, INTEGER )
Now this MEASURE
 
No. of Children =
VAR myvalue =
    SELECTEDVALUE ( TableName[Id] )
VAR mycategory =
    SELECTEDVALUE ( TableName[Category] )
RETURN
    CALCULATE (
        COUNT ( TableName[PATH] ),
        TableName[PATH] = myvalue,
        TableName[Category] <> mycategory,
        ALL ( TableName )
    )
Try my new Power BI game Cross the River

View solution in original post

Super User III
Super User III

Re: DAX - Measure to get Count of child Id's for a given Parent

@sandeepklce250 

 

Please see attached file as well with your sample data and results

 

Try my new Power BI game Cross the River
sandeepklce250 Frequent Visitor
Frequent Visitor

Re: DAX - Measure to get Count of child Id's for a given Parent

Thank you Zubair, this worked. Thanks a bunch for attaching the sample report as well.

Helpful resources

Announcements
Exclusive LIVE Community Event #3 – Ask Arun Anything

Exclusive LIVE Community Event #3 – Ask Arun Anything

Join us in the third Triple A event!

Meet the 2020 Season 1 Power BI Super Users!

Meet the 2020 Season 1 Power BI Super Users!

It’s the start of a new Super User season! Learn all about the new Super Users and brand-new tiered recognition system.

Super User Challenge: Can You Solve These?

Super User Challenge: Can You Solve These?

We're celebrating the start of the New Super User season with our first ever Super User 'Can You Solve These?' challenge.

January 2020 Community Highlights

January 2020 Community Highlights

Make sure you didn't miss any of the things that happened in the community in January!

Top Solution Authors
Top Kudoed Authors