Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Calculate Totals for Dynamic Date Range

Hi,

 

I have a table as imported from an excel file with two columns, Date and Amount. I have created a calculated column for a date 3 days in the past using the DAX formula StartDate = Sheet1[Date] = 3

 

I am trying to show an additional measure L3D Amount which would be a sum of the amount for the last 3 days, or however many days are present. Please see what I am trying to accomplish below. How would I be able to do this? Thanks

 

  

DateAmountL3D Amount 
31/01/199910,000.0010,000.00Sum of amount for 31/01/2017
01/07/20174,974.004,974.00Sum of amount for 01/07/2017
02/07/20175,475.0010,449.00Sum of amount for 01/07/2017, 02/07/2017
03/07/20179,305.0019,754.00Sum of amount for 01/07/2017, 02/01/2017, 03/07/2017
04/07/20173,169.0017,949.00Sum of amount for 02/07/2017, 03/01/2017, 04/07/2017
05/07/20172,649.0015,123.00Sum of amount for 03/07/2017, 04/01/2017, 05/07/2017
06/07/20172,585.008,403.00etc.
07/07/20177,572.0012,806.00 
08/07/20179,255.0019,412.00 
09/07/20173,433.0020,260.00 
10/07/20173,349.0016,037.00 
1 ACCEPTED SOLUTION

L3D Amount :=
VAR enddate =
    MAX ( Table1[Date] )
VAR startdate = enddate - 2
RETURN
    CALCULATE (
        [Sum of Amount],
        Table1[Date] >= startdate
            && Table1[Date] <= enddate
    )

Otherwise, if your version of Excel/ DAX does not support variables

L3D Amount No VAR :=
CALCULATE (
    [Sum of Amount],
    FILTER (
        ALL ( Table1[Date] ),
        Table1[Date] <= MAX ( Table1[Date] )
            && Table1[Date]
                >= MAX ( Table1[Date] ) - 2
    )
)

 

Both of the above are measures, you should not need your calculated column where you found the day that was 3 days previous.

 

In general, for values you want displayed within a visualization/Pivot Table/ etc., measures are more efficient than calculated columns because:

1. They do not take up physical space in your Data Model, which increases file size and memory used.

2. Calculated columns are calculated at refresh, taking up more time at refresh. Measures are only calculated when you place them in a visual/Pivot Table.

3. Measures are flexible, and you can change context with which you are seeing them very easily.

 

Let me know if this helps!

 

Result:

Capture.JPG

View solution in original post

5 REPLIES 5
SivaMani
Resident Rockstar
Resident Rockstar

@Anonymous Did you create a calculated column or calculated Measure?

 

Just create a calculated column with this DAX,

 

L3D= CALCULATE(SUM(Table2[Amount]), DATESBETWEEN(Table2[Date],DATEADD(Table2[Date],-3,DAY),Table2[Date]))

 

Let me know if the issue still exists.

Anonymous
Not applicable

You could try something like this to get the last 3 days summed:

 

=CALCULATE(SUM(Sheet1[Amount]),DATESBETWEEN(Sheet1[Date],DATEADD(Sheet1[Date],-2,DAY),Sheet1[Date]), all(Sheet1))

Anonymous
Not applicable

Doesn't work, tried it already. It says a single value can't be determined and I need to use min, max, etc. to get a single value. Is it possible to do what I'm trying to do?

 

 

L3D Amount :=
VAR enddate =
    MAX ( Table1[Date] )
VAR startdate = enddate - 2
RETURN
    CALCULATE (
        [Sum of Amount],
        Table1[Date] >= startdate
            && Table1[Date] <= enddate
    )

Otherwise, if your version of Excel/ DAX does not support variables

L3D Amount No VAR :=
CALCULATE (
    [Sum of Amount],
    FILTER (
        ALL ( Table1[Date] ),
        Table1[Date] <= MAX ( Table1[Date] )
            && Table1[Date]
                >= MAX ( Table1[Date] ) - 2
    )
)

 

Both of the above are measures, you should not need your calculated column where you found the day that was 3 days previous.

 

In general, for values you want displayed within a visualization/Pivot Table/ etc., measures are more efficient than calculated columns because:

1. They do not take up physical space in your Data Model, which increases file size and memory used.

2. Calculated columns are calculated at refresh, taking up more time at refresh. Measures are only calculated when you place them in a visual/Pivot Table.

3. Measures are flexible, and you can change context with which you are seeing them very easily.

 

Let me know if this helps!

 

Result:

Capture.JPG

Anonymous
Not applicable

@bdymit

 

Works like a charm, thanks!

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors