Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

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 
  • bdymit's avatar
    bdymit
    9 years ago
    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:

5 Replies

  • Anonymous's avatar
    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's avatar
      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?

       

       

      • bdymit's avatar
        bdymit
        Icon for Resolver II rankResolver II
        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:

  • SivaMani's avatar
    SivaMani
    Icon for Resident Rockstar rankResident 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.