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

How always get latest value for particular Id ?

Hi all,

 

I need to create measure to calculate cumulative sum of quantity field which always shows value for the end of selected period.

 

Source data is like this:

OpportunityIdDateKeyQuantity
120191101   2
120191215   -1
120200129   0
120200130   0
120200131   1
220200101   0
220200102   0
220200103   1

There are quantity values by date, but some date might be missing.

Output of my report should be like this(instead of month could be year, quarter,  or any other related non-date dimension - in this case it's should be total for whole table):

MonthQuantity
Nov 20192
Dec 20191
Jan 20203


To do this I have column which calculates running total for each opportunity:
TotalQuantity = CALCULATE(
SUM('Opportunity Daily Snapshot'[Quantity]),
ALL('Opportunity Daily Snapshot'),
'Opportunity Daily Snapshot'[DateKey]<=EARLIER('Opportunity Daily Snapshot'[DateKey]),
'Opportunity Daily Snapshot'[OpportunityId]=EARLIER('Opportunity Daily Snapshot'[OpportunityId])
)

Result:

OpportunityIdDateKeyQuantityTotalQuantity
12019110122
120191215-11
12020012901
12020013001
12020013112
22020010100
22020010200
22020010311

Now I need some measure/column to calculate sum(TotalQuntity)  by Month, using last  TotalQuantity value in each month.
I tried to do this in measure:
Test:=
CALCULATE (
SUM (TotalQuantity ),
FILTER ( ALL ( 'Opportunity Daily Snapshot'), 'Opportunity Daily Snapshot'[DateKey] = MAX ('Opportunity Daily Snapshot'[DateKey]) ),
VALUES ('Opportunity Daily Snapshot'[OpportunityId])
)
But problem, is that some opportunities don't have any values on the last day of month, which is returned by MAX ('Opportunity Daily Snapshot'[DateKey]). 

Is there any way to filter by max(Datekey) for particular OpportunityId in the selected period(Month, Quarter etc)?
Or is there any other way to get only latest row for each OpportunityId in current selection?

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

@cyberirbis - Did you get a resolution? If so, please mark as answer, otherwise, @ me.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Hi, I've tried this, but it doesn't resolve my particular issue.

 

Finally I fixed it by changing source table. I added records on each day for each OpportunityId. It return correct values, but in some cases query fails fith out of memory error

amitchandak
Super User
Super User

@cyberirbis , try like

sumx(summarize(table,table[OpportunityId],"_1" ,lastnonblankvalue(table[DateKey],sum(Table[Quantity]))),[_1])

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Community Champion
Community Champion

@cyberirbis - This is basically Lookup Min/Max if I understand correctly.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Lookup-Min-Max/m-p/985814#M434



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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.