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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
MatevzP
Helper I
Helper I

Rolling sum of date ranges (recurring costs with start & end date)

Hello,

I did a simple rolling sum for invoices, but that was easy because i have a single date for each invoice. 
It looks something like this 

SUM Rolling Costs to date =
CALCULATE( [SUM Cost Invoice],
FILTER(ALLSELECTED(mp_projectcost),
mp_projectcost[Date Issued] <= MAX(mp_projectcost[Date Issued])))
 
But now i'm facing a challange where i have to calculate a rolling (cumulative) sum for date ranges.
We have projects that run in parallel. With the Start and End date i get number of working days and dividing project budget by working days i get an average daily cost of a project.

Example from gantchart
MatevzP_0-1715760140158.png

 

How would i use start & end date of projects with daily average cost of each and calculate a rolling sum?

As of now, this is all i have:

MatevzP_1-1715760215468.png

 

This issue is well beyond me.
Help, please 😩

1 ACCEPTED SOLUTION

Hi @MatevzP ,

 

Sorry, I misunderstood your needs and it turns out that there is also a need for cumulative.

 

You can try below cumulative measure.

 

# Option 1

 

xifeng_L_0-1715782622390.png

 

 

Rolling cost 1 = 
IF([daily average cost]<>BLANK(),
    CALCULATE(
        [daily average cost],
        FILTER(
            ALLSELECTED('Calendar'),
            'Calendar'[Date] <= MAX('Calendar'[Date])
        )
    )
)

// Option 1 , This method may has flaws, such as if the value of a date in the middle is empty, that date will be ignored

 

 

 

# Option 2

 

xifeng_L_2-1715782762735.png

 

 

Rolling cost 2 = 
VAR TempTable = 
    CALCULATETABLE(
        ADDCOLUMNS(VALUES('Calendar'[Date]),"Daily Cost",[daily average cost]),
        ALLSELECTED()
    )
VAR LatestDate = MAXX(FILTER(TempTable,[Daily Cost]<>BLANK()),'Calendar'[Date])
VAR CurDate = MAX('Calendar'[Date])
RETURN
IF(CurDate<=LatestDate,
    SUMX(
        FILTER(TempTable,'Calendar'[Date]<=CurDate),
        [Daily Cost]
    )
)

 

 

 

Demo - Rolling sum of date ranges.pbix

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

 

 

 

View solution in original post

9 REPLIES 9
xifeng_L
Super User
Super User

Hi @MatevzP ,

 

What is your expected result? And can provide some sample data or pbix file?

Hi,

I would like to create an S-curve (primitive one). So everyday, i'd like to sum up everage daily costs of all projects ("PO Budget per day" calculation from the screen shot). 

 

I don't have sample data as it's confidential, but if needed, i could create something.

Hi @MatevzP ,

 

I modelled some of the data myself, not sure if I can reproduce your scenario but you can refer to it.

 

xifeng_L_0-1715763871092.png

 

Demo - Rolling sum of date ranges.pbix

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

@xifeng_L 
Firstly, thank you for your effort and submitted proposal! Appreciate it very much!

I'm almost there. What you coded is a great calculation that gives me daily consumption. I think it doesn't calculate a rolling sum. I managed to get it with additional DAX formula, but the last hurdle i have is, i would like the chart to stop at today (i have created a new table with calendar up-to-today). As of now, the chart goes on even if no data is inside. I would like to use my dynamic calendar table that goes up to today, or that the rolling sum function (line chart) stops with the last available data, even better...

MatevzP_0-1715776612438.png

Demo - Rolling sum of date ranges.pbix



Could you help me out with that as well? 🙂

Thank you!

In the meantime, i'll use a rimitive solution and put a date filter on the report, so we'll adjust manually the range.
It would be nice to have DAX do it for us automatically, but that will work as well.

MatevzP_0-1715778045053.png

 

Hi @MatevzP ,

 

Sorry, I misunderstood your needs and it turns out that there is also a need for cumulative.

 

You can try below cumulative measure.

 

# Option 1

 

xifeng_L_0-1715782622390.png

 

 

Rolling cost 1 = 
IF([daily average cost]<>BLANK(),
    CALCULATE(
        [daily average cost],
        FILTER(
            ALLSELECTED('Calendar'),
            'Calendar'[Date] <= MAX('Calendar'[Date])
        )
    )
)

// Option 1 , This method may has flaws, such as if the value of a date in the middle is empty, that date will be ignored

 

 

 

# Option 2

 

xifeng_L_2-1715782762735.png

 

 

Rolling cost 2 = 
VAR TempTable = 
    CALCULATETABLE(
        ADDCOLUMNS(VALUES('Calendar'[Date]),"Daily Cost",[daily average cost]),
        ALLSELECTED()
    )
VAR LatestDate = MAXX(FILTER(TempTable,[Daily Cost]<>BLANK()),'Calendar'[Date])
VAR CurDate = MAX('Calendar'[Date])
RETURN
IF(CurDate<=LatestDate,
    SUMX(
        FILTER(TempTable,'Calendar'[Date]<=CurDate),
        [Daily Cost]
    )
)

 

 

 

Demo - Rolling sum of date ranges.pbix

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

 

 

 

OK, @xifeng_L , we are almost there, 90% what was desired.
I am sorry for nagging you so much. 

But we have a discrepancy with the calculation. We have omitted weekends when calculating "Average daily cost", but we display weekends in linechart and they are being accounted for in Rolling cost dax, hence the rolling (cumulative) cost is not matching the actuall SUM of all costs. 

MatevzP_0-1715785058471.png

I altered by using DATEDIFF function, that way we count also weekends for calculation, but a preferable way would be to also omitt weekends from the rolling calculation.

What i have so far is a column that denotes if it's a weekend or not. But i don't know how to incorporate to your "rolling cost2" dax code 😬

MatevzP_0-1715785710812.png

 



Is that someth that is easily acchieved? If it's not an easy one, counted weekends will also work.
Thank you so far!

Nevermid. No dax wizardry needed! I simply filter the page with "isWeekday"...!

Thank you so much for your help! 

😊

Good idea 👍

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.