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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ianburns143
New Member

Creating a Accumulated Net Inventory on hand qty table

Hi all

 

Very new to coding in power query and just started using Power Pivot.. but getting there slowly, building myself up to get into Power Bi

 

basically i have 3 tables  which i need to transform

 

1/ STOCK onhand - Type = Stock, Code , QTY Transaction date = Today - per item could have various Qty in same day

2/ STOCK IN WARD -Type = Work order,  Code , QTY Transaction date =  various - per item could have various Qty's in same day

3/ STOCK OUT WARD Type = Sales order , Code, QTY , Transaction date = various - per item could have various Qty's in same day

 

 

 

Examples of my Tables (simplified)

 

TYPECODEQTYDATE
STOCKA1631/07/2025
STOCKA1431/07/2025
STOCKC1131/07/2025

 

TYPECODEQTYDATE
WORK ORDERB12031/07/2025
WORK ORDERA12002/08/2025
WORK ORDERA11003/08/2025
WORK ORDERB1228/08/2025
WORK ORDERA11005/09/2025
WORK ORDERC11005/09/2025

 

TYPECODEQTYDATE
SALES ORDERA12001/07/2025
SALES ORDERA11001/07/2025
SALES ORDERA11001/08/2025
SALES ORDERB12002/08/2025
SALES ORDERC1603/09/2025

 

The calculation = Stock qty per code + Work order per code - Sale order per code ( did do a power pivot measure with this calculation, but the qty's are not accumulated from 1 period (date) to the next )

 

so for any give date (time frame) I can create a  power pivot table to pull through as below

 

 JulyAugustSept
CODENET STOCKWORK ORDERSALES ORDERNET STOCKWORK ORDERSALES ORDERNET STOCKWORK ORDERSALES ORDERNET STOCK
A110030-203010-101000
B1020200202002
C11001001106-5
D10000000000

 

as an add on for STOCK table I have a created date = today. however Sales Order or Work Order Dates could be in the Past.. Is there a away If all these tables are joined that it will reflect the earliest date in the full data set.

 

Any help in how I pull these 3 tables together to get the desired output would be greatly appreciated , and which i will ofc added to my Power Query/ Power Pivot (Bi) knowledge 

 

1 ACCEPTED SOLUTION

Hey @ianburns143,

Looking at your issue, I can see the problem. Your measures are summing ALL transactions across the entire dataset instead of respecting the pivot table's row context (CODE and DATE). The ALL(Transactions) function is removing all filters, which is why you're getting totals across everything.

Here's the alternate approach:

Replace your current measures with these corrected versions:

1. Net Inventory (Fixed):

Net Inventory = SUM(Transactions[FINAL QTY])

2. Accumulated Net Inventory (Fixed):

Accumulated Net Inventory =
CALCULATE(
SUM(Transactions[FINAL QTY]),
FILTER(
ALL(Dates[Date]),
Dates[Date] <= MAX(Dates[Date])
)
)

3. Individual Transaction Types (Fixed):

STOCK =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "STOCK"
)

WORK ORDER =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "WORK ORDER"
)

SALES ORDER =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "SALES ORDER"
)

Why Your Original Measures Weren't Working

  1. ALL(Transactions) removes all filters from the Transactions table, including the CODE filter from your pivot table rows
  2. SUMX(ALL(Transactions), ...) was calculating the same total for every row
  3. The measures weren't respecting the individual item codes (A1, B1, C1, etc.)

Key Changes Made

  1. Removed ALL(Transactions) - Now measures respect the pivot table context
  2. Used SUM() instead of SUMX() - More efficient for simple aggregations
  3. Used CALCULATE() with filters - Properly filters by transaction type while maintaining row context
  4. Kept ALL(Dates[Date]) only in accumulation - This is correct for time intelligence

Expected Behavior Now

  • Each measure will now calculate only for the specific CODE in each row
  • The accumulated measure will sum all transactions for that CODE up to the selected date
  • Individual transaction type measures will show only their respective values

Try these corrected measures and your pivot table should now show the proper running totals by item code and date as desired.

Fixed? ✓ Mark it • Share it • Help others!


Best Regards,
Jainesh Poojara | Power BI Developer

View solution in original post

6 REPLIES 6
jaineshp
Memorable Member
Memorable Member

Hey @ianburns143,

You need to create a running total inventory calculation that combines three transaction types and accumulates values over time. The formula is: Stock On Hand + Work Orders - Sales Orders = Net Inventory.

Step-by-Step Implementation

1. Data Preparation in Power Query

Combine All Transaction Tables:

  • Append all three tables (Stock, Work Orders, Sales Orders) into a single transactions table
  • Ensure consistent column structure across all tables
  • Add a multiplier column: Stock (+1), Work Orders (+1), Sales Orders (-1)


Source Tables Structure:
- TYPE | CODE | QTY | DATE
- Add calculated column: [Multiplier] = if [TYPE] = "SALES ORDER" then -1 else 1
- Final QTY = [QTY] * [Multiplier]

2. Create Date Dimension Table

Establish Proper Date Relationships:

  • Create a continuous date table covering your entire date range
  • Set earliest date as minimum date from all three source tables
  • Ensure proper date relationships for time intelligence functions

3. Power Pivot Measures Implementation

Base Inventory Measure:

 

Net Inventory =
SUMX(
ALL(Transactions),
Transactions[QTY] * Transactions[Multiplier]
)

 

Accumulated Inventory Measure:

Accumulated Net Inventory =
CALCULATE(
[Net Inventory],
FILTER(
ALL(Dates[Date]),
Dates[Date] <= MAX(Dates[Date])
)
)

4. Transaction Type Handling

Separate Calculations by Type:

  • Stock On Hand: Filter TYPE = "STOCK"
  • Inward Stock: Filter TYPE = "WORK ORDER"
  • Outward Stock: Filter TYPE = "SALES ORDER"
  • Apply appropriate positive/negative values for each type

5. Time-Based Accumulation Logic

Key Considerations:

  • Stock entries use current date (TODAY)
  • Work Orders and Sales Orders use historical transaction dates
  • Running total must consider all transactions up to the selected date
  • Multiple transactions per day per item must be properly aggregated

6. Data Model Relationships

Required Relationships:

  • Transactions table linked to Date dimension on transaction date
  • Item codes properly categorized for filtering
  • Proper cardinality settings (many-to-one from transactions to dates)

7. Expected Output Structure

Final Pivot Table Format:


CODE | DATE | STOCK | WORK ORDER | SALES ORDER | NET STOCK | ACCUMULATED NET
A1 | 31/07| 10 | 0 | 0 | 10 | 10
A1 | 01/08| 0 | 0 | 100 | -100 | -90
A1 | 02/08| 0 | 200 | 0 | 200 | 110

8. Power Query Transformation Steps

Data Consolidation Process:

  1. Load all three source tables
  2. Standardize column names and data types
  3. Add transaction type identifiers
  4. Create unified transaction table
  5. Establish proper date formatting
  6. Add calculated columns for inventory direction

9. Validation and Testing

Quality Assurance Steps:

  • Verify total quantities match source data
  • Test accumulation logic with known scenarios
  • Confirm date filtering works correctly
  • Validate negative inventory scenarios
  • Check multiple transactions per day handling

10. Performance Optimization

Efficiency Considerations:

  • Use proper indexing on date and code columns
  • Implement incremental refresh for large datasets
  • Consider partitioning by date ranges
  • Optimize DAX calculations for better performance

This solution will provide you with a robust inventory tracking system that accurately reflects accumulated net inventory positions over time, handling all your transaction types appropriately while maintaining data integrity and calculation accuracy.

Fixed? ✓ Mark it • Share it • Help others!

Best Regards,
Jainesh Poojara | Power BI Developer

Hi @jaineshpoojara 

Thank you so much for taking some time to show me this.  I have followed your steps but I am not seeing the desired output. looks like it is summing all the Qty's across the dataset ,

 

Power Pivot Table below , with settings and what the desired output should look like

 

 

ianburns143_2-1754114753569.png

 

 

 

 

Here are the Steps followed

 

Source Tables

1/ I have alligned all the Fields across all 3 tables to have matching attributes

2/ I have created the date table (date only) running 1st day of my dataset to the last date

3/ In all 3 tables I have grouped all the Qty's having the same dates

 

Transaction Table

 

1/ Append all 3 tables together -  output below 

2/ Created the Calculated Multiplier  and Final Qty columns as you requested

 

transaction table output

ianburns143_0-1754114036480.png

 

Power Pivot

 

1/Date table and transaction table loaded into Data Model and relationship established between dates

  

ianburns143_1-1754114300156.png

 

2/ I have created the following measures in the Transaction table

 

a)  Net Inventory:=SUMX(ALL(Transactions),Transactions[QTY] * Transactions[Multiplier])

b) Accumulated Net Inventory:=CALCULATE([Net Inventory],FILTER(ALL(Dates[Date]),Dates[Date] <= MAX(Dates[Date])))

c) STOCK:=SUMX(FILTER(Transactions,Transactions[TYPE] = "STOCK" ),Transactions[FINAL QTY])

d)WORK ORDER:=SUMX(FILTER(Transactions,Transactions[TYPE] = "WORK ORDER" ),Transactions[FINAL QTY])

e)SALES ORDER:=SUMX(FILTER(Transactions,Transactions[TYPE] = "SALES ORDER" ),Transactions[FINAL QTY])

 

I am probally missing something simple, but if you could advise it would be greatly appreciated

 

 

 

 

 

 

Hey @ianburns143,

Looking at your issue, I can see the problem. Your measures are summing ALL transactions across the entire dataset instead of respecting the pivot table's row context (CODE and DATE). The ALL(Transactions) function is removing all filters, which is why you're getting totals across everything.

Here's the alternate approach:

Replace your current measures with these corrected versions:

1. Net Inventory (Fixed):

Net Inventory = SUM(Transactions[FINAL QTY])

2. Accumulated Net Inventory (Fixed):

Accumulated Net Inventory =
CALCULATE(
SUM(Transactions[FINAL QTY]),
FILTER(
ALL(Dates[Date]),
Dates[Date] <= MAX(Dates[Date])
)
)

3. Individual Transaction Types (Fixed):

STOCK =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "STOCK"
)

WORK ORDER =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "WORK ORDER"
)

SALES ORDER =
CALCULATE(
SUM(Transactions[FINAL QTY]),
Transactions[TYPE] = "SALES ORDER"
)

Why Your Original Measures Weren't Working

  1. ALL(Transactions) removes all filters from the Transactions table, including the CODE filter from your pivot table rows
  2. SUMX(ALL(Transactions), ...) was calculating the same total for every row
  3. The measures weren't respecting the individual item codes (A1, B1, C1, etc.)

Key Changes Made

  1. Removed ALL(Transactions) - Now measures respect the pivot table context
  2. Used SUM() instead of SUMX() - More efficient for simple aggregations
  3. Used CALCULATE() with filters - Properly filters by transaction type while maintaining row context
  4. Kept ALL(Dates[Date]) only in accumulation - This is correct for time intelligence

Expected Behavior Now

  • Each measure will now calculate only for the specific CODE in each row
  • The accumulated measure will sum all transactions for that CODE up to the selected date
  • Individual transaction type measures will show only their respective values

Try these corrected measures and your pivot table should now show the proper running totals by item code and date as desired.

Fixed? ✓ Mark it • Share it • Help others!


Best Regards,
Jainesh Poojara | Power BI Developer

@jaineshpoojara 

 

Hi jainesh, this is perfect thank you so much for the solution, but more importantly thank you for giving me an insight into the actual coding and why it wasn't working.... very insightful!

Hey @ianburns143,

Thank you for the kind recognition - always happy to contribute!

Fixed? ✓ Mark it as solution • Give Kuddos • Help others!


Best Regards,
Jainesh Poojara | Power BI Developer

ianburns143
New Member

Sorry about the formating on the tables , didn't pull through as I expected , On the output should look like this

 

ianburns143_0-1754048164779.png

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors