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 write a dax to Get cumulative sum till 6th row and after 6th row rolling 6 months sum ?

HI Team,

I've requirement to calculate two different conditions:

1. To calculate cummulative sum till 6th row

2. To calculate Rollling 6 months sum (After 6 th row) 

 

Reference Link for PBI:     https://1drv.ms/u/s!Au-aOkl1BoHuhytgh_iqEh6UG4WA?e=IwUWDN

 

Formula:

Formula with Data.PNG

 

Reference Link for PBI:     https://1drv.ms/u/s!Au-aOkl1BoHuhytgh_iqEh6UG4WA?e=IwUWDN

 

Please let me know if you need anything

 

Thanks In Advance

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I suggest having a Dim-Date table and using the time intelligence function in DAX to create a measure.

However, if you do not have a Dim-Date table, please check the below picture and the attached pbix file.

Create a calculated column in the table, that is the Month Number CC, and then create measures like below.

Picture1.png

 

NS total: =
SUM( Sheet3[NS] )
 
NS 6 months rolling: =
VAR currentyearmonth_sortID =
MAX ( Sheet3[Year] ) * 12
+ MAX ( Sheet3[Month Number CC] )
VAR newtable =
ADDCOLUMNS (
ALL ( Sheet3 ),
"@NSTotal", [NS total:],
"@sortID",
CALCULATE ( MAX ( Sheet3[Year] ) * 12 + MAX ( Sheet3[Month Number CC] ) )
)
VAR filternewtable =
FILTER (
newtable,
[@sortID] >= currentyearmonth_sortID - 5
&& [@sortID] <= currentyearmonth_sortID
)
RETURN
SUMX ( filternewtable, [@NSTotal] )
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi,

I suggest having a Dim-Date table and using the time intelligence function in DAX to create a measure.

However, if you do not have a Dim-Date table, please check the below picture and the attached pbix file.

Create a calculated column in the table, that is the Month Number CC, and then create measures like below.

Picture1.png

 

NS total: =
SUM( Sheet3[NS] )
 
NS 6 months rolling: =
VAR currentyearmonth_sortID =
MAX ( Sheet3[Year] ) * 12
+ MAX ( Sheet3[Month Number CC] )
VAR newtable =
ADDCOLUMNS (
ALL ( Sheet3 ),
"@NSTotal", [NS total:],
"@sortID",
CALCULATE ( MAX ( Sheet3[Year] ) * 12 + MAX ( Sheet3[Month Number CC] ) )
)
VAR filternewtable =
FILTER (
newtable,
[@sortID] >= currentyearmonth_sortID - 5
&& [@sortID] <= currentyearmonth_sortID
)
RETURN
SUMX ( filternewtable, [@NSTotal] )
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Vera_33
Resident Rockstar
Resident Rockstar

Hi @Anonymous 

 

Do you need a DAX Calculated column? And the logic is about June and differentiate years?

Vera_33_0-1637714748129.png

Cum_NS = 
VAR CurMonth =  MONTH(Sheet3[Date])
RETURN
IF(CurMonth<7, SUMX(FILTER(Sheet3,Sheet3[Year]=EARLIER(Sheet3[Year])&&MONTH(Sheet3[Date])<=CurMonth),Sheet3[NS]),
SUMX(FILTER(Sheet3,Sheet3[Year]=EARLIER(Sheet3[Year])&&MONTH(Sheet3[Date])<=CurMonth&&MONTH(Sheet3[Date])>=CurMonth-6),Sheet3[NS]))

 

Anonymous
Not applicable

Hi @Vera_33 ,

 

Thanks for the response. Yes we need calculated column and need some changes in formula

In the formula need changes.

1. We don't need to differentiate years means starting from the 7th row in table same logic shiould be applied to till the last .

 

For example:  in the current formula, from every every year stating month it will be recalculated . 

Expected : It shouldn't be recalculated and from 7th row it should be  be rolling 6 months sum irrespective of the year.

 

every year.PNG

 

2. Logic is not related on June month and it should be related on the rownumber 

means if the data start from jun 2018 to 2021 nov then the first 6  rows should be cummulative after that it should be last 6 months rolling sum.

June.PNG

 

Please let me  know if you need any details

Thanks In Advance

Hi @Anonymous 

 

So does the table sort by date? We can add the Row number based on date?

Vera_33_0-1637731459244.png

CurRow = COUNTROWS(FILTER(Sheet3,Sheet3[Date]<=EARLIER(Sheet3[Date])))


Cum_NS = 
VAR CurR=Sheet3[CurRow]
RETURN
IF([CurRow]<7, SUMX(FILTER(Sheet3,Sheet3[CurRow]<=CurR),Sheet3[NS]),
SUMX(FILTER(Sheet3,Sheet3[CurRow]>=CurR-6&&Sheet3[CurRow]<=CurR),Sheet3[NS]))

Otherwise you need to add Index column as Row number via Power Query

Vera_33_1-1637731532079.png

Please also consider the size and the suggestion from @Jihwan_Kim 

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