Forum Discussion
Calcualting Top 5 Customers
Hello All,
I have a questoin where I need to find the top 5 customers but with the categories as Tier1 and Tier2.
I have a table as below screenshot
Power Query:-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZQ7csMwDETvopozJkBSIOtcIZ3HZQq3vn8RfeBoyZWVyh6/pfSMhXS/T18yhUlu6aZRdf0acm7L5/L7I/xhiQe3WZgb8DwT13zwapl4EjhfC3Ad9ZqlFSti1GvxhINeLZX4oachNj5/6K1cgKdeT4Ok7fIJ8aG38KrMDXgR4qin0oijXi4ZeB71yn48I0a9OSlzo3I6juV6Ox3Hcr0d52Us18spiFHPy+m5UTkdx+l5Ox3H6Xk7zmcqdyvn+/nzUkyg4d4PR2ysiCLoWc6vgqp7UxCx864pwXVzhBqnCJfOKle2dbSlqVSypT9UabYf7nM52/q/bRtt90WjRPekb7vGERvXjSLdxo4r18iWVk7iqPspgr5UwDtjF7PzDBrT8N469I73zOMX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Begin of Week" = _t, Sales = _t, #"Customer Group" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Sales", Int64.Type}, {"Customer Group", type text}}),
#"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Begin of Week", type date}}, "en-ZA"),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type with Locale", "Tier1/2", each if [Customer Group] <> "Tier2" then "Tier 1" else "Tier 2")
in
#"Added Conditional Column"Where I have Tier1 and Tier2 customers that I have categorized as per user.
Now I am using a Area chart to show the top 5 customers as below.
I have filtered the top 5 customers by inbuilt power bi option under the filter's panel which filters the top 5 customers irresptive of the Tier1 or Tier2 which is expected.
Now the real ask here is,
By Default the area chart should show the top 5 customers which are from Tier1 only.
That means, Until user filters the Tier2 customers from slicer the area chart should show the top 5 customers from tier1 customers only.
If user selects Tier2 only then it should show the top 5 customers from tier2.
But there are situations where user can select SOME OF THE CUSTOMERS from Tier1 and Whole Tier2 and vice versa.
For example:-
If user selected, C3, C4 from Tier1 and selected Tier2 which by default selected all the tier2 customers,
Then in that case, top 5 customers in area chart should be, C3, C4 and then top3 from Tier2 customers.
Note:- As it is a sample data, I have jus included five customers under Tier1, but there are more than 20 Customers under tier1 and more than 50 Customers under Tier2.
Here I am giving the onedrive link for the pbix file.
https://1drv.ms/u/s!AmauTLNmHKexhGcs7UQE-SoJtnTZ
Request you to please suggest.
Thanks,
Mohan V.
- Anonymous4 years ago
Hi MohanV ,
I updated your sample pbix file(see attachment), please check if that is what you want.
1. Create a measure as below to judge if a customer is in the top five of the sales ranking
Flag = VAR _rank = RANKX ( ALLEXCEPT ( 'Table', 'Table'[Tier1/2] ), CALCULATE ( SUM ( 'Table'[Sales] ) ), , DESC, DENSE ) RETURN IF ( _rank <= 5, 1, 0 )2. Apply the visual level filter with the condition on the area chart
Best Regards
3 Replies
- amitchandak
Super User
MohanV , You can try TOPN
example
CALCULATE([measure],TOPN(10,allselected(Customer[Customer]),[measure],DESC),VALUES(Customer[Customer]))
It should give top 5 inside each tier if that is used in visual
else refer
https://www.sqlbi.com/articles/filtering-the-top-3-products-for-each-category-in-power-bi/
- MohanVFrequent Visitor
amitchandak thanks for your quick responce.
I tried this in my file.
But seems like its not working.
As i have selected, C1, C2, C3 from tier1, and Tier2 completely...
It should give me the Tier1 values first and then top2 from Tier2 as i already selected 3 customers from tier1
Please suggest
- AnonymousNot applicable
Hi MohanV ,
I updated your sample pbix file(see attachment), please check if that is what you want.
1. Create a measure as below to judge if a customer is in the top five of the sales ranking
Flag = VAR _rank = RANKX ( ALLEXCEPT ( 'Table', 'Table'[Tier1/2] ), CALCULATE ( SUM ( 'Table'[Sales] ) ), , DESC, DENSE ) RETURN IF ( _rank <= 5, 1, 0 )2. Apply the visual level filter with the condition on the area chart
Best Regards