Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

powerpivot - DAX running total (or count) across 2 groups

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...