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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
julesdude
Post Partisan
Post Partisan

Pie Chart Problems - Can't Add Values for Totals Using Last Date Only

Hi there,

 

Edit to my original post as it wasn't clear! I'm having difficulty getting my pie chart to work. I have a source table that's like this:

 

Last UpdatedCountryAsset ReferenceTotal Value
02/03/2022Italyaaa4,324
02/03/2022Italybbb42
04/01/2022Egyptccc56,540,000
16/03/2021Italyaaa546
16/03/2021Italybbb16,730,000
18/12/2020Egyptccc546
24/08/2020Egyptccc4
24/08/2020Egyptccc6
27/08/2019Italyaaa7,567
27/08/2019Italyaaa56
27/08/2019Italyaaa4,534
27/08/2019Italyaaa53
27/08/2019Italybbb634
27/08/2019Italybbb42
27/08/2019Italybbb5,423
27/08/2019Italybbb534
23/07/2019Egyptccc635
22/07/2019Egyptccc6
21/07/2019Egyptccc457
20/07/2019Egyptddd4
19/07/2019Egyptddd657
18/07/2019Egypteee65,765
17/07/2019Egypteee746
16/07/2019Egyptddd74
15/07/2019Egyptddd5,435

 

I want a pie chart and a table to gather the total amount for each country using the latest date of each asset reference that is available and ignore values for any date prior to the latest date for that asset. So essentially, in table form (which i can also easily switch to a pie using the same logic) it would give me the following values:

 

CountryTotal Value
Italy4,366
Egypt

56,540,004

 

How can I achieve this? All I am getting at the moment is a total amount for Country that sums up every value against every date for every asset reference. All I want is country totals that only adds values of assets with that have the latest date only for that asset.

 

I hope that makes sense! Any help would be really appreciated! 

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

Hi, @julesdude ;

You could create a measure.

total =
var _max=CALCULATE(MAX('TableA'[Date]),ALLEXCEPT(TableA,'TableA'[Country]))
return CALCULATE(SUM('TableB'[Total Value]),FILTER(TableB,[Date]=_max))

The final show:

vyalanwumsft_0-1658977115672.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

17 REPLIES 17
julesdude
Post Partisan
Post Partisan

@TheoC thank you so much for your time helping me on this.

And....it works perfectly for sub total values in the table.

The only issue now is that these are not reflected in the Grand Total, which is a problem because it is this total I'll need to be showing in the table I'm creating and the pie chart:

julesdude_0-1659086723831.png

Can Grand Totals like this reference just the visible counterparts in the table? Is it even possible in Power BI?

 

Thanks again for all your help with this.

TheoC
Super User
Super User

Hi @julesdude 

 

Okay, I am hoping based on all the back and forths, this is the right outcome you are wanting:

 

TheoC_0-1659055972737.png

Basically, the measure is saying to only return the Amount for the Country and Asset Reference if the date is the Last Date, and then make everything else disappear (just from the table visual).

 

Measure = 

VAR _0 = LASTDATE ( 'Table'[Date] )
VAR _1 = CALCULATE ( LASTDATE( 'Table'[Date] ) , ALLEXCEPT ( 'Table' ,'Table'[Country] ,'Table'[Asset Reference] ) )
VAR _2 = IF ( _1 = _0 , SUM ('Table'[Amount] ) , BLANK() )

RETURN

_2

After you shared the new data, it became a lot easier to follow. PBIX is attached.

 

Please mark this as the correct solution if it is what you're after!

 

Thanks heaps,

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

v-yalanwu-msft
Community Support
Community Support

Hi, @julesdude ;

You could create a measure.

total =
var _max=CALCULATE(MAX('TableA'[Date]),ALLEXCEPT(TableA,'TableA'[Country]))
return CALCULATE(SUM('TableB'[Total Value]),FILTER(TableB,[Date]=_max))

The final show:

vyalanwumsft_0-1658977115672.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.

@julesdude with the changes you require, @v-yalanwu-msft has provided a good solution and I recommend that you accept this post as a solution if you can.

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Thanks @v-yalanwu-msft and @TheoC 

 

I get this error when I implement the DAX, I think because of the references to two different tables:

The syntax for Table A is incorrect.......etc.

 

julesdude_3-1659000249598.png

total =
var _max=CALCULATE(MAX('TableA'[Date]),ALLEXCEPT(TableA,'TableA'[Country]))
return CALCULATE(SUM('TableB'[Total Value]),FILTER(TableB,[Date]=_max))

I had changed this to:

 

 

 

total = 
var _max=CALCULATE(MAX('Table B'[Date],ALLEXCEPT('Table A'[Country]))
return CALCULATE(SUM('Table B'[Value]),FILTER('Table B'Date]=_max))

....to try use the date in Table B. The date in Table A isn't really relevant here because it's a last updated date more than anything else. It's the Date in Table B that I need to be the latest and have the MAX on to find the latest date for an asset reference.

 

So basically I need listed the country field from Table A which provides the list of countries, then from Table B to be a sum of the latest date values by asset reference (like in table in my OP), to give you a table split by country giving you the total of the latest asset values.

If you could help further then that would be great. I hope this makes sense.

 

 

 

 

@TheoC @v-yalanwu-msft 

This may explain a bit better:

 

julesdude_0-1659008314733.png

 

@julesdude  I have adapted @v-yalanwu-msft solution. Can you try this as a measure. Just adjust column names to what they are in your data.

 

 

 

 

 

Measure = 

VAR _1 = 

	CALCULATE ( 
		MAX ( 'Table'[Last Updated] ) ,
			ALLEXCEPT ( 'Table' , 'Table'[Country], 'Table'[Asset Reference] )
			)

RETURN

	CALCULATE ( 
		SUM ( 'Table'[Amount] ) ,
			FILTER ( 'Table'[Last Updated] = _1 )
			)

 

 

 

 

 

 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Thanks so much @TheoC 

The only problem is that country needs to be called from the other table - Table A via the Asset Reference link. Will the above still work? I simplified the latest table so give an idea of what it should look like as one. But the country is looked up using the asset reference link:

 

julesdude_0-1659015751379.png

 

 

 

@julesdude okay, thanks so much for sending that through. Unfortunately, due to your Table B only having Asset References and no country, you cannot achieve what you are wanting because there is no way for Power BI to determine which Asset Reference is associated with which country. 

 

For example, Both Italy and Egypt have the same Asset Reference (e.g. AAA can be either Italy or Egypt).  And, given you have linked Table A and Table B via a relationship at the Asset Reference level (which is NOT a unique key), Power BI does not know which Country to return because there is nothing distinct or unique to identify the Country that the Asset Reference belongs to.

 

I hope that makes sense?

 

Cheers mate.

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Hi @TheoC 

Thanks for looking at it. It sort of makes sense. I was a bit lost when you said AAA can be either Italy or Egypt because TableA is listing distinct references and the country column provides the country membership they belong to.
So if you're saying that the DAX can't lookup this reference to begin summing assets for each country, how else can I do this?
I had a thought - what if I merged the two tables/queries in Power Query so that I create a new column in Table B called Country which lists the corresponding country based on the asset reference join of Table A to Table B. We would then get the following:

julesdude_0-1659047914720.png

This could be better to work with as now all held in the same table, but I'd still be uncertain how the DAX would operate and call requiring the logic above?

Hi @julesdude 

 

As long as you have the ability to identify which Asset Reference belongs to which Country, then you can do it.  The issue in this instance, with the data that currently exists in Table A and Table B, it cannot be done.

 

When I was putting together a new measure for you which started with a LOOKUPVALUE as the first variable, it wasn't letting add 'Table A'[Country] as the third parameter being the Search Value.  The reason for this is because there's no way for Power BI to determine which Asset Reference belongs to a specific Country with respect to the population of data that currently exists.

 

Unless there is other data that you have which has not been shared, there is no way in which the outcome can be achieved unfortunately (from my understanding).

 

I hope this makes sense.

 

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Hi @TheoC 

Yes I'm afraid although I've simplified and I've taken a number of columns out that aren't relevant, this is all I've got that's relevant to this issue.
Ok, I have merged the query to bring a country reference column into table B so that I now have this:

 

julesdude_0-1659050810913.png

Updated file:

File 

 

Can your DAX above be adapted to now just reference this table in some way?

Hi @TheoC @v-yalanwu-msft 

 

I've attached a sample workbook here removing unnecessary data and providing only needed columns as examples above. Data has been replaced slightly. :

 

Example 

@julesdude definitely.  Can you send the data you're working off and I'll put a solution together for you first thing in the morning!

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

TheoC
Super User
Super User

Hi @julesdude 

 

I used two calculated columns to achieve the below however I am confident this can be achieved more efficiently in some other way. Output below:

 

TheoC_1-1658796649322.png

 

 

Two calculated columns are:

 

1. Max Date to return the latest date based on the country:

 

Max Date =

VAR _1 = 'Table (2)'[Country]
VAR _2 = MAXX ( FILTER ( ALL ('Table (2)' ) , 'Table (2)'[Country] = _1 ) , 'Table (2)'[Last Updated] )

RETURN

_2

2. Max Amount to return the maximum amount based on parameters specified:

 

Max Amount =


VAR _1 = CALCULATE ( MAX ('Table (2)'[Total Value] ) , ALLEXCEPT ( 'Table (2)' , 'Table (2)'[Country] ,'Table (2)'[Asset Reference] ,'Table (2)'[Last Updated] ) )
VAR _2 = IF ( AND ( _1 = 'Table (2)'[Total Value] , 'Table (2)'[Last Updated] = 'Table (2)'[Max Date] ) , _1 , 0 )

RETURN

_2

 

I hope this helps 🙂

 

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

@TheoC thank you I am very grateful for your help.

 

I think your solution might be difficult to implement for added DAX columns because in my example I did simplify things somewhat into one table. Technically I have two tables at work here (although they do have a relationship) which are joined by asset reference:

julesdude_0-1658830044656.png

So if we are going to go the add column using DAX route, it might need to be modified?

Like you, I was thinking there could be a cleaner way of achieving this, but I'm happy to go with whatever works if the extra columns route is easier. 

Any help appreciated.

@julesdude apologies! I hit Accept Solution instead of Reply. I will provide you a solution shortly!

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

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.