DAX running total (or count) across 2 groups

谁说我不能喝 提交于 2019-11-27 08:26:01

问题


I'm very new to DAX and PowerPivots. I am using PowerPivot to pull data from an SQL server. The data I'm working with is pretty large and complex but I'm considering a simplified version of it for this question.

Let's say I have 2 columns, one that contains product names and one that contains a date that product was sold. I have a row for each time 1 unit of any product is sold.

Product | Date
Orange  | 08/13/2013
Orange  | 08/13/2013
Orange  | 08/13/2013
Apple   | 08/14/2013
Apple   | 08/16/2013
Orange  | 08/17/2013
Orange  | 08/17/2013

I want to use DAX to get a running count of how much of a product has been sold to date over the entire data set. This is what I would like to end up with.

Product | Date        | Cumulative Sales
Orange  | 08/13/2013  | 1
Orange  | 08/13/2013  | 2
Orange  | 08/13/2013  | 3
Apple   | 08/14/2013  | 1
Apple   | 08/16/2013  | 2
Orange  | 08/17/2013  | 4
Orange  | 08/17/2013  | 5

Any help would be appreciated.

EDIT: One more thing, the data is not necessarily ordered by date. I could potentially order it by date but it would require modification of some other things so my preference would be not to do so if at all possible. There are a lot of other formulas in the sheet I inherited and reordering may break something else.


回答1:


You can make a calculated measure in PowerPivot to handle this. If you want to get the cumulative sales for all time you can do this:

    CALCULATE(     SUM( FactSales[SalesAmount] ),
    FILTER(
        ALL( DimDate) ,
        DimDate[Datekey] <= MAX( DimDate[Datekey] )
    )
)

If you want to be able to select certain time period (ex: running total for selected weeks or months) you can do this:

 CALCULATE( SUM( FactSales[SalesAmount])  ,
                 FILTER(
                    ALLSELECTED( DimDate),
                    DimDate[Datekey] <= MAX( DimDate[Datekey] )
                )
    )

Source: Javier Guillen's blog



来源:https://stackoverflow.com/questions/18275270/dax-running-total-or-count-across-2-groups

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!