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

Rolling sum in matrix categories with slicer

Hi,

 

I'm quite new to DAX and Power BI and I'm having a struggle getting this rolling sum to work with category in matrix using a slicer in the report. We have a month slicer in the report and this sample is for 202111 selection. In matrix we want for each month to be shown rolling 12 month sum per areaid. I have managed to get the rolling sum to work without category, but it does not work with categories. Is this even possible with DAX? I know I could do this with other reporting tools so I would assume this should be possible with DAX too but I just can't figure it out. Here is the DAX I have now.

 

 

 

ActionsRolling:=
VAR endDate = MAX(Calendar[YYYYMM])
VAR startDate = endDate - 100
VAR RollingSum =
CALCULATE(SUM(Actions[checkins]),FILTER(ALL(Actions), AND(Actions[YYYYMM] > startDate, Actions[YYYYMM] <= endDate)))
RETURN RollingSum
Actions_R12:=
VAR endDate = MAX(TimeSlicer[YYYYMM])
VAR startDate = endDate - 100
VAR MeasureR = Actions[ActionsRolling]

RETURN
IF(MIN(
Calendar[YYYYMM]) <= startDate,
BLANK(),
IF(MIN(Calendar[YYYYMM]) > endDate,
BLANK(),
MeasureR))

 

 

 

With this DAX we get the result like this. These numbers in matrix tables might not be 100% accurate as I quickly put them up in excel but you should get the idea of the problem here.

 

 202012202101202102202103202104202105202106202107202108202109202110202111
Area1141158160167175178182187191190180160
Area2141158160167175178182187191190180160
Area3141158160167175178182187191190180160
total141158160167175178182187191190180160

 

While the result should look like:

 

 202012202101202102202103202104202105202106202107202108202109202110202111
Area111109101212131415151515
Area2116135140147153159163168171173163144
Area314131110107655221
total141158160167175178182187191190180160

 

Here are the tables with joins.

image.png

Thank you in advance!

 

- Aku

1 ACCEPTED SOLUTION
v-robertq-msft
Community Support
Community Support

Hi, @Anonymous 

After my long time of research, I can finally figure out a method that can help to achieve the output you wanted to get.

In order to deal with the data loss situation in some date rows, I think the only way is to create a calculated table to fill the rows with no data as blank as the new main table to be used, you can create a table like this:

Actions1 =

var _yyyymm=SUMMARIZE(Actions,Actions[YYYYMM])

var _areaid=SUMMARIZE(Actions,Actions[Areaid])

return

CROSSJOIN(_yyyymm,_areaid)

Then create a calculated column in the new table:

Rank by areaid = RANKX(FILTER(ALL(Actions1),[Areaid]=EARLIER(Actions1[Areaid])),[YYYYMM],,ASC,Skip)

Create a slicer table like this:

Slicer = SUMMARIZE(Actions,Actions[YYYYMM])

Create a measure like this:

ActionsRolling1 =

var _selectedrank=CALCULATE(MAX('Actions1'[Rank by areaid]),FILTER(allselected(Actions1),[YYYYMM]=SELECTEDVALUE(Slicer[YYYYMM])))

var _rolling=CALCULATE(SUM(Actions1[Actions]),FILTER(ALL(Actions1),[Areaid]=MAX(Actions1[Areaid])&&[Rank by areaid]<=MAX('Actions1'[Rank by areaid])&&[Rank by areaid]>=MAX('Actions1'[Rank by areaid])-11))

return

IF(MAX('Actions1'[Rank by areaid])>=12&&MAX('Actions1'[Rank by areaid])<=_selectedrank&&MAX('Actions1'[Rank by areaid])>=_selectedrank-11,_rolling,BLANK())

 

Then you can go to the report page to place the measures and columns in the new table:

vrobertqmsft_0-1636705366673.png

 

And I think you can get what you want.

You can download my test pbix file below

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

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

View solution in original post

9 REPLIES 9
v-robertq-msft
Community Support
Community Support

Hi, @Anonymous 

After my long time of research, I can finally figure out a method that can help to achieve the output you wanted to get.

In order to deal with the data loss situation in some date rows, I think the only way is to create a calculated table to fill the rows with no data as blank as the new main table to be used, you can create a table like this:

Actions1 =

var _yyyymm=SUMMARIZE(Actions,Actions[YYYYMM])

var _areaid=SUMMARIZE(Actions,Actions[Areaid])

return

CROSSJOIN(_yyyymm,_areaid)

Then create a calculated column in the new table:

Rank by areaid = RANKX(FILTER(ALL(Actions1),[Areaid]=EARLIER(Actions1[Areaid])),[YYYYMM],,ASC,Skip)

Create a slicer table like this:

Slicer = SUMMARIZE(Actions,Actions[YYYYMM])

Create a measure like this:

ActionsRolling1 =

var _selectedrank=CALCULATE(MAX('Actions1'[Rank by areaid]),FILTER(allselected(Actions1),[YYYYMM]=SELECTEDVALUE(Slicer[YYYYMM])))

var _rolling=CALCULATE(SUM(Actions1[Actions]),FILTER(ALL(Actions1),[Areaid]=MAX(Actions1[Areaid])&&[Rank by areaid]<=MAX('Actions1'[Rank by areaid])&&[Rank by areaid]>=MAX('Actions1'[Rank by areaid])-11))

return

IF(MAX('Actions1'[Rank by areaid])>=12&&MAX('Actions1'[Rank by areaid])<=_selectedrank&&MAX('Actions1'[Rank by areaid])>=_selectedrank-11,_rolling,BLANK())

 

Then you can go to the report page to place the measures and columns in the new table:

vrobertqmsft_0-1636705366673.png

 

And I think you can get what you want.

You can download my test pbix file below

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

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

Anonymous
Not applicable

Hi,
Thank you for your time. This calculation seems to work as we wanted, though it feels little odd to use calculated table to achieve it.
-Aku

v-robertq-msft
Community Support
Community Support

Hi, 

According to your description and sample picture, I can now clearly understand your requirement, I think you can try to create a calculated table for the slicer and this measure to achieve your needs:

Create a calculate table like this:

Slicer = SUMMARIZE(Actions,Actions[YYYYMM],Actions[Rank by areaid])

Create a measure:

ActionsRolling1 =

var _rolling=CALCULATE(SUM(Actions[Actions]),FILTER(ALL(Actions),[Areaid]=MAX(Actions[Areaid])&&[Rank by areaid]<=MAX(Actions[Rank by areaid])&&[Rank by areaid]>=MAX('Actions'[Rank by areaid])-11))

var _selectedrank=CALCULATE(MAX('Actions'[Rank by areaid]),FILTER(ALL(Actions),[Areaid]=SELECTEDVALUE(Actions[Areaid])))

return

IF(MAX([Rank by areaid])>=12&&MAX([Rank by areaid])<=_selectedrank&&MAX([Rank by areaid])>=_selectedrank-11,_rolling,BLANK())

 

And you can create two slicers to place the [areaid] and [YYYYMM] to get what you want.

vrobertqmsft_0-1636510894652.png

 

You can download my test pbix file below

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

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

Anonymous
Not applicable

Hi,

 

I'm sorry but I think I was unclear with my clarification. So this is still what I need to present:

 

image.png

 

With the additional image I provided I tried to explain how the data needs to be calculated. I added another image where I highlighted with color boxes what should be calculated for each value. I'm not 100% sure if the rolling sum is accurate term for this need but my company uses it for this purpose at least. So the sum needs to be for each shown value from past 12 months, including the selected month in the 12 months. Slicer selection in this sample would of been 202109 and like said in original post there can be empty months but in calculation we don't want to skip them, their value would just be 0 for the sum, in this sample I have presented them as empty values but in our data there is no row for these months. Hopefully this clarifies the need.

 

image.png

 

- Aku

Hi, 

According to your description, you said that in your data there is no row for some months, which may cause some confusion, Would like to post some sample data which is more relevant to your true data model (without sensitive data) and your expected result.

 

How to Get Your Question Answered Quickly 

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

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

Anonymous
Not applicable

Hi,

 

The original list is accurate, if you look at it closely Area1 and Area3 are missing some months, for Area2 there is row for every month on this selected period though.

 

- Aku

v-robertq-msft
Community Support
Community Support

Hi, 

According to your description and sample pictures, I can clearly understand your requirement, I think you can create two calculated columns to achieve your requirements:

Rank by areaid = RANKX(FILTER(ALL(Actions),[Areaid]=EARLIER(Actions[Areaid])),[YYYYMM],,ASC,Dense)
ActionsRolling =

var _rolling=CALCULATE(SUM(Actions[Actions]),FILTER(ALL(Actions),[Areaid]=EARLIER(Actions[Areaid])&&[Rank by areaid]<=EARLIER(Actions[Rank by areaid])))

return

IF([Rank by areaid]>=12,_rolling,BLANK())

 

And you can get what you want, like this:

vrobertqmsft_0-1636364578968.png

 

Then you can place this column into your matrix as the value field:

vrobertqmsft_1-1636364578972.png

 

You can download my test pbix file below

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

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

Anonymous
Not applicable

Hi,

 

That is not exactly what I was looking for. The sum needs to be 12 months rolling from the presented month. The solution also requires already summed rows for the rank to work properly, I could sum them first but would make things simpler if there is solution that would not require it, if that is not possible then we must use the summed rows and live with that. As this does not solve my problem I did not test if this would it work with month slicer selection to show the rolling 12 months. I added another sample here how the data would show if we selected 202109 from slicer.

 

image.png

 

Thanks for your input though.

 

- Aku

Anonymous
Not applicable

To keep the sample data simple I have already summed it with areaid. If the calculation is not possible with 2 different table, we can get the areadid in actions table if necessary. For all months there is no actions for each areaid.

 

YYYYMMAreaidActions
201901Area15
201902Area11
201903Area11
201905Area12
201906Area12
201909Area11
201910Area12
202001Area12
202002Area11
202003Area12
202004Area11
202005Area12
202006Area11
202008Area11
202009Area11
202101Area11
202103Area13
202104Area13
202105Area12
202106Area12
202107Area11
202108Area12
202109Area11
201901Area221
201902Area27
201903Area29
201904Area213
201905Area216
201906Area213
201907Area26
201908Area24
201909Area23
201910Area210
201911Area213
201912Area27
202001Area23
202002Area29
202003Area29
202004Area27
202005Area26
202006Area215
202007Area29
202008Area28
202009Area28
202010Area211
202011Area219
202012Area212
202101Area222
202102Area214
202103Area216
202104Area213
202105Area212
202106Area219
202107Area214
202108Area211
202109Area210
202110Area21
201901Area31
201902Area32
201903Area31
201905Area33
201906Area31
201907Area31
201909Area33
202011Area31
202012Area31

 

Here is one of the rolling selections for a reference:

 

YYYYMMAreaidActionsRolling total
202001Area23 
202002Area29 
202003Area29 
202004Area27 
202005Area26 
202006Area215 
202007Area29 
202008Area28 
202009Area28 
202010Area211 
202011Area219 
202012Area212116
202101Area222135
202102Area214140
202103Area216147
202104Area213153
202105Area212159
202106Area219163
202107Area214168
202108Area211171
202109Area210173
202110Area21163
202111Area20144

 

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