Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
fabienrolland
Frequent Visitor

nb of tickets within each product category where category has the highest revenue in the ticket

Hello, I have a sales table which could look like this

 

ticket idproduct iddatecategorysubcategoryRevenue
1product101-01-2019category1subcategory110
1product202-01-2019category1subcategory15
1product303-01-2019category1subcategory25
2product404-01-2019category1subcategory320
2product505-01-2019category1subcategory310
2product606-01-2019category2subcategory450
3product707-01-2019category2subcategory55
3product808-01-2019category2subcategory610
3product909-01-2019category2subcategory710

 

I want to be able to build a table with category as a dimension and calculate a measure that would give for each category the nb of (distinct) tickets where the category has the highest total revenue in the ticket.

 

Let me give an example with the data from the above table.

here you see for each ticket which category has the highest total revenue.

ticketcategorytotal revenue in ticketcategory has highest revenue in ticket?
1category120Yes
1category20No
2category130No
2category250Yes
3category10No
3category225Yes

 

Based on that i obtain the table below (which is the one i'm looking for) giving the nb and % of tickets where the category has the highest total revenue.

categorynb of tickets where category has highest revenue in ticket%
category1133%
category2267%
Total3100%

 

Not trivial !

And to add a little more complexity, i would like to same measure to calculate either by category or subcategory depending on which dimension i use in the table.

 

Thanks for your help

2 ACCEPTED SOLUTIONS


@fabienrolland wrote:

What i'd like is a measure that can calculate what you did but depending if i'm at the category level, it calculates the top n by category, and if i'm at the subcategory level it calculates topn by subcategory.


Is it possible to create a single dynamic measure like that, that can be used in a product hierarchy?

 


To do this you would have to test if the subcategory  or category is currently selected. You would do this by checking the lowest level first, then work your way up

 

eg.

Test3 =
var _allTickets = VALUES(Tickets[ID Ticket])
return
IF( HASONEVALUE(Produits[Sub Famille]), 
		var _topSubCat = GENERATE(_allTickets, TOPN(1, ALL(Produits[Sub Famille]), [CA YR N], DESC))
		RETURN COUNTROWS(FILTER(_topSubCat, [Sub Famille] = SELECTEDVALUE(Produits[Famille])))
	IF( HASONEVALUE(Produits[Famille]), 
		var _topCat = GENERATE(_allTickets, TOPN(1, ALL(Produits[Famille]), [CA YR N], DESC))
		RETURN COUNTROWS(FILTER(_topCat, [Famille] = SELECTEDVALUE(Produits[Famille])))
	    , COUNTROWS(_allTickets) 
	)
)

View solution in original post

 Makes sense. So if i use a product hierarchy with 4 levels, i would start at the lowest level (say sku is level 4) then work my way up (level 3: line > level 2 : subcategory > level 1 : category). Is that right?

Thanks a lot!

View solution in original post

6 REPLIES 6
d_gosbell
Super User
Super User

Based on the data you provided you could calculate this with a measure like the following:

Nb of tickets where category has highest revenue in ticket = 
// Get all tickets in the current filter context
var _allTickets = VALUES(Sales[ticket id])
// get the top category for each ticket
var _topCat = GENERATE(_allTickets, TOPN(1, ALL(Sales[category]), [Total Revenue], DESC))
// count the "selected" categories in the _topCat table variable
return IF( HASONEVALUE(Sales[category]), COUNTROWS(FILTER(_topCat, [category] = SELECTEDVALUE(Sales[category]))), DISTINCTCOUNT(Sales[ticket id]) )

 

Thanks a lot

I transformed your formula slightly and it seems to work.

Test3 =
var _allTickets = VALUES(Tickets[ID Ticket])
var _topCat = GENERATE(_allTickets, TOPN(1, ALL(Produits[Famille]), [CA YR N], DESC))
return

IF( HASONEVALUE(Produits[Famille]), COUNTROWS(FILTER(_topCat, [Famille] = SELECTEDVALUE(Produits[Famille]))), COUNTROWS(_allTickets) )

 

HOWEVER, as you can see from the below, i built a table with category (yello) and underneath each category i have subcategories. What i'd like is a measure that can calculate what you did but depending if i'm at the category level, it calculates the top n by category, and if i'm at the subcategory level it calculates topn by subcategory.


Is it possible to create a single dynamic measure like that, that can be used in a product hierarchy?

Capture.JPG

 

 


@fabienrolland wrote:

What i'd like is a measure that can calculate what you did but depending if i'm at the category level, it calculates the top n by category, and if i'm at the subcategory level it calculates topn by subcategory.


Is it possible to create a single dynamic measure like that, that can be used in a product hierarchy?

 


To do this you would have to test if the subcategory  or category is currently selected. You would do this by checking the lowest level first, then work your way up

 

eg.

Test3 =
var _allTickets = VALUES(Tickets[ID Ticket])
return
IF( HASONEVALUE(Produits[Sub Famille]), 
		var _topSubCat = GENERATE(_allTickets, TOPN(1, ALL(Produits[Sub Famille]), [CA YR N], DESC))
		RETURN COUNTROWS(FILTER(_topSubCat, [Sub Famille] = SELECTEDVALUE(Produits[Famille])))
	IF( HASONEVALUE(Produits[Famille]), 
		var _topCat = GENERATE(_allTickets, TOPN(1, ALL(Produits[Famille]), [CA YR N], DESC))
		RETURN COUNTROWS(FILTER(_topCat, [Famille] = SELECTEDVALUE(Produits[Famille])))
	    , COUNTROWS(_allTickets) 
	)
)

 Makes sense. So if i use a product hierarchy with 4 levels, i would start at the lowest level (say sku is level 4) then work my way up (level 3: line > level 2 : subcategory > level 1 : category). Is that right?

Thanks a lot!


@fabienrolland wrote:

 Makes sense. So if i use a product hierarchy with 4 levels, i would start at the lowest level (say sku is level 4) then work my way up (level 3: line > level 2 : subcategory > level 1 : category). Is that right?


Yes, that is correct.

 

The reason for this is that a filter at lower level creates an implied filter in the "parent" levels.

 

So in a structure like the following:

 

Level1          Level2
Bikes            Mountain Bikes
Bikes            Road Bikes
Accessories  Helmets     

 

A filter on Level2 = "Road Bikes" will create an implied filter of Level="Bikes", so you should always check for filters from the lower levels first

 

Hi @fabienrolland ,

I'd like to suggest you to take a look at following blog about measure calculate on total level:

Measure Totals, The Final Word

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.