Forum Discussion
Calculation using PATH function Hierarchy
Hi mail2vjj
Please try this MEASURE
Commission1 =
VAR myparent =
SELECTEDVALUE ( Sales[Name] )
VAR Children_ =
ADDCOLUMNS (
CALCULATETABLE (
VALUES ( 'Hierarchy'[Name] ),
FILTER ( ALL ( 'Hierarchy' ), 'Hierarchy'[Parent] = myparent )
),
"MySales", CALCULATE (
SUM ( Sales[Sales] ),
FILTER ( ALL ( Sales ), Sales[Name] = EARLIER ( 'Hierarchy'[Name] ) )
)
)
RETURN
SUMX ( Children_, [MySales] ) * .05
+ SUM ( Sales[Sales] ) * .1
I am not sure if above is the best way to do it...
But atleast it works with your sample data:smileywink:
- Zubair_Muhammad8 years ago
Community Champion
To get Commission 2, you can try this MEASURE
Commission_2 = VAR myparent = SELECTEDVALUE ( Sales[Name] ) VAR DC = ADDCOLUMNS ( ADDCOLUMNS ( FILTER ( ALL ( 'Hierarchy' ), PATHCONTAINS ( 'Hierarchy'[Name Path], myparent ) ), "ParentPosition", FIND ( myparent, SUBSTITUTE ( 'Hierarchy'[Name Path], "|", "" ), 1, 0 ) ), "Direct Child", MID ( SUBSTITUTE ( 'Hierarchy'[Name Path], "|", "" ), [ParentPosition] + 1, 1 ) ) VAR IDC = ADDCOLUMNS ( ADDCOLUMNS ( DC, "IndirectChild1", MID ( SUBSTITUTE ( 'Hierarchy'[Name Path], "|", "" ), [ParentPosition] + 2, 1 ) ), "IndirectChild2", MID ( SUBSTITUTE ( 'Hierarchy'[Name Path], "|", "" ), [ParentPosition] + 3, 1 ) ) VAR DirectChildren = ADDCOLUMNS ( DISTINCT ( SELECTCOLUMNS ( DC, "DC", [Direct Child] ) ), "Mysales", CALCULATE ( SUM ( Sales[Sales] ), FILTER ( ALL ( Sales ), Sales[Name] = [DC] ) ) ) VAR IndirectChildren = ADDCOLUMNS ( DISTINCT ( UNION ( SELECTCOLUMNS ( IDC, "IDC", [IndirectChild1] ), SELECTCOLUMNS ( IDC, "IDC", [IndirectChild2] ) ) ), "Mysales", CALCULATE ( SUM ( Sales[Sales] ), FILTER ( ALL ( Sales ), Sales[Name] = [IDC] ) ) ) RETURN SUMX ( IndirectChildren, [Mysales] ) * .01 + SUMX ( directChildren, [Mysales] ) * .05 + SUM ( Sales[Sales] ) * .1- Zubair_Muhammad8 years ago
Community Champion
- mail2vjj8 years ago
Helper III
Hi Zubair_Muhammad,
First of all a huge thanks for taking the pain yet again to write that mammoth code.
Your code is working but only to a certain extent.
As you can see in both Commission 1 & 2, the calculation of the commission is correct but at the grand total level, there is a mistake.
This is what the totals should be and the code shows, Commission_1 = 58.5 and Commission_2 = 57, which are not correct.
Name Sales Commission_1 Commission_2 A 30 10 14 B 60 12.5 13.9 C 80 14.5 14.5 D 80 15 15 E 50 5 5 F 80 8 8 G 50 5 5 H 60 6 6 I 80 8 8 Total 570 84 89.4 Also, I am trying to use a Hierarchy slicer on the table and the measures that you sent and it is showing me number is places, that it shouldn't.
In the following picture, when I select the B hierarchy, it is still showing me 1.50 for A, C, F & G.
Again, after selecting the B hierarchy, the grand total is wrong, For commission 1 it should be 46.5 (12.5+15+5+6+8) and commission 2 should be 47.9 (13.9+15+5+6+8).
But as you can see in the following picture, the totals are wrong again and it is still showing me values for other names even after the slicer is selected.
So can you please help fixing this and meanwhile if I come up with something, I will post about it.
I am updating the files in OneDrive as well.
https://1drv.ms/f/s!Ap0qSKP-4qpThCc1sSXanmibgIff
Again, thank you for taking the effort to write all that code.
Vishesh Jain
- Zubair_Muhammad8 years ago
Community Champion
The problem of incorrect totals can be resolved using these additional MEASURE
Com1 = IF ( HASONEFILTER ( Sales[Name] ), [Commission_1], SUMX ( ALLSELECTED ( Sales[Name] ), [Commission_1] ) )Com2 = IF ( HASONEFILTER ( Sales[Name] ), [Commission_2], SUMX ( ALLSELECTED ( Sales[Name] ), [Commission_2 ) )