cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
cmncp
Helper III
Helper III

DAX to display 2 levels of Hierarchy based on filter

I have a hierarchy table that looks like this:

2020-03-03_14-05-41.png

 

 

 

 

 

 

 

 

And a fact table like this:

2020-03-03_14-07-01.png

And a simple model:

2020-03-03_14-09-15.png

I have created a report with a Hierarchy Slicer and a Matrix as shown below.  But I also need to be able to display the table that is shown on the right.  In the sample shown, the user has selected "Node A.A" in the slicer, and the table needs to display the selected node ("Node A.A"), plus the 2 children ("Node A.A.A" and "Node A.A.B"), aggregating the data for them.  Is there a dax formula I can write that I can then use to filter this table?  Just to clarify, the requiement is to display the selected/filtered node and all children that are one level below.

2020-03-03_14-14-23.png

 

Note, real model is build using SSAS Tabular 2017, so some functions are not available (such as IsInScope).

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Here's the link. Take a look at how I create the LxToLx tables and how the Parent Level and Child Level are used in the measure.

https://1drv.ms/u/s!ApyQEauTSLtOgYJQZeQffdQS0PwRTg?e=XABEbl

Best
D

View solution in original post

11 REPLIES 11
az38
Community Champion
Community Champion

@cmncp 

cannot you just remove Level3 and Level4 from hierarchy in visual fields pane


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Hi @az38 No, because I need to display the selected node and all children.  So in the example, the user selected Node A.A and therefore we display that node plus its immediate children (Node A.A.A and Node A.A.B).  If the choses Node A.A.B, then we need to display Node A.A.B and its immediate children (Node A.A.B.A and node A.A.B.B).  It is all relative to the selection the user makes in the slicer.

Anonymous
Not applicable

The best way to achieve this is by creating another table that will store 2 columns:
- a column with each node
- a column with each subnode that corresponds to the node in the first column plus the node from the first column. The table should be connected to Node Name in the first table (dimension with all the levels). The filtering would be 1:* from the hierarchy dimension to the said table. You would display the figures only when one node would be selected from the hierarchy dimension. A visual displays nothing if the column with a measure has blanks - use this feature to your advantage.

 

Best
D

Hi @Anonymous Thanks for the suggestion, but I can't quite get it to work.  I have added the table like this:2020-03-04_11-19-09.png

 

And created relationship back to hierarchy table based on Children to NodeName:

2020-03-04_11-19-27.png

 

But when I build my report and select the top "Node A" in my slicer, I still get all levels showing.  How do I restrict the table to just 2 levels?

2020-03-04_11-19-49.png

 

 I have uploaded my PBIX file here: https://drive.google.com/open?id=13oxTuna3j9EYzvH7lkJb1dExq3k3U6oH 

 

Anonymous
Not applicable

Once you've got the correct setup, you'll write something like this:

// Say [Total Sales] works OK for any node.
//
// Relationships:
// Hierarchy[Node Name] 1 -> * Sales[Node Name]
// Hierarchy[Node Name] 1 -> * AllLevels[Parent]
//
// The measure that'll work with AllLevels:

[Node Sales] =
var __oneNodeVisible = HASONEVALUE( AllLevels[Parent] )
var __oneSubnodeVisible = HASONEVALUE( AllLevels[Children] )
var __shouldCalc =
	__oneNodeVisible && __oneSubnodeVisible
var __subnode = SELECTEDVALUE( AllLevels[Children] )
var __result = 
	CALCULATE(
		[Total Sales],
		Hierarchy[Node Name] = __subnode,
		ALL( Hierarchy )
	)
return
	if( __shouldCalc, __result )

Or something similar... You might need to discover which level the node belongs to and then make changes to __result accordingly.

 

Best

D

Anonymous
Not applicable

Or something like this:

// Say [Total Sales] works OK for any node.
//
// Relationships:
// Hierarchy[Node Name] 1 -> * Sales[Node Name]
// Hierarchy[Node Name] 1 -> * AllLevels[Parent]
//
// The measure that'll work with AllLevels:

[Node Sales] =
var __oneNodeVisible = HASONEVALUE( AllLevels[Parent] )
var __oneSubnodeVisible = HASONEVALUE( AllLevels[Children] )
var __shouldCalc =
	__oneNodeVisible && __oneSubnodeVisible
var __subnode = SELECTEDVALUE( AllLevels[Children] )
var __level = SELECTEDVALUE( AllLevels[Level] )
var __level1Result =
	CALCULATE(
		[Total Sales],
		Hierarchy[Level1] = __subnode,
		ALL( Hierarchy )
	)
var __level2Result =
	CALCULATE(
		[Total Sales],
		Hierarchy[Level2] = __subnode,
		ALL( Hierarchy )
	)
var __level3Result =
	CALCULATE(
		[Total Sales],
		Hierarchy[Level3] = __subnode,
		ALL( Hierarchy )
	)
var __level4Result =
	CALCULATE(
		[Total Sales],
		Hierarchy[Level4] = __subnode,
		ALL( Hierarchy )
	)
var __result = 
	switch( __level,
		1, __level1Result,
		2, __level2Result,
		3, __level3Result
		4, __level4Result,
		"This should not happen"
	)
return
	if( __shouldCalc, __result )

 

Best

D

Anonymous
Not applicable

Also, for this to work, the level in AllLevels must be the level of the child, not of the parent.

Best
D
Anonymous
Not applicable

I got it to work but I had to change the model. I've got the file on my OneDrive and I'll share as soon as possible.

Best
D
Anonymous
Not applicable

Here's the link. Take a look at how I create the LxToLx tables and how the Parent Level and Child Level are used in the measure.

https://1drv.ms/u/s!ApyQEauTSLtOgYJQZeQffdQS0PwRTg?e=XABEbl

Best
D

@AnonymousThat's awesome work.  Let me have a play around with it today.  Thanks for going to all that effort.

Anonymous
Not applicable

Hi there. It's the Parent that has to be connected to Node Name, not the children. Once this is done, then selecting a single node in Hierarchy will give you in AllLevels the children of the node and the node itself (in the Children column). You also have to have a measure that will work with AllLevels in such a way that it'll return the values for the visible Children when only one node is selected in Hierarchy. All the other rows will receive BLANK. This way your table created from Children and the measure will show what you want.

 

Bear in mind, though, that the measure you'll be creating to achieve this will have to rely on virtual relationships and TREATAS is your friend. If you can't use TREATAS, you'll  be relying on INTERSECT. The filtering should be from Hierarchy to AllLevels in a 1:* fashion (no cross-filtering as you don't need this at all and it would be confusing to say the least).

 

I'm at work and can't therefore open or even download files from a Google Drive. Could you please place the file somewhere else? Maybe OneDrive?

Best
D

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors