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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
nsessa
New Member

How to sum one column based on unique values in another column

Hi, 

 

I have one table i am currently trying to work with in Power Bi. I have 2 columns, one is sales orders, the other is the zip code that the order went to. I am trying to figure out how I can find the total orders that have the same zip code associated to it. 

 

What would be the best way to do this?

6 REPLIES 6
amitchandak
Super User
Super User

@nsessa , You can create new column and new measure like given below

new column = sumx(filter(table, [zip] = earlier([zip])),[sales])

new measure = sumx(filter(allselected(table), [zip] = max([zip])),[sales])

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi, 

 

Thank you both for the suggested solutions but neither of them to seem to work. The issue I am having in the beginning is that both the zip code & order column were in text format. I have changed to a whole number but whenever I try to create a visual off of the measure or column, I get an error message saying it can't display.

@nsessa

Zip codes should be text. Even if the format is a number, these are not numbers that should have a number semantics - you don't perform typical number operations like addition on zip codes. So, they should be text.

Second, if you drag the zip code column to a visual (say, table), then this measure will give you the total of orders that are associated to this code:

[Total] = SUM( T[Order Amount] )

where T is the name of your table.

If your need is different and you want to be able to drag orders onto a visual and then get the total for all orders for all the visible zip codes in the current context, then your measure would be:

CALCULATE( [Total], ALL( T ), VALUES( T[ZipCode] )
mahoneypat
Microsoft Employee
Microsoft Employee

To get the total for the same zip code, you can use this measure expression

 

Total This Zipcode = CALCULATE(SUM(Table[Amount]), ALLEXCEPT(Table, Table[Zipcode]))

 

Replace with your actual table and column names.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


camargos88
Community Champion
Community Champion

Hi @nsessa ,

 

Just drag both columns to a table visual for example and change the aggregation of the sales order to count.

 

So you have a total orders by zip code.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Hi @camargos88 thank you for that advice. Is there any way to do this as a measure though? 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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