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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Calculate a Rolling Sum by Fiscal Period

Hi!

 

I have a dataset that measures transaction quantities by product and business unit. I want to add a custom column to my report that shows the "Opening Quantity" for fiscal period where the Opening Quantity is equal to my Cumulative Quantity of a specific product plus the sum of period quantities preceding period x. For example, for Period = 3 the Opening Quantity would be Cumulative Quantity + Period 1 Net Qty + Period 2 Net Qty. I'm fairly new to Power BI so I am struggling with writing a measure that can give me the expected result.

 

Conditions for the quantities to roll up include:

-Must be of the same business unit

-Must be of the same item number

-Must be of the same century and fiscal year

-Do not roll up if period is equal to or greater than the current period

 

See the below sample:

Business UnitItem NumberCenturyFiscal YearPeriodCumulative QuantityNet QuantityOpening Quantity 
202066510110002020110001001000@Period=1; Opening Qty = Cumulative Qty
202066510110002020210002001100@Period=2; Opening Qty = Cumulative Qty + Period 1 Net Qty
202066510110002020310003001300@Period=3; Opening = Cumulative + Period 1 Net + Period 2 Net
202066510110002020410004001600@Period=4; Opening = Cumulative + Period 1 Net + Period 2 Net + Period 3 Net + Period 4 Net
202066510110002020510005002000@Period=4; Similarly Σp_5 = Cumulative + p_1 + p_2 + … + p_5
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

@Anonymous , you might try

Opening Qty = 
SUMX (
    DISTINCT ( Table1[Business Unit] ),
    SUMX (
        DISTINCT ( Table1[Item Number] ),
        VAR __p = MAX ( Table1[Period] )
        RETURN
            CALCULATE ( SUM ( Table1[Net Quantity] ), Table1[Period] < __p )
                + MAX ( Table1[Cumulative Quantity] )
    )
)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

@Anonymous , you might try

Opening Qty = 
SUMX (
    DISTINCT ( Table1[Business Unit] ),
    SUMX (
        DISTINCT ( Table1[Item Number] ),
        VAR __p = MAX ( Table1[Period] )
        RETURN
            CALCULATE ( SUM ( Table1[Net Quantity] ), Table1[Period] < __p )
                + MAX ( Table1[Cumulative Quantity] )
    )
)

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Thank you very much, your code worked exactly as intended!

amitchandak
Super User
Super User

@Anonymous 

Create a new table with Century Fiscal Year Period
Create a key in that table(Say Date) and fact table(You shared)

new columns
key = [Century]*10000 [Fiscal Year]*100 + [Period]
month Rank = RANKX(all('Date'),'Date'[key],,ASC,Dense)


New Measures
Cumm This Month = CALCULATE(sum('Table'[Cumulative Quantity]), FILTER(ALL('Date'),'Date'[month Rank]=max('Date'[month Rank]) ))

 

Net Quantity till last month = CALCULATE(sum('Table'[Net Quantity]), FILTER(ALL('Date'),'Date'[Fiscal Year]=max('Date'[Fiscal Year]) && 'Date'[month Rank] < Max('Date'[month Rank])))

 

Opening Quantity = [Cumm This Month] + [Net Quantity till last month ]

 

one more variation of new without year

 

Net Quantity till last month = CALCULATE(sum('Table'[Net Quantity]), FILTER(ALL('Date'), 'Date'[month Rank] < Max('Date'[month Rank])))

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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.