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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
cconnolly
Frequent Visitor

Calculated table grouped by a field (done) and labeled with conditional lookup using MAX function

Hi Everyone,

 

I work for a hospital and am working on a report to show total charges for each patient labeled with the CPT code (billable code for outpatient hospital services) that has the highest charge amounts.  This is considered the 'main CPT code' on the account. I can't include the real data for obvious reasons, but the data is formatted similar to the example below:

 

cconnolly_1-1659561085275.png

 

Note that goal output table includes all of the charges summed into one line, which I can handle by creating a calculated table.  The issue I'm having is how to lookup in the CPT code with the largest charge amount.  The data I'm using will be about ~2M rows by EOY and is super wide so I want to do this as efficiently as I can.  Any advice?

 

Hope that this is enough info for you to understand my issue! Happy to add any color that would be helpful.

 

Thanks,

Charlie

1 ACCEPTED SOLUTION
v-yalanwu-msft
Community Support
Community Support

Hi, @cconnolly ;

I changed the simple.

vyalanwumsft_0-1659926003389.png

Then you could create a new calculate table.

Table = 
SUMMARIZE(Data,Data[Patient],
"CPT",CALCULATE(MAX('Data'[CPT]),FILTER('Data',[Charges]=CALCULATE(MAX('Data'[Charges]),ALLEXCEPT(Data,'Data'[Patient])))) ,
"Charges",sum(Data[Charges]))

the final show:

vyalanwumsft_1-1659926067133.png

Or you could create a measure.

maxcpt = 
CALCULATE(MAX('Data'[CPT]),FILTER(ALL(Data),[Patient]=MAX('Data'[Patient])
&& [Charges]= CALCULATE(MAX('Data'[Charges]),ALLEXCEPT(Data,'Data'[Patient]))))

The final show:

vyalanwumsft_2-1659926107385.png


Best Regards,
Community Support Team _ Yalan Wu
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

5 REPLIES 5
v-yalanwu-msft
Community Support
Community Support

Hi, @cconnolly ;

I changed the simple.

vyalanwumsft_0-1659926003389.png

Then you could create a new calculate table.

Table = 
SUMMARIZE(Data,Data[Patient],
"CPT",CALCULATE(MAX('Data'[CPT]),FILTER('Data',[Charges]=CALCULATE(MAX('Data'[Charges]),ALLEXCEPT(Data,'Data'[Patient])))) ,
"Charges",sum(Data[Charges]))

the final show:

vyalanwumsft_1-1659926067133.png

Or you could create a measure.

maxcpt = 
CALCULATE(MAX('Data'[CPT]),FILTER(ALL(Data),[Patient]=MAX('Data'[Patient])
&& [Charges]= CALCULATE(MAX('Data'[Charges]),ALLEXCEPT(Data,'Data'[Patient]))))

The final show:

vyalanwumsft_2-1659926107385.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

The summarized table was exactly what I needed. Thank you!

ryan_mayu
Super User
Super User

@cconnolly 

you can just add the column to table visual

1.png

or create a table

Table = SUMMARIZE(Data,Data[Patient],"CPT",max(Data[CPT]),"Charges",sum(Data[Charges]))

 

2.PNG

pls see the attachment below





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

Proud to be a Super User!




Thank you! @ryan_mayu 

 

This is slightly different that what I'm trying to do.  My example was ambiguous so that's my fault.

 

Instead of including the highest CPT code by the CPT number, I want to include the CPT code with the largest amount in the charge column.  Your query functions correctly because in my example the highest CPT codes have the largest charges but it doesn't work in my actual data set.

 

Thank you,

@cconnolly 

could you pls provide the sample data which is close to your acutal data?





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

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.