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

How to calculate purchase and sales stock information from two columns into one separate column?

Hello,

 

I think I have a very simple question but I can't figure it out, I have a table with purchase and sales with stock quantity. I want to create a separate column that calculates the daily stock value. But how can I get a Total stock result like the column  "Total Stock Good" in my example, I just created that to makes things clear.

 

 

bkoenen_1-1639497005852.png

 

With kind regards,

 

Björn

1 ACCEPTED SOLUTION
YukiK
Impactful Individual
Impactful Individual

There may be other solutions, but I'd get cumulative for both purchase and sales qty and then subtract cumulative sales from cumulative purchase. The following measure should work:

My Measure =
VAR __SalesRunningTotal =
CALCULATE (
SUM ( 'financials'[ Sales] ),
FILTER (
ALLSELECTED ( 'financials'[Date] ),
ISONORAFTER ( 'financials'[Date], MAX ( 'financials'[Date] ), DESC )
)
)
VAR __DiscountRunningTotal =
CALCULATE (
SUM ( 'financials'[Discounts] ),
FILTER (
ALLSELECTED ( 'financials'[Date] ),
ISONORAFTER ( 'financials'[Date], MAX ( 'financials'[Date] ), DESC )
)
)
RETURN
__SalesRunningTotal - __DiscountRunningTotal

YukiK_0-1639499448766.png

 

Using power bi sample data

 

Please give it a thumbs up if this helps!

View solution in original post

4 REPLIES 4
smpa01
Super User
Super User

@bkoenen you would need an index column and then you can either do a calulcated column like this

Column =
VAR _purchase =
    SUMX (
        FILTER (
            'Table',
            'Table'[Index] <= EARLIER ( 'Table'[Index] )
                && 'Table'[Article] = EARLIER ( 'Table'[Article] )
        ),
        'Table'[Purchase QTY]
    )
VAR _sales =
    SUMX (
        FILTER (
            'Table',
            'Table'[Index] <= EARLIER ( 'Table'[Index] )
                && 'Table'[Article] = EARLIER ( 'Table'[Article] )
        ),
        'Table'[Sales QTY ]
    )
RETURN
    _purchase - _sales

 

or a measure like this

Measure =
VAR _filter =
    FILTER (
        ALL ( 'Table' ),
        'Table'[Index] <= MAX ( 'Table'[Index] )
            && 'Table'[Article] = MAX ( 'Table'[Article] )
    )
VAR _purchase =
    CALCULATE ( SUM ( 'Table'[Purchase QTY] ), _filter )
VAR _sales =
    CALCULATE ( SUM ( 'Table'[Sales QTY ] ), _filter )
RETURN
    _purchase - _sales

smpa01_0-1639501018927.png

 

smpa01_1-1639501075283.png

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Thank you for the code, I copied it and it works.

Kind regards Björn

YukiK
Impactful Individual
Impactful Individual

There may be other solutions, but I'd get cumulative for both purchase and sales qty and then subtract cumulative sales from cumulative purchase. The following measure should work:

My Measure =
VAR __SalesRunningTotal =
CALCULATE (
SUM ( 'financials'[ Sales] ),
FILTER (
ALLSELECTED ( 'financials'[Date] ),
ISONORAFTER ( 'financials'[Date], MAX ( 'financials'[Date] ), DESC )
)
)
VAR __DiscountRunningTotal =
CALCULATE (
SUM ( 'financials'[Discounts] ),
FILTER (
ALLSELECTED ( 'financials'[Date] ),
ISONORAFTER ( 'financials'[Date], MAX ( 'financials'[Date] ), DESC )
)
)
RETURN
__SalesRunningTotal - __DiscountRunningTotal

YukiK_0-1639499448766.png

 

Using power bi sample data

 

Please give it a thumbs up if this helps!

amitchandak
Super User
Super User

@bkoenen , Try a new columns

Sumx(filter(sheet, [Article] = earlier([Article]) && [delivery Date] <= earlier([delivery Date])),[Purchase Qty] - [Sales Qty])

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.