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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Jtbonner1986
Frequent Visitor

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

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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