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
Raphaël
Regular Visitor

Cumulative sum by dates by persons

Dear all, 

I am trying to build a cumulative sum by date and by person but can't figure out how to do that. The only thing I suceed to do is a simple cumulative sum by date.

 

Let me explain with an example.

 

Here is my dataset :

DatePersonsIndicator
01/02/2017Mick1
01/02/2017Mick1
01/02/2017Mick1
01/02/2017David1
02/02/2017David1
03/02/2017David1
03/02/2017Mick1

 

With below formula here is what I get :

Cumul = 
CALCULATE (
    SUM ('Query'[Indicator]),
    FILTER (ALL ('Query' ),'Query'[Date] <= MAX ( 'Query'[Date]))
)
DatePersonsIndicatorCumul
01/02/2017Mick11
01/02/2017Mick12
01/02/2017Mick13
01/02/2017David14
02/02/2017David15
03/02/2017David16
03/02/2017Mick17

 

But what I want is :

DatePersonsIndicatorCumul
01/02/2017Mick11
01/02/2017Mick12
01/02/2017Mick13
01/02/2017David11
02/02/2017David12
03/02/2017David13
03/02/2017Mick14

 

I have an other question,

I've found the Cumul query on this forum but I didn't exatcly understand how it's working and how the filer part is working, can someone explain it to me ?

 

Thank you in advance for your help.

I wish you a nice day.

1 ACCEPTED SOLUTION

Hi @Raphaël

 

I think you are right to ask not only the answer but also the logic running behind DAX !

 

I guess 'Persons' column also comes from your 'Query' Table, right ? In that case, try:

Cumul = 
CALCULATE (
    SUM ('Query'[Indicator]),
    FILTER (ALL ('Query'[Date] ),'Query'[Date] <= MAX ( 'Query'[Date]))
)

 

Filter (Table, filter...) is an iterator function. It iterates over each row of the Table (1st argument) in the current filter context and evaluates the filter condition.

And here is the explanation in your case:

 

The ALL function basically ignores the current filter context.

All(Table) ignores any filters which would come from this 'Table'. This is why your formula isn't computing what you want here.

 

By writing All(Query), you are ignoring the filter coming from date (which is right here because you want to do a running total) but also the filter coming from 'Persons' (which is NOT what you want here) or any other columns from 'Query' Table that you would potentially bring in your pivot table/slicers, etc.

 

All accepts a Table or a column as an argument.

My suggestion is based on All(Query[Date]) which allows you to ignore the filter context only coming from [Date] column of Query Table.

View solution in original post

6 REPLIES 6

Hi @Raphaël

 

I think you are right to ask not only the answer but also the logic running behind DAX !

 

I guess 'Persons' column also comes from your 'Query' Table, right ? In that case, try:

Cumul = 
CALCULATE (
    SUM ('Query'[Indicator]),
    FILTER (ALL ('Query'[Date] ),'Query'[Date] <= MAX ( 'Query'[Date]))
)

 

Filter (Table, filter...) is an iterator function. It iterates over each row of the Table (1st argument) in the current filter context and evaluates the filter condition.

And here is the explanation in your case:

 

The ALL function basically ignores the current filter context.

All(Table) ignores any filters which would come from this 'Table'. This is why your formula isn't computing what you want here.

 

By writing All(Query), you are ignoring the filter coming from date (which is right here because you want to do a running total) but also the filter coming from 'Persons' (which is NOT what you want here) or any other columns from 'Query' Table that you would potentially bring in your pivot table/slicers, etc.

 

All accepts a Table or a column as an argument.

My suggestion is based on All(Query[Date]) which allows you to ignore the filter context only coming from [Date] column of Query Table.

Hi @Datatouille,

Thank you for your feedback. I confirm your proposition is the right one and I thank you for this.

 

Thank you also for your explanation,

I didn't have enought time to read entirely the link about row & filter context (which is interesting) but I'll continue to read it tomorrow and ask you more questions about it Smiley Happy

 

I've also noticed you're french (as I am), may we continue in french (not sure this is allowed here) to ensure to myself a better understanding ?

Hi again  @Datatouille,

I've read the entire article and now understand the meaning of row & context filter.

 

So I've understood the following from below formual :

FILTER (ALL ('Query'[Date] ),'Query'[Date] <= MAX ( 'Query'[Date]))

 

ALL ('Query'[Date] ) : will bring me all [Date] from 'Query' depending on the filter context (i.e. the row where the value is calculated, so here, one person).

 

This part, I still not get it :

'Query'[Date] <= MAX ( 'Query'[Date])

 

I know it does the work properly but

how power bi knows from which date to which date filter my data to give me the running count I'm expecting ?

@Raphaël

 

So, As I mentioned earlier, at this stage, the Filter function is iterating over each row of 'Query' Table in the current filter context.

This part:

'Query'[Date] <= MAX ( 'Query'[Date])

'Query[Date]' uses the row context generated by Filter. You compare each date (visible in the current filter context) to MAX('Query'[Date]).

 

But what is MAX('Query'[Date]) doing ? It is computing the maximum date in the current filter context.

So in your case, your Max('Query'[Date] ) is always equal to the date in your pivot table. Filter only keeps all the dates (currently visible in the filter context) which are less or equal to this MaxDate.

 

But be careful, if you have several years in your model, this calculation will compute 'cumul' from the very beginning of the calendar. If you want to perform a genuine Year To Date calculations.

You can use this pattern:

1. Calculate ( [Measure] , DatesYTD ( Calendar[Date] )

or

2. TotalYTD ( [Measure] , Calendar[Date] ) 

 

I encourage you to use 1. because you can change the year end date with the 2nd (optional) parameter of DatesYTD function (very convenient when you are not computing values on calendar but fiscal years for example) and it materialises the calculate !

 

Anonymous
Not applicable


Hi,
I am looking for Cumulative comparision year over year.
I have Monthly data from Dec 2016 to Nov 2018, I want to compare Last year cumulatives to this year cumulatives.
However the Measure below is calculating for the entire two year period.
Cumulative Amount Paid Measure =
CALCULATE (
    SUM ( DuesPayment[Paid Amt] ),
    FILTER (
        ALLSELECTED('DuesPayment' ),
        'DuesPayment'[Weeks Out] <= MAX ( DuesPayment[Weeks Out] )
Can you explain how this works:
1. Calculate ( [Measure] , DatesYTD ( Calendar[Date] )
or
2. TotalYTD ( [Measure] , Calendar[Date] )

 

Thanks a lot,

VG

Yes sure, I just sent you a private message !

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.