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

Calculate previous year of aggregated sum

Hi,

 

I have a measure which is the cumulative sum of another measure for a selected period of time, which I calculate as below. 
How do I create a measure which calculates the previous year for the agg_buy measure based on the same selection?


TimeQuery[YearMonthDay] is a date column which contains the Year, month and day.

agg_buy = 
sumx(
    FILTER(
        ALLSELECTED(TimeQuery),
        TimeQuery[YearMonthDay] <=MAX(TimeQuery[YearMonthDay]) 
    
    ),


    [BUY] 
)

 

9 REPLIES 9
Icey
Community Support
Community Support

Hi @simber ,

 

How about this?

agg_buy = 
sumx(
    FILTER(
        ALLSELECTED(TimeQuery),
        TimeQuery[YearMonthDay] < MAX(TimeQuery[YearMonthDay]) -----------use "<" instead of "<="
    
    ),


    [BUY] 
)

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @Icey !

 

How would this aggregate the previous year? It only excludes the current selected value in the [YearMonthDay] column and doesn't aggregate the previous year.

Icey
Community Support
Community Support

Hi @simber ,

 

Sorry, some misunderstandings. If your [YearMonthDay] column is Date/DateTime type, have you tried SAMEPERIODLASTYEAR function? 

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I tried using it by replacing the max function so it looks like this:

 

agg_buy = 
sumx(
    FILTER(
        ALLSELECTED(TimeQuery),
        TimeQuery[YearMonthDay] < SAMEPERIODLASTYEAR(TimeQuery[YearMonthDay]) 
    
    ),


    [BUY] 
)

 

But all I got was a single value aggregation for the same period last year, which i can't display in a line chart.

Icey
Community Support
Community Support

Hi @simber ,

 

How about this?

 

agg_buy = 
CALCULATE( [BUY],SAMEPERIODLASTYEAR(TimeQuery[YearMonthDay]) 
    

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I doesn't work either. The [BUY] measure consists of serveral summarized columns and is not a cumulative sum. That's why the original measure looks like it does, as it creates a cumulative sum which can be displayed in a line chart.

Icey
Community Support
Community Support

Hi @simber ,

 

I create a simple sample and find that replace "ALLSELECTED" in your orginal "agg_buy" measure with "ALL" and create another measure like below:

LY = CALCULATE ( [agg_buy], SAMEPERIODLASTYEAR(TimeQuery[YearMonthDay]))

 

And get this result:

TY LY.PNG

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Your line graph looks just like I want the end result to look like but it still doesn't work.

 

I opened your pbix file and did see that your TY measure is calculated this way:

 

TY = CALCULATE(SUM('Table'[Value]),FILTER(ALL('Table'),'Table'[Date]<=MAX('Dates'[Date])))

 


But as you see in my version of TY (as agg_buy=TY) below, it in itself contains a measure. 

 

 

agg_buy =
sumx(
FILTER(
ALLSELECTED(TimeQuery),
TimeQuery[YearMonthDay] <=MAX(TimeQuery[YearMonthDay])
),
[BUY]
)

 


The measure [BUY] is used to summarize diffenerent columns depending on the selections from another slicer. It looks like this:

 

BUY = IF( HASONEVALUE('GridArea'[GridArea]),
switch(VALUES('GridArea'[GridArea]),
"SE1",(sum(trades_se1[asset1])+sum(trades_se1[asset2])),
"SE2",(sum(trades_se2[asset1])+sum(trades_se2[asset2])),
"SE3",(sum(trades_se3[asset1])+sum(trades_se3[asset2])),
"SE4",(sum(trades_se1[asset1])+sum(trades_se1[asset2])),

"Total",
(sum(trades_se1[asset1])+sum(trades_se1[asset2]))+
(sum(trades_se2[asset1])+sum(trades_se2[asset2]))+
(sum(trades_se3[asset1])+sum(trades_se3[asset2]))+
(sum(trades_se4[asset1])+sum(trades_se4[asset2]))
,
(sum(trades_se1[asset1])+sum(trades_se1[asset2]))
)

 


I also tried to replace SUM('Table'[Value]) in your measure TY with my [BUY] measure but I did not see any values when I put it in a line chart.

Icey
Community Support
Community Support

Hi @simber ,

 

Use CALCULATE as my expression has no difference between yours. It is unnecessary to change it.

 

Does your "agg_buy" measure give the expected result of this year? 

agg_buy =
sumx(
FILTER(
ALLSELECTED(TimeQuery),
TimeQuery[YearMonthDay] <=MAX(TimeQuery[YearMonthDay])
),
[BUY]
)

 

If it does, please modify "agg_buy" measure by replacing "ALLSELECTED" with "ALL", then, it will look like below. If there are some categories, use ALLEXCEPT instead of ALL.

agg_buy =
sumx(
FILTER(
ALL(TimeQuery),
TimeQuery[YearMonthDay] <=MAX(TimeQuery[YearMonthDay])
),
[BUY]
)

 

After that, create last year measure like so:

agg_buy_LY = CALCULATE ( [agg_buy], SAMEPERIODLASTYEAR(TimeQuery[YearMonthDay]))

 

Hope I explain clearly.

 

Can this work?

 

If it doesn't work, could you share us a sample without sensitive information or real data for test?

 

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.