cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Ramzan Regular Visitor
Regular Visitor

DAX to Calculate Opening and Closing Stock including Previous Period's Records

Hy all

Greetings and thanks in advance.

I have a large data table in Data Model in excel 2016. This table is in SQL Server and is connected to Data Model in excel with Power Query. It has millions of rows. I have created pivot table report on it.

I need to calculate opening and closing stock for selected period (date, month or year). The sample of data is as given below. I am showing just a few columns and records in sapmle.

 

Untitled.png

 

I have added 3 measures in the Pivot table fields.

1-Movement=calculate(sum('Table'[Qty]), 'Table'[In/Out]="In")

-calculate(sum('Table'[Qty]), 'Table'[In/Out]="Out")

It is working fine.

2-Opening Stock=calculate ( [Movement],

filter

 (all ('Table'[Date] ),

'Table'[Date]<max('Table'[Date] )))

3-Closing Stock=calculate ( [Movement],

filter

 (all ('Table'[Date] ),

'Table'[Date]<=max('Table'[Date] )))

There is a problem in the results of measure No. 2 and 3.

The result is ok if pivot table is viewed for all periods without filtering. But when I filter period (Date, Month or Year)  in pivot table, then result is not correct. It does not bring forward the closing stock of previous period.

Please suggest me some change in dax formula or some new formula for opening and closing stock calculation including previous period's data.

I will be very gratefull for any help.
The Pivot table I created is as given below.
Untitled2.pngUntitled3.png



1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: DAX to Calculate Opening and Closing Stock including Previous Period's Records

Hi

I modify two measures above with these:

 

Opening Stock =
CALCULATE (
    [Movement],
    FILTER (
        ALLEXCEPT ( Table1, Table1[Iteam Name] ),
        'Table1'[Date] < MAX ( 'Table1'[Date] )
    )
)

 

 

Closing Stock =
CALCULATE (
    [Movement],
    FILTER (
        ALLEXCEPT ( Table1, Table1[Iteam Name] ),
        'Table1'[Date] <= MAX ( 'Table1'[Date] )
    )
)

Finally, I get this result.

 

4.png

5.png

 

Best Regards

Maggie

View solution in original post

1 REPLY 1
Community Support Team
Community Support Team

Re: DAX to Calculate Opening and Closing Stock including Previous Period's Records

Hi

I modify two measures above with these:

 

Opening Stock =
CALCULATE (
    [Movement],
    FILTER (
        ALLEXCEPT ( Table1, Table1[Iteam Name] ),
        'Table1'[Date] < MAX ( 'Table1'[Date] )
    )
)

 

 

Closing Stock =
CALCULATE (
    [Movement],
    FILTER (
        ALLEXCEPT ( Table1, Table1[Iteam Name] ),
        'Table1'[Date] <= MAX ( 'Table1'[Date] )
    )
)

Finally, I get this result.

 

4.png

5.png

 

Best Regards

Maggie

View solution in original post

Helpful resources

Announcements
New Kudos Received Badges Coming

New Kudos Received Badges Coming

Kudos to you if you earned one of these! Check your inbox for a notification.

Microsoft Implementation for Communities Wins Award

Microsoft Implementation for Communities Wins Award

Learn about the award-winning innovation that was implemented across Microsoft’s Business Applications Communities.

Power Platform World Tour

Power Platform World Tour

Find out where you can attend!

Top Kudoed Authors (Last 30 Days)
Users online (4,288)