Forum Discussion

Aakash_Ladi's avatar
Aakash_Ladi
Regular Visitor
3 years ago

MAX function not working as expected

Hi,

Let me give a background to the problem I am facing:

I have a fact table which is a snapshot table (it basically stores multiple modifications of same OrderID). For Modification date, we have EffectiveDate and then we have another column named as ExpiredDate (which is basically day prior to the next EffectiveDate). So, here is some sample data:

OrderID    EffectiveDate    ExpiredDate                                  DepartDate         Quantity
1               1 Mar 2022       5 Apr 2022                                    5 Sep 2022          3
1               6 Apr 2022        4 Sep 2022                                   5 Sep 2022          2
1                5 Sep 2022       1 Jan 3000(some future date)      5 Sep 2022          3

2                2 Apr 2022        1 Jan 3000                                   2 Oct 2023          5

3                1 Jul 2023          1 Aug 2023                                 25 Dec 2023        2         

3                 2 Aug 2023        1 Jan 3000                                  25 Dec 2023       3


Now, based on such data, I have two create two measures:

1. Quantity Current Period -- It would be the Orders "as of Today"(2 Aug 2023) with DepartDate of this year (2023)
2. Quantity Prior Period -- It would be Orders as of last year (2 Aug 2022) with DepartDate of last year (2022)

I have a calendar table which is connected to depart date and filtering fine. So my Quantity Current Period measure looks like-

var filteredTable = FILTER(factTable,

DATE(2023,8,2) >= factTable[EffectiveDate]

&& DATE(2023,8,2) <= factTable[ExpiredDate]

)

 RETURN

 CALCULATE(SUM(factTable[quantity]), filteredTable)
This measure is working as expected.

 

For Prior Period, I tried various approaches, but nothing works and I am not able to figure out why it is happening:

I have a column in my calendar table with corrosponding Previous year date, I tried creating an inactive relationship between the departDate of factTable with PreviousYearDate and then using the UseRelationShip function. But it is showing blank values.
This is my measure:

var filteredTable = FILTER(factTable,

DATE(2022,8,2) >= factTable[EffectiveDate]

&& DATE(2022,8,2) <= factTable[ExpiredDate]

)

 RETURN

 CALCULATE(SUM(factTable[Quantity]),

filteredTable,
USERELATIONSHIP(DimCalendar[PreviousYearDate], FactTable[DepartDate])
)
But, this is giving all blank values and it is taking a lot of time.

 

Then I tried,
filter(All(Calendar[Date]), Calendar[Date] >= MIN(PreviousYearDate) && Calendar[Date] <= MAX(PreviousYearDate))
It is also not working. I even tried putting these Min and Max aggregation into different measures and those measures are giving me correct dates. 

 



I am not able to figure out a solution. Please help.

9 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Aakash_Ladi 

    You can try the following measure

     

    Quantity Prior Period =
    CALCULATE (
        SUM ( factTable[Quantity] ),
        DATE ( 2022, 8, 2 ) >= factTable[EffectiveDate],
        DATE ( 2022, 8, 2 ) <= factTable[ExpiredDate],
        DATEADD ( DimCalendar[Date], -1, YEAR )
    )
    

     

    If it cannot work,  can you provide some more sample data or related sample file?

     

    Best Regards!

    Yolo Zhu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Aakash_Ladi's avatar
    Aakash_Ladi
    Regular Visitor

    Hi Anonymous,


    Even DATEADD is not working. Please see the attached link for pbix file for your refrence(FYI I am not able to attach the file here, please see if you can access it from here or let me know if you can guide me how to add attachment).

    https://shorturl.at/zJW18

     

    Regards,

    Aakash Ladi