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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
leolapa_br
Resolver I
Resolver I

Consolidated running balance from two fact tables

I have this simple data model w/ two fact tables that pertain to purchases and sales of shares of stock:

Data_Model.png

 

I need to calculate a running balance by taking a running sum of shares purchased by ticker and SUBTRACT by the running sum of shares sold by ticker.

 

I tried this:

 

Shares balance 1 = 
VAR Date_Ref = MAX( dDates[Date] )
VAR Tick_Ref = VALUES( dAssets[Ticker] )
VAR Cumm_Purch = 
    CALCULATE( 
        SUMX( 
            fPurch, 
            fPurch[Shares] 
        ), 
        fPurch[Ticker] = Tick_Ref, 
        fPurch[Date] <= Date_Ref 
    )
VAR Cumm_Sold = 
    CALCULATE( 
        SUMX( 
            fSales, 
            fSales[Shares] 
        ), 
        fSales[Ticker] = Tick_Ref, 
        fSales[Date] <= Date_Ref 
    )
VAR Result = Cumm_Purch - Cumm_Sold
RETURN
    Result

 

And I also tried this:

 

Shares balance 2 = 
VAR Date_Ref_Purch = MAX( fPurch[Date] )
VAR Tick_Ref_Purch = VALUES( fPurch[Ticker] )
VAR Cumm_Purch = 
    CALCULATE( 
        SUMX( 
            fPurch, 
            fPurch[Shares] 
        ), 
        fPurch[Ticker] = Tick_Ref_Purch, 
        fPurch[Date] <= Date_Ref_Purch 
    )
VAR Date_Ref_Sold = MAX( fSales[Date] )
VAR Tick_Ref_Sold = VALUES( fSales[Ticker] )
VAR Cumm_Sold = 
    CALCULATE( 
        SUMX( 
            fSales, 
            fSales[Shares] 
        ), 
        fSales[Ticker] = Tick_Ref_Sold, 
        fSales[Date] <= Date_Ref_Sold 
    )
VAR Result = Cumm_Purch - Cumm_Sold
RETURN
    Result

 

And both produce the same WRONG results, as shown on the sample table that follows...

 

For this visual the 'Ticker' column comes from the dimension table dAssets and the 'Date' column comes from the date table.

 

And the red column I manually added to show the numbers I was supposed to compute:

 

TickerDate

Shares

purchased

Shares

sold

Shares

balance 1

Shares

balance 2

Correct

result

ABEV   3/1/2021   77 777777
ABEV   5/4/2021   73 7373150
ABEV   6/4/2021    150-150-1500
ABEV   6/28/2021   62 626262
ABEV   10/29/2021    62-62-620
ALSO3   1/7/2021   39 393939
ALSO3   2/3/2021    39-39-390
ALSO3   3/1/2021   45 454545
ALSO3   5/11/2021    45-45-450
ALSO3   6/29/2021   36 363636
ALSO3   1/7/2022   56 565692
ALSO3   3/30/2022    92-92-920

 

I suppose something on either code is messing up the results, what could that be?

1 ACCEPTED SOLUTION
ERD
Community Champion
Community Champion

Hi @leolapa_br ,

You can try this (you were close):

ERD_0-1692723795670.png

Shares balance = 
VAR Date_Ref = MAX('Date'[Date])
VAR Tick_Ref = MAX(dAssets[Ticker])
VAR Cumm_Purch = 
    CALCULATE( 
        SUM(fPurch[Sharespurchased]),
        dAssets[Ticker] = Tick_Ref, 
        'Date'[Date] <= Date_Ref
    )
VAR Cumm_Sold = 
    CALCULATE( 
        SUM(fSales[Sharessold]), 
        dAssets[Ticker] = Tick_Ref, 
        'Date'[Date] <= Date_Ref 
    )
VAR Result = IF([Shares purchased] <> BLANK() || [Shares sold] <> BLANK(), Cumm_Purch - Cumm_Sold)
RETURN
    Result

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

View solution in original post

1 REPLY 1
ERD
Community Champion
Community Champion

Hi @leolapa_br ,

You can try this (you were close):

ERD_0-1692723795670.png

Shares balance = 
VAR Date_Ref = MAX('Date'[Date])
VAR Tick_Ref = MAX(dAssets[Ticker])
VAR Cumm_Purch = 
    CALCULATE( 
        SUM(fPurch[Sharespurchased]),
        dAssets[Ticker] = Tick_Ref, 
        'Date'[Date] <= Date_Ref
    )
VAR Cumm_Sold = 
    CALCULATE( 
        SUM(fSales[Sharessold]), 
        dAssets[Ticker] = Tick_Ref, 
        'Date'[Date] <= Date_Ref 
    )
VAR Result = IF([Shares purchased] <> BLANK() || [Shares sold] <> BLANK(), Cumm_Purch - Cumm_Sold)
RETURN
    Result

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Appreciate your Kudos.

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

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.