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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Dayna
Helper V
Helper V

Iterating over records to calculate totals

Hello,

 

I have a table that provides a record when a value has increased, or decreased. For example:

DateProductTypeValue
01/07/2022ItemXSupply100
03/07/2022ItemXDemand200
06/07/2022ItemXSupply150
02/07/2022ItemYSupply250
05/07/2022ItemYSupply100
06/07/2022ItemYDemand175

 

What I'll do is join the current inventory we have in stock to give me my starting number, for example:

DateProductTypeValueStock Value
30/06/2022ItemXInventory 200
30/06/2022ItemYInventory 300
01/07/2022ItemXSupply100 
03/07/2022ItemXDemand200 
06/07/2022ItemXSupply150 
02/07/2022ItemYSupply250 
05/07/2022ItemYSupply100 
06/07/2022ItemYDemand175 

 

What I then need to do, is iterate over the records to get a running total of what we have, but also fill in the days that are blank.

 

In my example, my records would then look like this:

DateProductTypeValueStock ValueDaily Total
30/06/2022ItemXInventory 200200
30/06/2022ItemYInventory 300300
01/07/2022ItemXSupply100 300
02/07/2022ItemX   300
03/07/2022ItemXDemand200 100
04/07/2022ItemX   100
05/07/2022ItemX   100
06/07/2022ItemXSupply150 250
01/07/2022ItemY   300
02/07/2022ItemYSupply250 550
03/07/2022ItemY   550
04/07/2022ItemY   550
05/07/2022ItemYSupply100 650
06/07/2022ItemYDemand175 475

 

So what I've done is start with my inventory value, and where I have a supply record, I've added this to the running total, and where I have demand, I've subtracted it. If there's no changes in the day, I've used the previous value.

 

I did this in QlikView, but I have no idea where to start in PowerBI, whether I prep it in data view (i.e. grouping all my records and joining the inventory), but then how do I iterate over each record?

 

Any advice you can offer would be very much appreciated.

 

Many thanks,

Dayna

1 ACCEPTED SOLUTION
PaulDBrown
Community Champion
Community Champion

I suggest you model your data by creating separate tables for inventory, demand and supply:

from:

OrignalOrignalTo this for each typeTo this for each type

Create a dimension table for the products and a date table, and create the relationships between the corresponding fields:

model.png

 Use the fields from the dimension tables in measures, visuals, filters, slicers...

Next create simple sum measures for each type and this final measure to get the Daily total:

Value Daily total =
VAR RunningInventory =
    CALCULATE (
        [Sum inventory],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
VAR RunningSupply =
    CALCULATE (
        [Sum Supply],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
VAR RunningDemand =
    CALCULATE (
        [Sum Demand],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
RETURN
    RunningInventory + RunningSupply - RunningDemand

To get

result.png

 

I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

2 REPLIES 2
Dayna
Helper V
Helper V

Thank you so much for your help and steps, that's amazing - thanks again!

PaulDBrown
Community Champion
Community Champion

I suggest you model your data by creating separate tables for inventory, demand and supply:

from:

OrignalOrignalTo this for each typeTo this for each type

Create a dimension table for the products and a date table, and create the relationships between the corresponding fields:

model.png

 Use the fields from the dimension tables in measures, visuals, filters, slicers...

Next create simple sum measures for each type and this final measure to get the Daily total:

Value Daily total =
VAR RunningInventory =
    CALCULATE (
        [Sum inventory],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
VAR RunningSupply =
    CALCULATE (
        [Sum Supply],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
VAR RunningDemand =
    CALCULATE (
        [Sum Demand],
        FILTER (
            ALL ( 'Date Table' ),
            'Date Table'[Date] <= MAX ( 'Date Table'[Date] )
        )
    )
RETURN
    RunningInventory + RunningSupply - RunningDemand

To get

result.png

 

I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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
Top Kudoed Authors