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
Jtbonner1986
Helper I
Helper I

Calculating a Line of Balance (Running Totals)

Hi All, 

 

I have a problem i have run into when trying to create a running total of "line of balance" (LoB) 

 

The calcualtion for month 1 is ([total stock] + [purchases]) - [usage].

Then the following month the calculation will use the previous months output and then calculate the current months purchases - the current month usage

 

hope this makes sense? 

 

I have 3 tables (Stock, Purchases, Usage) and what i want to do is create a running Line Of Balance visual in power BI;

 

Stock;

MaterialStock on Hand
A100
B500
C1000

 

Purchases

MaterialPurchDateQuantity
A01/01/202310
A01/02/202320
B01/01/202310
B01/02/202320
C01/01/202310
C01/02/202320

 

Usage

 

MaterialUseageDateQuantity
A01/01/202350
A01/02/202310
B01/01/2023100
B01/02/202310
C01/01/2023300
C01/02/202320

 

what i want to do is create a table within power bi that outputs a visual with a running total calculated like the below. I have a calendar table already created:

 

Material01/01/202301/02/2023
A6070
B410420
C710710
 (Stock on hand + Purch) - Usage(Previous month LoB + Purchases) - usage

 

hope you can help

 

many thanks,

J

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

Hi @Jtbonner1986 ,

I created a sample pbix file(see the attachment), please check if that is what you want.

Measure = 
VAR _selmatrial =
    SELECTEDVALUE ( 'Stock'[Material] )
VAR _seldate =
    SELECTEDVALUE ( 'Calendar'[Date] )
VAR _stock =
    SUM ( 'Stock'[Stock on Hand] )
VAR _purchase =
    CALCULATE (
        SUM ( 'Purchases'[Quantity] ),
        FILTER ( 'Purchases', 'Purchases'[PurchDate] = _seldate )
    )
VAR _usage =
    CALCULATE (
        SUM ( 'Usage'[Quantity] ),
        FILTER ( 'Usage', 'Usage'[UseageDate] = _seldate )
    )
VAR _prempurchase =
    CALCULATE (
        SUM ( 'Purchases'[Quantity] ),
        FILTER (
            'Purchases',
            YEAR ( 'Purchases'[PurchDate] )
                = YEAR ( _seldate - 1 )
                && MONTH ( 'Purchases'[PurchDate] )
                    = MONTH ( _seldate - 1 )
        )
    )
VAR _premusage =
    CALCULATE (
        SUM ( 'Usage'[Quantity] ),
        FILTER (
            'Usage',
            YEAR ( 'Usage'[UseageDate] )
                = YEAR ( _seldate - 1 )
                && MONTH ( 'Usage'[UseageDate] )
                    = MONTH ( _seldate - 1 )
        )
    )
RETURN
    _stock + _purchase - ( _premusage - _prempurchase ) - _usage

yingyinr_0-1679034958548.png

If the above ones can't help you, could you please provide some raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? By the way, what kind of conditions are needed for different background colors? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
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

2 REPLIES 2
Jtbonner1986
Helper I
Helper I

Hi

 

Thanks so much for the response. So sorry it has taken me this long to reply. Sickness and other things have gotten in the way 

 

Okay, so the example file is exactly what i want. 

 

However when applying this to my dataset the results are not coming back as expected. I am not sure if this is because of the dates where there is no 'usage' or 'demand' and the calcualtion the resets to the original 'stock on hand'?

 

Any help is grately appreciated

 

Many thanks,

Josh

v-yiruan-msft
Community Support
Community Support

Hi @Jtbonner1986 ,

I created a sample pbix file(see the attachment), please check if that is what you want.

Measure = 
VAR _selmatrial =
    SELECTEDVALUE ( 'Stock'[Material] )
VAR _seldate =
    SELECTEDVALUE ( 'Calendar'[Date] )
VAR _stock =
    SUM ( 'Stock'[Stock on Hand] )
VAR _purchase =
    CALCULATE (
        SUM ( 'Purchases'[Quantity] ),
        FILTER ( 'Purchases', 'Purchases'[PurchDate] = _seldate )
    )
VAR _usage =
    CALCULATE (
        SUM ( 'Usage'[Quantity] ),
        FILTER ( 'Usage', 'Usage'[UseageDate] = _seldate )
    )
VAR _prempurchase =
    CALCULATE (
        SUM ( 'Purchases'[Quantity] ),
        FILTER (
            'Purchases',
            YEAR ( 'Purchases'[PurchDate] )
                = YEAR ( _seldate - 1 )
                && MONTH ( 'Purchases'[PurchDate] )
                    = MONTH ( _seldate - 1 )
        )
    )
VAR _premusage =
    CALCULATE (
        SUM ( 'Usage'[Quantity] ),
        FILTER (
            'Usage',
            YEAR ( 'Usage'[UseageDate] )
                = YEAR ( _seldate - 1 )
                && MONTH ( 'Usage'[UseageDate] )
                    = MONTH ( _seldate - 1 )
        )
    )
RETURN
    _stock + _purchase - ( _premusage - _prempurchase ) - _usage

yingyinr_0-1679034958548.png

If the above ones can't help you, could you please provide some raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? By the way, what kind of conditions are needed for different background colors? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

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.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.