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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.