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
MakLihp
Frequent Visitor

DAX measure to check if matching value exists in another column

Hi everybody,

 

I am working with a hierarchical dataset like the sample below in a dynamically filtered visual. As filters are applied to the visual, I want only the children that have an (indirect) hierarchical connection to a top level parent to be shown.

To check whether each ParentId matches with a ChildId, I currently use the following measure:

 

Current measure = 
var parentid = VALUES(Sheet1[ParentId])

//Defines logic for evaluating if child is matched to parent or not:
var matches = 
    IF(
        CALCULATE(
            COUNTROWS(Sheet1),
            ALLSELECTED(),
            TREATAS(parentid,Sheet1[ChildId])
        ),BLANK(),1
    )

return
//Perform logic for each row in table, except if the hierarchy level equals top:
SUMX(FILTER(Sheet1, Sheet1[HierarchyLevel]<>"Top"),matches)

 

 

It almost works. The problem is children such as ChildId 8, which have a parent (5) but this parent is not connected to the top level. See the difference between Current result and Expected result below:

 

ChildIdParentIdHierarchyLevelCurrent resultExpected result
1 top  
2 top  
31medium  
42medium  
511medium11
63low  
74low  
85low 1
912low11

 

Intuitively, I think the easiest solution would be to just have the measure run the same logic twice: first (1) checking if there is a matching ChildId in whole table (as is currently done), second (2) checking if there is a matching ChildId in a table filtered by the results of the first check. 
However, I cannot seem to get this working, nor do I know if it is the right solution at all.

Does anyone know a solution? Any help would be greatly appreciated!

3 REPLIES 3
MakLihp
Frequent Visitor

Will just post this here again to see if anyone knows how to get the expected result using a DAX measure:

 

ChildId ParentId HierarchyLevel Current result Expected result
1 top  
2 top  
31medium  
42medium  
511medium11
63low  
74low  
85low 1
912low11

 

Thanks in advance!

Greg_Deckler
Super User
Super User

@MakLihp Can you not just use PATHCONTAINS?

PATHCONTAINS function (DAX) - DAX | Microsoft Docs


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thanks for your reply! Maybe PATHCONTAINS works, but then I still wouln't know how to get the Expected result instead of Current result as described in my post. Any ideas?

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.