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
gelsonwj
Helper I
Helper I

calculate the accumulated

gelsonwj_1-1671802722605.png

I created a measurement that averages the amount of rain in a day on a farm, now I would like to do the cumulative calculation, that is, the value of the next line is the sum of the previous lines

1 ACCEPTED SOLUTION
AlB
Super User
Super User

@gelsonwj 

Your date_short column was of type text.  It needs to be of type date. That is why my first version was failing. It will work if you use this to create that column instead of the LEFT([date], 9) you had:

 

Date_short = DATEVALUE([date])

 

I have created another version that wil work with the months. Note that I have gotten rid of the date hierarchy (which I would strongly discourage as it can be the hidden cause of many problems)  and created a month column. It would be better to create a Year-Month column and better still to use a date table.

 

teste26/12 V2 = 
VAR currentDate_ = MAX ( 'coperplan01 pluviometer_monitoring_list'[Date_short] )
VAR dates_ =
    FILTER (
        CALCULATETABLE (
            DISTINCT ( 'coperplan01 pluviometer_monitoring_list'[Date_short] ),
            ALLSELECTED (
                'coperplan01 pluviometer_monitoring_list'[Date_short],
                'coperplan01 pluviometer_monitoring_list'[Month]
            )
        ),
        [Date_short] <= currentDate_
    )
RETURN
    CALCULATE (
        SUMX ( dates_, [Chuva Acumulada_] ),
        ALLSELECTED (
            'coperplan01 pluviometer_monitoring_list'[Date_short],
            'coperplan01 pluviometer_monitoring_list'[Month]
        )
    )

 

See it all at work in the attached file (Page 3)

 


SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

   

View solution in original post

13 REPLIES 13
AlB
Super User
Super User

@gelsonwj 

Your date_short column was of type text.  It needs to be of type date. That is why my first version was failing. It will work if you use this to create that column instead of the LEFT([date], 9) you had:

 

Date_short = DATEVALUE([date])

 

I have created another version that wil work with the months. Note that I have gotten rid of the date hierarchy (which I would strongly discourage as it can be the hidden cause of many problems)  and created a month column. It would be better to create a Year-Month column and better still to use a date table.

 

teste26/12 V2 = 
VAR currentDate_ = MAX ( 'coperplan01 pluviometer_monitoring_list'[Date_short] )
VAR dates_ =
    FILTER (
        CALCULATETABLE (
            DISTINCT ( 'coperplan01 pluviometer_monitoring_list'[Date_short] ),
            ALLSELECTED (
                'coperplan01 pluviometer_monitoring_list'[Date_short],
                'coperplan01 pluviometer_monitoring_list'[Month]
            )
        ),
        [Date_short] <= currentDate_
    )
RETURN
    CALCULATE (
        SUMX ( dates_, [Chuva Acumulada_] ),
        ALLSELECTED (
            'coperplan01 pluviometer_monitoring_list'[Date_short],
            'coperplan01 pluviometer_monitoring_list'[Month]
        )
    )

 

See it all at work in the attached file (Page 3)

 


SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

   

You are my hero

FreemanZ
Super User
Super User

hi  @gelsonwj 

 

try to create a measure like this:

YTD =
VAR _date  = MAX(TableName[Date_short])
RETURN
CALCULATE(
    SUM(TableName[Chuva]),
    FILTER(
         ALL(TableName),
         TableName[Date_short]<=_date
    )
)

 

this doesn't work for me, because the SUM() function only accepts a table, and in my case I have a measure that calculates the accumulated rain

AlB
Super User
Super User

Hi @gelsonwj 

Can you share the code of your current measure? It can probably be modified slightly to get to the cumulative.

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Ok

gelsonwj_0-1671805261270.png

 

Chuva Acumulada_ =
  VAR __Table = GROUPBY('coperplan01 pluviometer_monitoring_list',[Date_short],"__Average",AVERAGEX(CURRENTGROUP(),[quantity]))
  VAR __Result = SUMX(__Table,[__Average])
RETURN
  __Result

@gelsonwj 

Try this measure that uses the one you have:

Chuva Acumulada_Cumul =
VAR currentDate_ = SELECTEDVALUE ( 'coperplan01 pluviometer_monitoring_list'[Date_short] )
VAR dates_ =
    FILTER (
        CALCULATETABLE (
            DISTINCT ( 'coperplan01 pluviometer_monitoring_list'[Date_short] ),
            ALLSELECTED ( 'coperplan01 pluviometer_monitoring_list'[Date_short] )
        ),
        [Date_short] <= currentDate_
    )
RETURN
    SUMX ( dates_, [Chuva Acumulada_] )

I suspect your [Chuva Acumulada_] measure can be simplified to a large extent, but I would need to see a sample of the  'coperplan01 pluviometer_monitoring_list' table and an example based on it to be sure

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

i would like to have a result like this 

gelsonwj_0-1672053583875.png

 

this is the table. Each farm has its ID and a farm is composed of N rain gauges. In order to calculate the accumulated rain, it is necessary to make an average of each day, of all the rain gauges of a farm X, and day N is added to day N+1... "quantity" is the amount of rain in mm

 

gelsonwj_0-1672054332234.png

 

@gelsonwj 

Does the suggestion in my previous post work?

If not I would need more details as to where it does not and some sample datga, ideally a pbix t work on

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

gelsonwj_0-1672055479102.png

Did not work. Where can I send you the .pbix file?

You'd have to share here the URL to the file hosted elsewhere: Dropbox, Onedrive... or just upload the file to a site like tinyupload.com (no sign-up required).

Or share the URL it via PM if you prefer

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

@gelsonwj

Can you paste the code in text, so that it can be copied, rather than on a screen cap?

 

SU18_powerbi_badge

 

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

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.

Top Solution Authors