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
Moody_Data
Frequent Visitor

Calculated Column based on Multiple Rows

Hello All,

 

I am looking to create what I think is a fairly simple calculated column that will calculate the 'Order Revenue' column below. 

 

The unique order number may contain multiple rows.  The order revenue column should be calculated only on the highest priced unit within that order.  Here is how the table is organized in Power BI:

 
Order NumberUnitPriceOrder Revenue
123Case20
123Pallet1010
456Case22
789Case20
789Pallet1010
    
 

I am looking to create the 'Order Revenue' column in red above.  If there is an order with multiple different units, the new column will display just the max value of the price.

 

Thank you in advance and please let me know if further explanation is needed.

 

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@Moody_Data 

You could just drag ordernumber and price into the table visual, and use maximum categorize for price. max values.JPG

 

If you want to have the exact output in calculated column, the idea is you would need to create a index column, and if the order number for current row = previous role, then blank, if they are not the same order number you can return MAX(price) for the order number. Something like: 

Column = var maxprice = CALCULATE(MAX('Table'[Price]),FILTER('Table','Table'[Order Number]=EARLIER('Table'[Order Number]))) Return if([Order Number]= CALCULATE(MAX('Table'[Order Number]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1)),BLANK(),maxprice) 

 

Paul Zheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

View solution in original post

4 REPLIES 4
V-pazhen-msft
Community Support
Community Support

@Moody_Data 

You could just drag ordernumber and price into the table visual, and use maximum categorize for price. max values.JPG

 

If you want to have the exact output in calculated column, the idea is you would need to create a index column, and if the order number for current row = previous role, then blank, if they are not the same order number you can return MAX(price) for the order number. Something like: 

Column = var maxprice = CALCULATE(MAX('Table'[Price]),FILTER('Table','Table'[Order Number]=EARLIER('Table'[Order Number]))) Return if([Order Number]= CALCULATE(MAX('Table'[Order Number]),FILTER('Table','Table'[Index]=EARLIER('Table'[Index])-1)),BLANK(),maxprice) 

 

Paul Zheng
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Thank you @V-pazhen-msft , I created the new index column and with the expression provided in the solution, the Order Revenue column now displays only the max price for those orders that contain multiple lines.

 

Much appreciated!

amitchandak
Super User
Super User

@Moody_Data , Create a new column like

Order Revenue = if([Price]= maxx(filter(Table, Table[Order Number] =earlier(Table[Order Number])),Table[Price]),[Price],blank())

@amitchandak , Many thanks for the response.

 

This is very close to working but I may have left out a scenario from the original post.

 

If there is an order that contains multiple rows of only the same item, it is returning the max value of both rows:

 

Order NumberUnitPriceOrder Revenue
987Case22
987Case22

 

Ideally would appear as the below so that each order is assigned only 1 price each:

 

Order Number UnitPriceOrder Revenue
987Case20
987Case22

 

I will also take a look - thank you for any ideas on how to modify this to just show 1 price per order.

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.