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
IF
Post Prodigy
Post Prodigy

Sum between two months

Hi,

I have a Data table(Main), Calendar Table (Month), and Type table (Type). I use two slicers; one for type selection from the Type table and the other one is for the month selection from the Month table.

333.jpg

I want to calculate the sum for the selected type with the condition: If 02.2020 is selected and type b is selected, it should bring the sum between 12.2019 (inclusive) and 10.2019 (inclusive). Similarly; if I select 05.2020, the sum should be between 03.2020 and 01.2020.

 

I wrote this measure and it brings data starting from the selection month to 2 months backward which I don't want:

selected type moving sum =
CALCULATE(SUM(Main[value]),FILTER(ALL(Main),'Main'[dateselection]<=SELECTEDVALUE('Month'[Month])&&'Main'[dateselection]>=CALCULATE(MIN('Month'[Month]),DATEADD('Month'[Month],-2,MONTH))&&'Main'[type]=SELECTEDVALUE('Type'[type])))
 

I have also the list to correspond to the result, but it is also not in the way that I am looking for. I uploaded the file to:

https://www.dropbox.com/s/t9mbm0x1p3l1eti/sum%20between2.pbix?dl=0

 

Can you help me how to show data between -2 and -5 months based on the selection?

Thanks a lot in advance!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

8 REPLIES 8
IF
Post Prodigy
Post Prodigy

Hi, thanks for the measure. it works. Is it possible to see the values as a table? For example; if I select 02.2020 and type "b", I want to see the list:

b12.20195
b11.20192
b10.20194

thanks in advance

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you very much! How can I show those in a table as a list? regards,

Hi,

Not sure of how to do that.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, I used the measure in a line chart to show the values for each month and it didn't work. Do I do stmh wrong? Regards,

Hi,

My measure will return a single scalar value.  That is what i thought your original requirement was.  Now i do not know what you want.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
AllisonKennedy
Super User
Super User

@IF Is your month table a date table? https://excelwithallison.blogspot.com/2020/04/dimdate-what-why-and-how.html 

 

Is your Type table related to Main table? It should be, so you don't need this filter included in your formula. https://excelwithallison.blogspot.com/search?q=complicated 

 

If so, try using DAX DATESINPERIOD https://dax.guide/datesinperiod/ :

 

selected type moving sum =
VAR _selectedMonth = MAX(Dates[Date])
VAR _MonthsEnd = -2
VAR _PeriodLength = -3
VAR _endDate = EOMONTH(_SelectedMonth, _MonthsEnd)

RETURN
CALCULATE(SUM(Main[value]), DATESINPERIOD( Dates[Date] , _endDate, _PeriodLength, MONTH )

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Hi, if I am doing it correctly, the measure doesn't show the value in a line chart to show values for each month. line. Can you test as well? Thanks 🙂 

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.