Hello @ronald_balza,
Thank You for posting your query onto the Forum.
Firstly, the formulas that you’ve provided onto the post as well as the scenario that you’ve highlighted was observed nowhere into the file.
Apart from that, coming to the solution now, you had created “Many-to-Many” bi-directional relationship into the model and that too between the Dimension tables i.e. between “Customer” table and “GropedFinalisedInvoices” table. Below is the screenshot provided for the reference -
Rather I created a relationship “One-to-Many” relationship between the Fact and Dimension Tables i.e. between “FactFinalisedInvoicesDetailed” table and “GropedFinalisedInvoices” table based on the Code field which is common between the two tables. Below is the screenshot provided of the revised relationship of the data model -
Also it’s observed that you’re dragging “Group Owner” field from the Dimension Table and “Invoice Type” from the Fact Table in order to create a hierarchy. My question is - “How can data from the Fact Table flow up to the Dimension Table?”
So now, since you’re trying to create a hierarchial grouping based on Ranking technique and evaulate the “Total Invoices” results based on that. This technique is complex to achieve -
Firstly, you’ll be required to break ties since there’re multiple invoices with the same amount. So to break the ties below is the formula provided for the reference -
Alphabetical Rank - Owner =
VAR _Current_Owner = SELECTEDVALUE( FactFinalisedInvoicesDetailed[Name] )
VAR _Result =
COUNTROWS(
FILTER( ALL( FactFinalisedInvoicesDetailed[Name] ) ,
FactFinalisedInvoicesDetailed[Name] <= _Current_Owner ) )
RETURN
_Result
Once you break the ties, you’ll be required to calculate the ranking by adding the context of the “Invoice Type” within the Ranking measure since your next hierarchy after the “Group Owner” is “Invoice Type”. Below is the formula of the ranking provided for the reference -
Ranking - Owner =
VAR _Ranking =
CALCULATE(
RANKX(
ALL( FactFinalisedInvoicesDetailed[Name] ) ,
[Total Invoice] + DIVIDE( [Alphabetical Rank - Owner] , 100 ) ,
,
DESC ) ,
ALLSELECTED( FactFinalisedInvoicesDetailed[Invoice Type] ) )
RETURN
IF( ISBLANK( [Total Invoice] ) ,
BLANK() ,
_Ranking )
And now, once the ties are broken and context of the hierarchy is added into the Ranking Technique than you’ll be able to group them using the “Grouping and Segmentation Technique”. Below is the formula provided for the reference -
TOPN - 1 =
VAR _TOPN =
CALCULATE( [Total Invoice] ,
FILTER( VALUES( FactFinalisedInvoicesDetailed[Name] ) ,
COUNTROWS(
FILTER( 'Customer Rank' ,
[Ranking - Owner] >= 'Customer Rank'[Min Rank] &&
[Ranking - Owner] <= 'Customer Rank'[Max Rank] ) ) > 0 ) )
RETURN
IF( ISBLANK( [Total Invoice] ) ,
BLANK() ,
_TOPN )
Now, what you did was firstly, you didn’t broke the ties than you creating the ranking directly by “Invoice Type” which is incorrect. Because your Invoice Type is the secondary hierarchy, the first hierarchy is set for “Group Owner”. So without considering the context of the “Group Owner” hierarchy you went directly for the “Invoice Type” and lastly you created a grouping formula by considering the “Invoice Type” again whereas it should’ve been “Group Owner”. Below is the formula provided which you’ve written -
Total Invoice By Rank Group =
SUMX(
KEEPFILTERS(
FILTER(
ALLSELECTED( FactFinalisedInvoicesDetailed[Invoice Type] ),
COUNTROWS(
FILTER(
'Customer Rank',
[Rank by Invoice Type] >= 'Customer Rank'[Min Rank] &&
[Rank by Invoice Type] <= 'Customer Rank'[Max Rank] )
) > 0
)
), [Total Invoice]
)
Rather since you want to use this pattern of formula i.e. SUMX rather than CALCULATE than the formula will be like this (Note: But first you’ll have to create the previous formulas like I’ve mentioned. Don’t use this formula directly) -
TOPN - 2 =
VAR _TOPN =
SUMX(
FILTER( VALUES( FactFinalisedInvoicesDetailed[Name] ) ,
COUNTROWS(
FILTER( 'Customer Rank' ,
[Ranking - Owner] >= 'Customer Rank'[Min Rank] &&
[Ranking - Owner] <= 'Customer Rank'[Max Rank] ) ) > 0 ) , [Total Invoice] )
RETURN
IF( ISBLANK( [Total Invoice] ) ,
BLANK() ,
_TOPN )
Now, see the results. Below is the screenshot of the final output provided for the reference -
Lastly, if you see the totals of your results and compare it with mine. You’ll observe that your formula considered the range of Group Owner even after 700 whereas my formula will stop at the range of 700.
I’m also attaching the working of the PBIX file for the reference. And if you want to cross-verify my results you can go to the “Cross-Verification” worksheet for that as well.
Important Note: The sorting is possible either for the Grouping or for the Numbers. Both are not possible at the same time.
Hoping you find this useful and meets your requirements that you’ve been looking for.
Thanks and Warm Regards,
Harsh
Hierarchial Grouping Ranking - Harsh.pbix (376.0 KB)