Quantcast
Viewing all articles
Browse latest Browse all 217232

DAX Measure to get aggregation of repeated values for the latest date

Hi there,

 

Id

Date

Country

Category

Gender

CurrentStock

UnitValue

1

1/1/2016

France

A

Male

6

1

1

1/1/2016

France

A

Female

6

2

1

1/1/2016

France

B

Male

6

5

1

1/1/2016

France

B

Female

6

7

2

1/1/2016

UK

A

Male

7

3

2

1/1/2016

UK

A

Female

7

4

2

1/1/2016

UK

B

Male

7

5

2

1/1/2016

UK

B

Female

7

8

21

1/2/2016

France

A

Male

3

10

21

1/2/2016

France

A

Female

3

5

21

1/2/2016

France

B

Male

3

4

21

1/2/2016

France

B

Female

3

3

 

Consider the dataset above which is the result of a data entry form with the Id column unique per form entry. On the form there are actually four Unit Value fields, one for each combination of Category and Gender “Category A Male Units” etc. However, there is only one CurrentStock field because this is not broken out by Category or Gender. The data from the form has been unpivoted into the table above for the model so we can have Category and Gender dimension fields for series and slicers etc. The Month and Country are single values per form.

The measure for UnitValue is a simple Total Units := SUM([UnitValue) since you can add these over all dimensions. However, for the CurrentStock I should only get one of the values per form Id but should be able to sum them over all forms. This seems to be working with:

Total Current Stock =

SUMX (

   VALUES ( 'Forms'[Id] ),

   CALCULATE ( MIN ( Forms '[CurrentStock] ) )

)

However, this is further complicated by the fact that CurrentStock is semi-additive i.e. is a rolling value. Forms are usually filled once a month (although you could have more than one in a month) with the CurrentStock representing the stock level at that time. So we should only get the most recent value for any given date range. But within that date range we should be able to see a total over all Countries. To achieve this I was looking to do something like the following:

Total Current Stock =

   CALCULATE (

       SUM ('Forms'[CurrentStock]),

       LASTNONBLANK (

           'Date'[Date],

           CALCULATE(SUM('Forms'[CurrentStock]))

       )

   )

But I need to combine these two ideas i.e. for the last non-blank date in the current date range, get the set of form data and then sum up the CurrentStock, taking only one value per form (Id). I think I should be using a GROUPBY with ADDCOLUMN but I got stuck at this point.

Any help would be appreciated.

Thanks,

Daire


Viewing all articles
Browse latest Browse all 217232

Trending Articles