Forum Discussion
Calculation using PATH function Hierarchy
Hello everyone,
I am trying to do a calculation based on a hierarchy.
I have a table with Names and their Parents and I have created a hierarchy using the PATH function.
I have sales data in another table and from that data based on the hierarchy I am trying to calculate the commission.
This is the picture of my hierarchy:
These are all the Paths that I have created and the link to the files is at the end of the message.
This is my Sales Table:
| Date | Name | Sales |
| 01-01-18 | A | 10 |
| 01-01-18 | B | 20 |
| 01-01-18 | C | 50 |
| 01-01-18 | D | 30 |
| 01-01-18 | E | 40 |
| 01-01-18 | F | 60 |
| 01-01-18 | G | 20 |
| 01-01-18 | H | 30 |
| 01-01-18 | I | 60 |
| 02-01-18 | A | 20 |
| 02-01-18 | B | 40 |
| 02-01-18 | C | 30 |
| 02-01-18 | D | 50 |
| 02-01-18 | E | 10 |
| 02-01-18 | F | 20 |
| 02-01-18 | G | 30 |
| 02-01-18 | H | 30 |
| 02-01-18 | I | 20 |
This is my Total Sales Table along with the Desired Commission results:
| 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 | 11 |
| E | 50 | 5 | 5 |
| F | 80 | 8 | 8 |
| G | 50 | 5 | 5 |
| H | 60 | 6 | 6 |
| I | 80 | 8 | 8 |
I have 2 ways to calculate commission.
First:
Commission on own sale (10%) + Commission for every sub-branch of the hierarchy (5%).
For eg. For B, own sale is 60 and sub-branch sales for D and E are 80 & 50 respectively, so total is 130.
So Commission for B is (60*10%) + (130*5%) = 12.5
For eg. For D, own sale is 80 and sub-branch sales for H and I are 60 & 80 respectively, so total is 140.
So Commission for B is (80*10%) + (140*5%) = 15
Second:
Commission on own sale (10%) + Commission from every sub-branch (5%) and sub-sub branches of the hierarchy (1%).
For Eg. For A, own sale is 30, sub-branch sales for B and C are 140 and all other sub-sub-branches (D,E,F,G,H,I) total is 400
So Commission for A is (30*10%) + (140*5%) + (400*1%) = 14
Similarly Commission for B, Own Sale is 60, sub-branch sales for D and E are 80 & 50 respectively and sub-sub-branch sale for H and I are 60 and 80 respectively.
So Commission for B is (60*10%) + (130*5%) + (140*1%) = 13.9
Ths following is the link to my Excel file and the PBIX file.
https://1drv.ms/f/s!Ap0qSKP-4qpThCc1sSXanmibgIff
It would be great if anyone can help me with this.
Thank you,
Vishesh Jain
30 Replies
- mail2vjj
Helper III
Hi everyone,
I am trying to come up with a DAX code for my problem and this is where I have reached so far.
Thinking of my problem in DAX terms, for calculation of the commission 2 conditions need to be satisfied.
1. The Path should contain the name of the parent for which we are calculating the commission.
2. The Hierarchy Depth of the parent should be less than the child, on the basis of which the commission percentage will be decided and the commission will be calculated.
This is the code that I have come up with so far, and obviously it is not working, because for some reason I do not understand, the EARLIER function is not working.
Commission 2 = IF(
AND(
CONTAINS(Sales, Sales[Name_Path], (Sales[Name])),
Sales[Hierarchy_Depth] < Sales[Hierarchy_Depth]), Sales[Sales]*0.05, 0)
If someone can explain to where am I going wrong or what can be done to get it right, please do let me know.
Any help is appreciated.Thank you,
Vishesh Jain
- Zubair_Muhammad
Community Champion
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- Zubair_Muhammad
Community Champion
I am not sure if above is the best way to do it...
But atleast it works with your sample data:smileywink: