Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

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):

 

CountryEanIDCategoryYYYY_MM_WWeek idStoreTurnover$Distinctcount of stores in last 4 weeks
SE3111ORAL CARE2018_01_197NOR1 
DE3111ORAL CARE2018_01_197HUM1 
SE3111ORAL CARE2018_01_299KOS4 
SE3111ORAL CARE2018_01_299KON3 
SE3111ORAL CARE2018_01_4101ARJ43
SE3111ORAL CARE2018_03_9107ARJ41
SE3111ORAL CARE2018_04_14114ARJ41
SE3111ORAL CARE2018_04_17117ROM42
SE3111ORAL CARE2018_05_20121KOS41
SE3111ORAL CARE2018_05_21122HAS34
SE3111ORAL CARE2018_05_21122FAL34
SE3111ORAL CARE2018_05_21122ROM44
SE3111ORAL CARE2018_06_23125FAL23
SE3111ORAL CARE2018_06_24126FAL81
DE3111ORAL CARE2018_06_24126KOB11
DE2014ORAL CARE2018_01_197ALB5 
DE2014ORAL CARE2018_01_197ARH1 
DE2014ORAL CARE2018_01_197SKO1 
DE2014ORAL CARE2018_01_299KVI2 
SE2014ORAL CARE2018_01_3100NYH71
DE2014ORAL CARE2018_01_3100SUP35
SE2014ORAL CARE2018_01_4101BRA32
DE2014ORAL CARE2018_01_4101HUM14
DE2014ORAL CARE2018_01_4101OST14
DE2014ORAL CARE2018_02_7105AAL11
DE2014ORAL CARE2018_02_8106ARH12
DE2014ORAL CARE2018_03_11110NDR61

 

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

  • Stachu's avatar
    Stachu
    Community 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_Muhammad's avatar
      Zubair_Muhammad
      Community 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
          )
      )
    • Anonymous's avatar
      Anonymous
      Not applicable

      Stachu

       

      You were righr unconsistent Week Id was a problem. That was an issue how it was calcualted.

      Thanks