Forum Discussion
Distinctcount by groups in calc column
I am looking for a DAX to use in calc column. (Not a measure, because I will use it for further calculations)
It should calculate Distinctcount of stores in last 4 weeks. Here is how it should work (last column):
| Country | EanID | Category | YYYY_MM_W | Week id | Store | Turnover$ | Distinctcount of stores in last 4 weeks |
| SE | 3111 | ORAL CARE | 2018_01_1 | 97 | NOR | 1 | |
| DE | 3111 | ORAL CARE | 2018_01_1 | 97 | HUM | 1 | |
| SE | 3111 | ORAL CARE | 2018_01_2 | 99 | KOS | 4 | |
| SE | 3111 | ORAL CARE | 2018_01_2 | 99 | KON | 3 | |
| SE | 3111 | ORAL CARE | 2018_01_4 | 101 | ARJ | 4 | 3 |
| SE | 3111 | ORAL CARE | 2018_03_9 | 107 | ARJ | 4 | 1 |
| SE | 3111 | ORAL CARE | 2018_04_14 | 114 | ARJ | 4 | 1 |
| SE | 3111 | ORAL CARE | 2018_04_17 | 117 | ROM | 4 | 2 |
| SE | 3111 | ORAL CARE | 2018_05_20 | 121 | KOS | 4 | 1 |
| SE | 3111 | ORAL CARE | 2018_05_21 | 122 | HAS | 3 | 4 |
| SE | 3111 | ORAL CARE | 2018_05_21 | 122 | FAL | 3 | 4 |
| SE | 3111 | ORAL CARE | 2018_05_21 | 122 | ROM | 4 | 4 |
| SE | 3111 | ORAL CARE | 2018_06_23 | 125 | FAL | 2 | 3 |
| SE | 3111 | ORAL CARE | 2018_06_24 | 126 | FAL | 8 | 1 |
| DE | 3111 | ORAL CARE | 2018_06_24 | 126 | KOB | 1 | 1 |
| DE | 2014 | ORAL CARE | 2018_01_1 | 97 | ALB | 5 | |
| DE | 2014 | ORAL CARE | 2018_01_1 | 97 | ARH | 1 | |
| DE | 2014 | ORAL CARE | 2018_01_1 | 97 | SKO | 1 | |
| DE | 2014 | ORAL CARE | 2018_01_2 | 99 | KVI | 2 | |
| SE | 2014 | ORAL CARE | 2018_01_3 | 100 | NYH | 7 | 1 |
| DE | 2014 | ORAL CARE | 2018_01_3 | 100 | SUP | 3 | 5 |
| SE | 2014 | ORAL CARE | 2018_01_4 | 101 | BRA | 3 | 2 |
| DE | 2014 | ORAL CARE | 2018_01_4 | 101 | HUM | 1 | 4 |
| DE | 2014 | ORAL CARE | 2018_01_4 | 101 | OST | 1 | 4 |
| DE | 2014 | ORAL CARE | 2018_02_7 | 105 | AAL | 1 | 1 |
| DE | 2014 | ORAL CARE | 2018_02_8 | 106 | ARH | 1 | 2 |
| DE | 2014 | ORAL CARE | 2018_03_11 | 110 | NDR | 6 | 1 |
I would like this to be calculated by Country, Category and EanID.
For example:
For Country = SE, for EanID = 3111, for Category = ORAL CARE
in Week id = 101, I have a 3 distinct Stores selling in last four weeks: ARJ, KON and KOS.
The last four weeks are: 101, 100, 99, 98. So Distinctcount of stores in last 4 weeks = 3
Please note Week id is non continuous.
Thanks in advance
Anonymous
May be something like this
Column = CALCULATE ( DISTINCTCOUNT ( Table1[Store] ), FILTER ( ALLEXCEPT ( Table1, Table1[Country], Table1[EanID], Table1[Category] ), Table1[Week id] <= EARLIER ( Table1[Week id] ) && Table1[Week id] >= EARLIER ( Table1[Week id] ) - 3 ) )
4 Replies
- StachuCommunity Champion
I still think the measure would be more appriopiate in this case as you look for aggregation dependant on particular week selection.
currently your example is inconsistent e.g. blanks in top 4 rows are not in line with your description (i.e. for 1st row there was exactly 1 distinct store with last 4 weeks being 94-97, while it's all populated for week 121)
How are you planning to use this measure/column later? Maybe that will help to properly adjust the setup- Zubair_MuhammadCommunity Champion
Anonymous
May be something like this
Column = CALCULATE ( DISTINCTCOUNT ( Table1[Store] ), FILTER ( ALLEXCEPT ( Table1, Table1[Country], Table1[EanID], Table1[Category] ), Table1[Week id] <= EARLIER ( Table1[Week id] ) && Table1[Week id] >= EARLIER ( Table1[Week id] ) - 3 ) )- AnonymousNot applicable
- AnonymousNot applicable
You were righr unconsistent Week Id was a problem. That was an issue how it was calcualted.
Thanks