Search code examples
powerbidaxcumulative-sumcumulative-line-chart

Power BI Dax Cumulative Line Graph


I have been trying to get a cumulative sum line onto a line chart. If this were only about a single variable (like time) it would be easy. But I need the chart to respond to selections of other dimensional data on other visualizations.

I have mocked up some data to demonstrate the problem.

I created a measure to show the cumulative amounts:

Qty Cumulative = CALCULATE (
    [Qty],
    FILTER (
        ALL ( data ),
        data[Month]
            <= MAX ( data[Month] )
    )
)

But it doesn't respond to selections made elsewhere on the page. So next I tried to add some context back into the measure:

Qty Cumulative 2 = CALCULATE (
    [Qty],
    FILTER (
        ALLSELECTED( data ),
        data[Month] <= MAX ( data[Month] )
        && data[Product] = SELECTEDVALUE(data[Product])
        && data[Region] = SELECTEDVALUE(data[Region])
    )
)

This one responds to other selections, but it requires exactly one value for each dimension. If no value is selected (so ALL) or two or more values are selected, the Qty Cumulative 2 by Month chart is blank.

Here's a screen capture of what I build in Power BI Desktop.

visualization sample

How can I make add a cumulative line to a chart and still have the output relate to the selections made on the page?


Solution

  • All(data) clears all filters on the table, you only want to clear the filters on date, so you just need to change it to All(data[Month]):

    Cumulative = 
      VAR selectedMonth = SELECTEDVALUE(data[Month])
      RETURN CALCULATE([Qty], ALL(data[Month]), data[Month] <= selectedMonth)