Forum Discussion
Repeat sum over date range
- 8 years ago
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
RemovedColumn2. Here is a sample of the Month data & a sample of your table formatting:
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
- 8 years ago
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]
)
Add corresponding fields into table visual.
Best regards,
Yuliana Gu