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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TimPowerBI
Frequent Visitor

Creating a cumulative cost over time based on slicer filter

Hi all,

 

I am trying to create a graph of cumulative cost over time based on what project is selected by the user on the dashboard.

 

ProjectDateCost
A20/01/202010.00
B20/01/202020.00
A20/01/202030.00
B21/01/202010.00
A21/01/202020.00
B22/01/202020.00
A23/01/202010.00
B23/01/202020.00
A23/01/202020.00

 

I have created this dummy data for show. 

 

I have been trying to use DAX to created a cumulative cost column but the problems Ive been having are:

  • using something like CumulativeCost = CALCULATE(SUM(Sheet1[Cost]), all(Sheet1), Sheet1[Date] <= EARLIER(Sheet1[Date])) gives the right cumulative cost for both projects but not for just one.
  • Using it with a filter: 
    CumulativeCost = CALCULATE(SUM(Sheet1[Cost]), all(Sheet1), Sheet1[Date] <= EARLIER(Sheet1[Date]), Sheet1[Project] = EARLIER(Sheet1[Project]))
    creates the right sums for the individual filters and works if the project A and B are on the same day (because theyll be summed) but if they aren't then itll only show the cumulative cost for that project on that day and not total (obviously).
     
    Is there any way to have the cost summed up for each day based on the filters that are selected from the dashboard?
    I am new to DAX so all help is welcomed! 
     
     
    Thanks 
 

 

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @TimPowerBI ,

 

The solution will depend on how your data model is setup and can be achieved by using either a  column or a measure.
 Sol # 1: using a separate Date table.

Cumulative Sum Measure =
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER ( ALL ( 'Dates'[Date] ), 'Dates'[Date] <= MAX ( 'Dates'[Date] ) )
)


Sol # 2 :  no separate Dates table.

Cumulative Sum Measure =
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER ( ALL ( 'Fact'[Date] ), 'Fact'[Date] <= MAX ( 'Fact'[Date] ) )
)


 Sol # 3: as a calculated column.

Cumulative Sum Column = 
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER (
        ALL ( 'Fact' ),
        'Fact'[Date] <= EARLIER ( 'Fact'[Date] )
            && 'Fact'[Category] = EARLIER ( 'Fact'[Category] )
    )
)









Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Create a Calendar Table and build a relationship from the Date column of your Data Table to the Date column of your Calendar Table.  To your visual/filter, drag the Project column from the Data Table and select any one project.  Write these measures

Total cost = SUM(Data[Cost])

Total cost till date = Calculate([Total cost],datesbetween(Calendar[Date],minx(all(Calendar[Date]),Calendar([Date])),min(calendar[Date])))

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
danextian
Super User
Super User

Hi @TimPowerBI ,

 

The solution will depend on how your data model is setup and can be achieved by using either a  column or a measure.
 Sol # 1: using a separate Date table.

Cumulative Sum Measure =
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER ( ALL ( 'Dates'[Date] ), 'Dates'[Date] <= MAX ( 'Dates'[Date] ) )
)


Sol # 2 :  no separate Dates table.

Cumulative Sum Measure =
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER ( ALL ( 'Fact'[Date] ), 'Fact'[Date] <= MAX ( 'Fact'[Date] ) )
)


 Sol # 3: as a calculated column.

Cumulative Sum Column = 
CALCULATE (
    SUM ( 'Fact'[Column] ),
    FILTER (
        ALL ( 'Fact' ),
        'Fact'[Date] <= EARLIER ( 'Fact'[Date] )
            && 'Fact'[Category] = EARLIER ( 'Fact'[Category] )
    )
)









Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
mahoneypat
Employee
Employee

Instead of a calculated column, this is better done with a measure like the one below.  It will work in your visual whether Project is in it or not.  For example, make a matrix with date on the rows and project on the column, with this measure in the values.

 

Cumulative Cost =
VAR __maxdate =
    MAX ( Cost[Date] )
RETURN
    CALCULATE (
        SUM ( Cost[Cost] ),
        ALLSELECTED ( Cost ),
        VALUES ( Cost[Project] ),
        Cost[Date] <= __maxdate
    )

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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