Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

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 dShopArchive-forum.PNG

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 2
Chakravarthy
Resolver II
Resolver II

@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
Not applicable

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

Shaxa_0-1702636344112.png

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.