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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.