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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Yggdrasill
Responsive Resident
Responsive Resident

Value of stock over time with movements and price changes

I have a model with five tables.

Yggdrasill_0-1671066313331.png

 

The fact table ('InstrumentTransactions') has dates of purchased or sold quantity of stocks.

The table that registeres value of a stock is in InstrumentExchangeRate table

 

To calculate the quantity owned at any given time I calculate it as a balance or an inventory like so:

 Qty Running Total = CALCULATE(
    SUM( InstrumentTransaction[Amount] ),
    FILTER(
        ALL( DimTradeDate ),
        DimTradeDate[dimDateKeyAsDate] <= MAX( DimTradeDate[dimDateKeyAsDate] )
    )
)

Where Amount is the actual quantity purchased or sold at DimTradeDate

Now here comes the interesting part and that is to multiply the quantity owned with the latest rate at any given time.

The table 'InstrumentExchangeRate' contains the date and new rate for any ticker. For simplicity we're just going to look at one ticker.

Yggdrasill_1-1671066738244.png

 

Now I want to calculate, using a DAX measure, the value of ticker A through ALL dates.

For example, I purchase 100 on 1.1.2022 and sell 50 on 24.2.2022 then my data would look like this:
(note my dates are in dd.mm.YYYY)

Yggdrasill_2-1671067095112.png

 

I for some reason have been unable to come up with a solution on this

 

 

 

 

1 ACCEPTED SOLUTION

Hi,

You may download my PBI file from here.  See the third table int he image.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

7 REPLIES 7
Yggdrasill
Responsive Resident
Responsive Resident

Here's a file with hardcoded examples of how the model looks like and data.

FindLatestValue in SDC table.pbix

Yggdrasill_0-1671102876557.pngYggdrasill_1-1671102892772.png

 

Hi,

You may download my PBI file from here.  See the third table int he image.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Yggdrasill
Responsive Resident
Responsive Resident

I'm modifiying the table so it only has max 1 value per day. Look at the InstrumentExchangeRate table as a slowly changing dimension

Yggdrasill
Responsive Resident
Responsive Resident

The plot thickens - It's a model in Analysis Services. I can't share the file nor use PQ :I

Ashish_Mathur
Super User
Super User

Hi,

Ensure that the dates in your visual are dragged from the Calendar Table.  Try these measures

Price = sum('InstrumentExchangeRate'[Value])

Last known price = 

Latest Balance with formula =
CALCULATE([Price],LASTNONBLANK('Calendar'[Date],CALCULATE([Price])))
Hope this helps.

Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, I added a relationship between the calendar table and the exchange table and it works.

Last known price =
VAR _Balance =
    CALCULATE (
        [new Price],
        LASTNONBLANK ( 'DimTradeDate'[dimdatekeyasdate], CALCULATE ( [new Price] ) )
    )
RETURN
    _Balance

Where Price is simply a sum of the exchange rate

But it fails when you have two ore more price changes per day so I did this instead:

Latest Rate = 
CALCULATE (
    MAX ( InstrumentExchangeRate[exchangerate] ),
    FILTER (
        InstrumentExchangeRate,
        InstrumentExchangeRate[dimdatekey] <= MAX ( DimTradeDate[dimdatekey] )
            && InstrumentExchangeRate[dimdatekey] >= MIN ( DimTradeDate[dimdatekey] )
    )
)



Now I just need to calculate the balance over time which should be the QTY Balance * latest price and I'm done.

But I only get this and I need to fill in the gaps

Yggdrasill_1-1671070967880.png

 

 

Hi,

In the initial example you took you did not mention that there can be more than 1 price on a day.  Anyways, in the Query Editor, the approach should be to retain only 1 row per day.  If you are amenable to my approach, then share the download link of your PBI file. 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.