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! Learn more

Reply
Crefin
Frequent Visitor

"Rolling" sum of a subgroup using the most recent record of each item in the group at a given date?

Let's say I have an inventory table of products.  Items are in categories such as "produce" and "meat" while also in subcategories like "fruit" and "vegetables".  Each record in the table is the inventory at a given date for each product.  Inventory is not done consistently where there is a record within the subcategory for each product each day.  They vary.  What is needed is the total inventory levels for each category and subcategory at the time of each transaction.

 

Here is an example table:

Item    Inventory       Date              Category        SubcategoryCategory TotalSubcategory Total
101/1/2021ProduceFruit  
201/1/2021ProduceVegetables  
301/1/2021ProduceVegetables  
2501/2/2021ProduceVegetables  
2481/5/2021ProduceVegetables  
1101/7/2021ProduceFruit  
181/8/2021ProduceFruit  
2421/8/2021ProduceVegetables  
141/10/2021ProduceFruit  
3161/12/2021ProduceVegetables  
551/18/2021MeatDeli  
3111/31/2021ProduceVegetables  
4142/1/2021MeatPrepackaged  

 

If I were to do this in a script or formula, I would do something like the following:

For category:

1) Select all records for the category, sort by date descending

2) For each product in the category, select the first record (most recent) 

3) Calculate the sum of the inventory across these most recent records

 

In the above example,

  • The Category Total for Produce on the 1/10/2021 dated record would be 4 (Product 1) + 42 (Product 2) + 0 (Product 3) = 46. 

 

  • On 1/12/2021 the number would change to 62 since the calculation would be 4 (Product 1) + 42 (Product 2) + 16 (Product 3) = 62.  The most recent record for Product C supercedes the original one at this point.

 

The same functionality would be applied at the subcategory level.  I'm just not sure how to dynamically select the most recent records within the category to perform the sum operation.  

 

Any suggestions? Many thanks for your advice.

1 ACCEPTED SOLUTION
Crefin
Frequent Visitor

Thank you! The table works perfectly. 

 

I will be using this calculation frequently across different metrics, specifically to perform weighted average calculations where the Measure2 value.  Would a calculated column be preferred? 

 

I tried converting it to a line chart to show the total over time and it seems to only show the most recent record as the "total"  (e.g. Produce shows 16 for January 22).

View solution in original post

7 REPLIES 7
smpa01
Super User
Super User

@Crefin  you need two measures for this

 

Measure1 = 
VAR _select =
    SELECTEDVALUE ( 'Calendar'[Date] )
VAR _date =
    CALCULATE (
        MAX ( 'Table 1'[Date] ),
        FILTER ( 'Table 1', 'Table 1'[Date] <= _select )
    )
VAR _sum =
    CALCULATE (
        SUM ( 'Table 1'[Inventory] ),
        FILTER ( 'Table 1', 'Table 1'[Date] = _date )
    )
RETURN
    _sum




Measure2 = 
SUMX (
    ADDCOLUMNS (
        SUMMARIZE (
            'Table 1',
            'Table 1'[Category],
            'Table 1'[Subcategory],
            'Table 1'[Item]
        ),
        "total", [Measure1]
    ),
    [total]
)

 

smpa01_0-1640667443371.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Crefin
Frequent Visitor

Thank you! The table works perfectly. 

 

I will be using this calculation frequently across different metrics, specifically to perform weighted average calculations where the Measure2 value.  Would a calculated column be preferred? 

 

I tried converting it to a line chart to show the total over time and it seems to only show the most recent record as the "total"  (e.g. Produce shows 16 for January 22).

Anonymous
Not applicable

Did you, @Crefin, or @smpa01 ever figure out how to show the values over time via a line graph rather than for a single selected value? I'm running into the same issue?

I ultimately wound up restructuring my data model to enable more out-of-the-box functionality to work.  In my case, it was to create a table containing a row for each applicable reporting date and columns showing each data point "as of" the given reporting date (which was many lookups for Max date <= report date that may be impractical depending on the volume of data).  The measures to calculate weighted averages and chart the output worked normally from there.  Good luck!

Anonymous
Not applicable

Thanks! For future reference: https://community.powerbi.com/t5/Desktop/Create-a-Line-Graph-showing-changes-to-a-portfolio-over-tim...

 

Just paying it forward if anyone else comes here. 

amitchandak
Super User
Super User

@Crefin , Create two measures

Last Inv = CALCULATE(LASTNONBLANKVALUE(Data[Date], calculate(SUM(Data[Inventory]))), FILTER(ALLSELECTED(Data), Data[Category] = max(Data[Category]) && Data[item] = MAX(Data[item])))

Total Inv = var _t = SUMMARIZE((Data), Data[Item],Data[Category]) var _tab = ADDCOLUMNS(_t,"_1",[Last Inv]) 
return sumx(_tab,[_1])

 

Use Total Inv

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi amitchandak,

thanks for your suggestion.  I was unable to produce the correct totals with this on all the dates.  On January 8 it correctly shows Produce inventory of 50.  Incrementing by 1 day, January 9 displays Produce inventory as blank.  Then, on the 10th when Fruit changes from 8 to 4 (which should result in inventory of 46), it shows inventory of 4, ignoring Item 2's inventory of 42 from January 8th.  It seems to identify the highest dated record per the slicer and sum the inventory records on that day.  In the business case here, the subcategories would not necessarily have records all on the same day.  

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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