Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

Extract values between BeginDate and EndDate then select top 1 where durationInDays is max

Hi comunity ,

 

Would appreciate any help , because i stuck on task for several days . I need tables in SSAS tabular cube '

хив Торговых Точек'(fact table) and 'ТорговаяТочка'(dimension) . They have relationsip through ShopId column .  I have also Calendar table which is marked as Date table which has not relationship with 

'хив Торговых Точек' as there is no appropriate Date column in 'хив Торговых Точек'.

 
I want to create measure in 'ТорговаяТочка' table 
which would extract Shopid which was active during selected month in filters .
Conditions are : 
There are BeginDate ,EndDate ,isArchive and durationInDays columns in 

'хив Торговых Точек' . So let's say i have selected date November 2016 .I want code to filter out if selected month and year in filter , falls into range of begindate and enddate , so then it would extract the one in range .But there could more than one Shopid in that date range ,so in this case it should extract the one whose duratioInDays is maximum . So for example ,here is the data in d

database , as you can see there are multiple values for shopid,where November 2016 falls apart

between begindate and enddate . I need to take only that which has the most days in durationInDays column(red marked) and count it as 1.So for each month it would count 1 for only that ShopId which falls into range with the highest value in durationInDays column . I have dax code , which does meet requirements,so for now from the above screenshot it take shopid where duratioIndays = 179 ,for the same filter November 2016 . Thanks in advance .

VAR HasFilter =
    HASONEVALUE('Календарь'[MonthNo])

VAR CloseDateFilter =
    FILTER(
        ALL('Aрхив Торговых Точек'),
        'Aрхив Торговых Точек'[isArchive] = 0 &&
        'Aрхив Торговых Точек'[BeginDate] <= max('Календарь'[DayDate]) &&
        'Aрхив Торговых Точек'[EndDate] > max('Календарь'[DayDate])    
    )
VAR MaxDurationShop =
    CALCULATE(
        MAXX(
            VALUES('Aрхив Торговых Точек'[ShopID]),
            CALCULATE(
                MAX('Aрхив Торговых Точек'[durationIndays]),
                CloseDateFilter,
                VALUES('Aрхив Торговых Точек'[ShopID])
            )
        )
    )

VAR CountOfShopsWithConditions =
    CALCULATE(COUNTROWS('Aрхив Торговых Точек'),FILTER('Aрхив Торговых Точек','Aрхив Торговых Точек'[durationInDays] = MaxDurationShop))


RETURN
    IF(HasFilter, IF(NOT(ISBLANK(MaxDurationShop)), CALCULATE(CountOfShopsWithConditions), BLANK()),BLANK())

 

2 Replies

  • Anonymous , Create a measure like this.

     CALCULATE(AVERAGEx(FILTER(Table,Table[BeginDate]<=max('Date'[Date]) && (ISBLANK(Table[EndDate]) || Table[EndDate]>max('Date'[Date]))),(DATEDIFF(Table[BeginDate],Max('Date'[Date]),Day))),CROSSFILTER(Table[BeginDate],'Date'[Date],None))

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi ,thanks for your reply . Here is the error message :