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
AdamM
Frequent Visitor

Repeat sum over date range

I'm struggling with how to use DAX to repeatedly SUM a value for each month if it falls within a date range.

 

This is a sample of my data:

Monthly SavingsSavings Effective DateSavings End Date
10,0001/1/20176/30/2017
15,0001/1/201712/31/2017

 

This is the output that I want:

MonthMonthly Savings
Jan25,000
Feb25,000
Mar25,000
Apr25,000
May25,000
Jun25,000
Jul15,000
Aug15,000
Sep15,000
Oct15,000
Nov15,000
Dec

15,000

 

What is the most efficient way to do this?

2 ACCEPTED SOLUTIONS
fhill
Resident Rockstar
Resident Rockstar

 

1.  You need to create a Table with just MONTHS in it to calculate your values per month.  You can do this in Excel and Copy/Paste into PowerBI, or here's code the 'Advanced Editor' code form Query Editor to build a list -100 months back to +100 months foward.  (You can edit the 2nd row as needed.)

 

let
Source = Table.FromList({-100..100}, each{_}),
AddedStartOfMonth = Table.AddColumn(Source, "StartOfMonth", each Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),[Column1])), type date),
AddedEndOfMonth = Table.AddColumn(AddedStartOfMonth, "EndOfMonth", each Date.EndOfMonth([StartOfMonth]), type date),
RemovedColumn = Table.RemoveColumns(AddedEndOfMonth,{"Column1"})
in
RemovedColumn

 

2.  Here is a sample of the Month data & a sample of your table formatting:

 

Capture.PNG

 

3.  Here's the DAX Column code for a column created on the *** Month Table *** that Sums Monthly Savings for each month if the Month Start date falls between your Eff Date & End Date.  

Monthly Savings = CALCULATE(SUM(Table1[Monthly Savings]), FILTER(ALL(Table1), Query1[StartOfMonth] >= Table1[Eff Date] && Query1[StartOfMonth] <= Table1[End Date]))

 

*** BTW, this assums solid months Start & end Dates.  If this could start/ stop mid-month, you need to create a DAILY table of every day, Create a 'Daily Savings' value in your raw data (DateDiff your two dates + 1 / Monthly Savings, use simular code to find a Daily Savings value, and then SUM up the Days into Months using PowerBI Tables or another DAX Measure.

 

FOrrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




View solution in original post

Ashish_Mathur
Super User
Super User

Hi @AdamM,

 

You may refer to my solution here.

 

Hope this helps.


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

View solution in original post

5 REPLIES 5
v-yulgu-msft
Employee
Employee

Hi @AdamM,

 

Create a calendar table.

Calendar =
FILTER (
    CALENDAR (
        MIN ( Savings[Savings Effective Date] ),
        MAX ( Savings[Savings End Date] )
    ),
    DAY ( [Date] ) = 1
)

Create a calculated table via CrossJoin.

CrossJoin =
ADDCOLUMNS (
    FILTER (
        CROSSJOIN ( Savings, 'Calendar' ),
        'Calendar'[Date].[MonthNo] >= Savings[Savings Effective Date].[MonthNo]
            && 'Calendar'[Date].[MonthNo] <= Savings[Savings End Date].[MonthNo]
    ),
    "MonthName", 'Calendar'[Date].[Month]
)

1.PNG

 

Add corresponding fields into table visual.

2.PNG

 

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Ashish_Mathur
Super User
Super User

Hi @AdamM,

 

You may refer to my solution here.

 

Hope this helps.


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

Ashish,

 

Thank you! Your solution produced the daily savings amounts which enabled the ability to aggregate in different ways when I join my savings data with other related datasets. I believe expanding to a record for savings dollars each day is the ideal approach since it will be the most flexible as my reporting requirements change over time.

 

Adam

You are welcome.


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

 

1.  You need to create a Table with just MONTHS in it to calculate your values per month.  You can do this in Excel and Copy/Paste into PowerBI, or here's code the 'Advanced Editor' code form Query Editor to build a list -100 months back to +100 months foward.  (You can edit the 2nd row as needed.)

 

let
Source = Table.FromList({-100..100}, each{_}),
AddedStartOfMonth = Table.AddColumn(Source, "StartOfMonth", each Date.StartOfMonth(Date.AddMonths(Date.From(DateTime.LocalNow()),[Column1])), type date),
AddedEndOfMonth = Table.AddColumn(AddedStartOfMonth, "EndOfMonth", each Date.EndOfMonth([StartOfMonth]), type date),
RemovedColumn = Table.RemoveColumns(AddedEndOfMonth,{"Column1"})
in
RemovedColumn

 

2.  Here is a sample of the Month data & a sample of your table formatting:

 

Capture.PNG

 

3.  Here's the DAX Column code for a column created on the *** Month Table *** that Sums Monthly Savings for each month if the Month Start date falls between your Eff Date & End Date.  

Monthly Savings = CALCULATE(SUM(Table1[Monthly Savings]), FILTER(ALL(Table1), Query1[StartOfMonth] >= Table1[Eff Date] && Query1[StartOfMonth] <= Table1[End Date]))

 

*** BTW, this assums solid months Start & end Dates.  If this could start/ stop mid-month, you need to create a DAILY table of every day, Create a 'Daily Savings' value in your raw data (DateDiff your two dates + 1 / Monthly Savings, use simular code to find a Daily Savings value, and then SUM up the Days into Months using PowerBI Tables or another DAX Measure.

 

FOrrest




Did I answer your question, or help you along the way?
Please give Kudos or Mark as a Solution!


https://www.linkedin.com/in/forrest-hill-04480730/

Proud to give back to the community!
Thank You!




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.