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

How to calculate opening /closing stock using DAX

Hi all, 

 

I have the following data set and I am trying to calculate the opening and closing stock in each month. 

 

DateProduct nameOPENING STOCKPurchasesSalesCLOSING STOCK
01-Jan-21Product 11400100-3001200
01-Jan-21Product 2200200-300100
01-Jan-21Product 3400300-200500
01-Jan-21Product 41000400-1001300
01-Feb-21Product 11200100-3001000
01-Feb-21Product 210050-10050
01-Feb-21Product 3500300-200600
01-Feb-21Product 41300400-1001600
01-Mar-21Product 11000100-300800
01-Mar-21Product 250450-300200
01-Mar-21Product 3600300-200700
01-Mar-21Product 41600400-1001900
01-Apr-21Product 1800100-300600
01-Apr-21Product 2200200-150250
01-Apr-21Product 3700300-200800
01-Apr-21Product 41900400-1002200

 

When I select the exact month then the correct opening and closing stocks are shown within the dashboard  

Antros_0-1671801757954.png

Whereas if I select more than one month then the opening and closing stock sums all the previous months and does not show the correct closing balance  

Antros_1-1671802169056.png

 

Thank you in advance for your support!

1 ACCEPTED SOLUTION
Antros
Frequent Visitor

The below helped me resolve the issue: 

 

The opening and closing cannot be aggregated over the Date dimension.

 

I have created measures to handle it correctly as below:

Opening = CALCULATE(SUM(CLOSING_STOCK), FIRSTDATE(DATE))

Closing  = CALCULATE(SUM(CLOSING_STOCK), LASTDATE(DATE))

View solution in original post

5 REPLIES 5
Antros
Frequent Visitor

Thank you for the response!

 

Unfortunately this does not resolve the issue when you are trying to see the by product analysis.

 

As shown in the image below the closing stock using the measure  

Closing stock = CALCULATE(SUM('Sample data'[CLOSING STOCK]),FILTER(ALLSELECTED('Sample data'),'Sample data'[Date] <= MAX('Sample data'[Date]))) shows the same closing stock for all the producs which is not the case

Antros_0-1672307342767.png

 

Antros
Frequent Visitor

The below helped me resolve the issue: 

 

The opening and closing cannot be aggregated over the Date dimension.

 

I have created measures to handle it correctly as below:

Opening = CALCULATE(SUM(CLOSING_STOCK), FIRSTDATE(DATE))

Closing  = CALCULATE(SUM(CLOSING_STOCK), LASTDATE(DATE))

@Antros But mai I know why are you using measure in table for closing stock instead of closing stock column.

Antros
Frequent Visitor

Hi apologies for the confusion, the reason that I am trying to put a measure is because the actual closing stock is included in my data set and I don't want to do any sum. 

In the example provided the actual closing stock for Αpr-21 is €3.850 and not €13.800

The problem that I am facing is when I select more than one month in the slicer or even if I don't select any month then in the table shows the aggregate amount of €13.800 instead of €3.850

Antros_2-1672309135528.png

 

 

ddpl
Solution Sage
Solution Sage

@Antros try this

Create Measure as per below:

 

Measure = CALCULATE(SUM('Table'[CLOSING STOCK]),FILTER(ALLSELECTED('Table'),'Table'[Date] <= MAX('Table'[Date])))

 

ddpl_0-1672219110913.png

 

OR YOU CAN USE WATERFALL CHART AS PER BELOW:

Here did not required to create any measure

ddpl_1-1672219633920.png

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.