Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Running Total with categories

Hi all, I have a question on running totals:
I have a table "Data" of projects (identified by ID). Each project has a generated power "Power", an operational start year "Op_Year" (in number format - not date), a current status (e.g. design, under construction, operative) "Status" and a country "Geo".

I successfully plotted in a stacked bar chart the additional power that will be available from 2020 to 2025 by Geo and in another by Status and I would like now to create other two bar charts with the cumulative installed power (also considering power already installed before 2020 but start plotting from 2020 onward). 

I used the following measure that worked to plot the cumulative power:

RunningTotal = CALCULATE(SUM('Data'[Power]]);FILTER(ALLSELECTED('Data');'Data'[Op_Year]<=MAX('Data'[Op_Year])))

(with minimum x-axis value 2020 and max 2025 to limit the years as needed).

But when I try to add a legend (Geo or Status) to the chart it does not work anymore (it even changes values and totals in the chart)! I also tried to modify the formula this way but again it did not work correctly (same for Status instead of Geo).

RunningTotal = CALCULATE(SUM('Data'[Power]]);FILTER(ALLSELECTED('Data');'Data'[Op_Year]<=MAX('Data'[Op_Year])&&'Data'[Geo]=MAX('Data'[Geo])))


Can you please help me to understand where I am mistaking? Thanks!

11 Replies

  • sturlaws's avatar
    sturlaws
    Resident Rockstar

    Hi Anonymous,

     

    could you try changing your code to this:

    RunningTotal =
    CALCULATE (
        SUM ( 'Data'[Power] );
        FILTER (
            ALLEXCEPT ( 'Data'; Data[Geo] );
            'Data'[Op_Year] <= MAX ( 'Data'[Op_Year] )
        )
    )
    
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi sturlaws, thanks for you reply.
      Unfortunately it does not work . Until I try to categorize the thing it is working as expected (see picture).

       









      When I add "Geo" to the legend of the chart, it changes to this:

       

       

       

       

       

       

       

       

      While what I expect is something like this:

      • Elvi_well's avatar
        Elvi_well
        Frequent Visitor

        Hi!
        did you solved the problem? I have the same issue.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous 

    Try replace ALLSELECTED('Data') to All(Data). 

    If not working, try :

     

    RunningTotal = CALCULATE(SUM('Data'[Power]]);FILTER(ALL('DATA'); SUMX(FILTER('DATA';EARLIER('DATA'[Op_Year])<='DATA'[Op_year);1)))

     

     

    Paul Zheng
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous thank you for the input.
      Again not working, unfortuntely. The output is the one below:

       

      If it may help, below the raw data I am using for this example (the real db is much more bigger and confidential, but the below is fully representative of the problem)

      Spoiler
      IDPowerGeoStatusOp_Year
      110RoWInstallation2021
      25RoWOperational2019
      34EuropeDesign2021
      47AsiaOperational1996
      510EuropeOperational1993
      63AmericasInstallation2021
      75AsiaOperational2000
      83RoWOperational1990
      97EuropeDesign2024
      101EuropeOperational2006
      111RoWDesign2025
      124AsiaOperational1999
      131RoWOperational2016
      146AsiaInstallation2021
      157RoWOperational2015
      166RoWOperational2009
      173RoWOperational2016
      183EuropeOperational1997
      198RoWOperational2001
      204EuropeOperational1994
      215RoWInstallation2022
      222EuropeOperational2004
      237RoWOperational2012
      241AmericasOperational2017
      259AsiaDesign2024
      264AsiaOperational2018
      272RoWInstallation2023
      287RoWOperational2015
      295AsiaOperational2017
      303AmericasInstallation2020
      314EuropeDesign2028
      326AsiaOperational2019
      338RoWOperational2017
      3410EuropeOperational1993
      359AsiaOperational2017
      365RoWDesign2023
      379RoWOperational2019
      382AsiaDesign2024
      393AsiaOperational2013
      403AsiaOperational2017
      417AmericasOperational1997
      426AmericasDesign2027
      435RoWOperational2002
      445AmericasOperational2018
      458AmericasDesign2022
      466EuropeOperational2008
      472RoWOperational2015
      484AmericasOperational2011
      496AsiaOperational2017
      507AsiaOperational2000

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Can anyone help on this?

        Thanks! ðŸ˜Š

  • The issue you’re facing with your cumulative power measure when adding a legend (such as "Geo" or "Status") is likely due to how the DAX formula interacts with the context of the visualization. When you add a legend, it creates additional filtering contexts that affect the calculation, so it’s essential to modify the formula to account for these contexts properly.

    Here's a solution you can try by modifying the `RunningTotal` measure to take into account the legend (e.g., "Geo" or "Status"):

    ### Running Total with Categories Solution:

    ```DAX
    RunningTotal =
    VAR CurrentGeo = MAX('Data'[Geo]) -- or MAX('Data'[Status]) if using Status as the category
    VAR CurrentYear = MAX('Data'[Op_Year])

    RETURN
    CALCULATE(
    SUM('Data'[Power]),
    FILTER(
    ALLSELECTED('Data'),
    'Data'[Op_Year] <= CurrentYear
    && 'Data'[Geo] = CurrentGeo -- or 'Data'[Status] = CurrentStatus] for Status
    )
    )
    ```

    ### Explanation:
    1. **`CurrentGeo` and `CurrentYear`**: These variables capture the current category and year that the chart is focusing on, based on the selected data points.

    2. **Filtering with Context**: The `FILTER` function now includes both the year and the `Geo` (or `Status`), ensuring that the cumulative total is calculated only for the current category being plotted.

    3. **ALLSELECTED**: This function ensures that the running total respects the selections made in the report (e.g., if you have other slicers affecting the data).

    ### Notes:
    - If you're using `Status` as the legend instead of `Geo`, replace `Geo` with `Status` in the formula.
    - You can use this formula for both charts with different legends (one for `Geo` and one for `Status`) by adapting the variable `CurrentGeo` to the relevant column.

    This should allow you to properly display the running totals for each category (either by "Geo" or "Status") in the chart without affecting the totals or changing values unexpectedly.

    • Elvi_well's avatar
      Elvi_well
      Frequent Visitor

      Thanks for the explanation!
      I have the issue that if there is no a category on the last period it does not show me from the previous period. So mark A from grade 2 is not showen on the grade 3. Is it possible to solve it somehow?