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
Anonymous
Not applicable

How to get the the sum of previous month values in DAX

Hi All,

Can you please help me to achieve a logic to calculate the Final Value(column) logic by using DAX .

My case is to have "0' for January 2022 and for Feb the value should be Jan 2022 and For April the value should be sum(Jan,feb,Mar) and for May it should be sum(Jan,Feb,mar,Apr).

I am creating a measure , For which  I have created "start range" and "End Range" measures to get this Final Value column to be created. Please help

MonthStart RangeEndRangeAttritionFinal Value
1/1/221/1/221/31/22100
2/1/221/1/221/31/222010
3/1/221/1/222/28/223030
4/1/221/1/223/31/224060
5/1/221/1/224/30/2250100
6/1/221/1/225/31/2260150
7/1/221/1/226/30/2270200

 

Thanks in advance,

1 ACCEPTED SOLUTION
Pragati_Sharma
Frequent Visitor

Hello, 
You can use the following measure to calculate rolling sum for previous months. 

Find Value =
IF(SELECTEDVALUE('Table'[Month]) = 1, 0,
CALCULATE(
SUM('Table'[Attrition]),
FILTER(
ALLSELECTED('Table'[Date]),
'Table'[Date]< MAX('Table'[Date]))
))

Output:
Month = Month(Table[Date])
Pragati_Sharma_0-1659699378226.png

 

 

View solution in original post

7 REPLIES 7
OliT
Resolver I
Resolver I

Hi @Anonymous 

Please try this measure

 

measure =
SUMX (
    FILTER ( ALL ( 'table' ), 'table'[MonthStart] < MAX ( 'table'[MonthStart] ) ),
    [Attrition]
)

 

 

Regards,

OliT

Pragati_Sharma
Frequent Visitor

Hello, 
You can use the following measure to calculate rolling sum for previous months. 

Find Value =
IF(SELECTEDVALUE('Table'[Month]) = 1, 0,
CALCULATE(
SUM('Table'[Attrition]),
FILTER(
ALLSELECTED('Table'[Date]),
'Table'[Date]< MAX('Table'[Date]))
))

Output:
Month = Month(Table[Date])
Pragati_Sharma_0-1659699378226.png

 

 
Anonymous
Not applicable

Hi @Pragati_Sharma 

Thanks for the quick reply,

Unfortunately I cannot use "Attrition" in the SUM because  it was derived using the below logic measures. Please help me how to crack this.

 

Attrition = CALCULATE (
DISTINCTCOUNT( employee[employee_code] ),
  employee[Attrition filter] = "yes"
)
 
Attrition filter =
var monthkey = year([Month])&FORMAT(month([Month]),"00")
var lwdmmyy= year([LWD])&FORMAT(month([LWD]),"00")
return
IF( lwdmmyy=monthkey, "yes","No")
 
Thanks,
Anonymous
Not applicable

@ddpl - Hi, Looking for your reply/ any alternate solution. Thank you in advance

ddpl
Solution Sage
Solution Sage

@Anonymous ,Hey bro

 

You don't need other column to get running total like Start or End Range

Just create a measure as below and put it along with Date and Attrition in Table...

 

Your Need = 

VAR need = CALCULATE(SUM('Table'[Attrition]), FILTER(ALLSELECTED('Table'), 'Table'[Date]<MAX('Table'[Date])))

RETURN IF(need=BLANK(), 0, need)
Anonymous
Not applicable

Hi @ddpl ,

Thanks for the quick reply,

Unfortunately I cannot use "Attrition" in the SUM because  it was derived using the below logic measures. Please help me how to crack this.

 

Attrition = CALCULATE (
DISTINCTCOUNT( employee[employee_code] ),
  employee[Attrition filter] = "yes"
)
 
Attrition filter =
var monthkey = year([Month])&FORMAT(month([Month]),"00")
var lwdmmyy= year([LWD])&FORMAT(month([LWD]),"00")
return
IF( lwdmmyy=monthkey, "yes","No")
 
Also, what is  need in the below line of your code:
RETURN IF(need=BLANK(), 0, need)
Thanks
 
amitchandak
Super User
Super User

@Anonymous , You can join date with date table and can use time intelligence

 

MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))


last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Sales[Sales Amount]),previousmonth('Date'[Date]))

 

Power BI — Month on Month with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
https://www.youtube.com/watch?v=6LUBbvcxtKA

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.