The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi, I am looking to write a DAX measure to find the vendors with the top 5 sales, and to label the other vendors as "Others" as I want to avoid showing too many different vendor names when I build the visual. This is my thought process:
With this thought process, I write the following DAX expression but it did not work:
Vendor Name | Order No. | Value |
Vendor 1 | 1 | 10 |
Vendor 1 | 2 | 15 |
Vendor 2 | 3 | 15 |
Vendor 2 | 4 | 15 |
Vendor 3 | 5 | 25 |
Vendor 4 | 6 | 5 |
Vendor 5 | 7 | 30 |
Vendor 6 | 8 | 30 |
Vendor 7 | 9 | 10 |
And I am looking to get something like this:
Output Column/Measure | Total Value |
Vendor 1 | 25 |
Vendor 2 | 30 |
Vendor 3 | 25 |
Vendor 5 | 30 |
Vendor 6 | 30 |
Others | 15 |
Appreciate if you can provide me with some advice or guidance.
Edit: Link to sample file - https://we.tl/t-NCWML5aA4X
Hi @KongZY ,
To achieve the result you need to:
1. Create a separate table with all vendors + 'Others'. One of the ways is to do it in Power Query
let
Source = t,
#"Removed Other Columns" = Table.SelectColumns(Source,{"Vendor Name"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
Custom1 = Table.InsertRows(#"Removed Duplicates", 0, {[Vendor Name="Others"]})
in
Custom1
2. Create measures:
total value = SUM(t[Value])
vendor_rank = RANKX ( ALL ( vendors[Vendor Name] ), [total value],, DESC )
custom_value =
VAR others = FILTER ( ALL ( vendors[Vendor Name] ), [vendor_rank] > 5 )
VAR others_value = SUMX ( others, [total value] )
RETURN
IF (
[vendor_rank] <= 5,
[total value],
IF ( SELECTEDVALUE ( vendors[Vendor Name] ) = "Others", others_value )
)
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.
Check out my latest demo report in the data story gallery.
Stand with Ukraine!
Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/
Thank you!
VendorRanked = // Try this one might help you
VAR VendorSales =
SUMMARIZE(
'PurchaseOrders',
'PurchaseOrders'[Vendor Name],
"TotalSales", SUM('PurchaseOrders'[Value])
)
RETURN
IF(
RANKX(VendorSales, [TotalSales], , DESC) <= 5,
'PurchaseOrders'[Vendor Name],
"Others"
)
Did I answer your question? If so, please mark my post as a solution!
Proud to be a Super User!
Hi Muhammad, thank you for your response. Can I check with you is your solution meant to be a measure or a column?
I have the following issue when I run it as a measure:
And this issue when I run it as a calculated column:
//Try this one
VendorRanked =
VAR VendorSales =
SUMMARIZE(
'PurchaseOrders',
'PurchaseOrders'[Vendor Name],
"TotalSales", SUM('PurchaseOrders'[Value])
)
VAR CurrentVendor = VALUES('PurchaseOrders'[Vendor Name])
RETURN
IF(
RANKX(VendorSales, [TotalSales], , DESC) <= 5,
CurrentVendor,
"Others"
)
Did I answer your question? If so, please mark my post as a solution!
Proud to be a Super User!
Hi Muhammad, thank you for your help. I played around a bit and managed to get it to work using
FIRSTNONBLANK('PurchaseOrders'[Vendor Name], [Vendor Name])
inside the RETURN function. I suspect is because the measure would take a column as a return value.