Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

SUM CONDITIONAL WITH 2 DIFFERENT TYPE DATE

Hello everyone,   I searched the forum and I didn't find any cases similar to mine.    I would like to create a "SUM CONDITIONAL". Indeed, I would like to make the sum of the sales by taking into...
  • Cmcmahan's avatar
    7 years ago

    A lot of this depends on how your data model is are set up, since the relationships here are important.  It's also important that you know how you want to display/use this particular measure.

     

    Making a best guess, it seems like you would just use your dates as two filter conditions, just like any other measure that has multiple fields to filter by.  This is no different from using [Size] = "Large" && [Color] = "Red" as filter conditions.


    This returns a sum of [Value] where the Recorded date is in the month of August 2019, and the Effective date is between August 1 2019, and August 1 2022. You can adjust the DATESINPERIOD parameters as you see fit for your case

    SumBasedOnTwoDates =
    CALCULATE (
        SUM ( Table1[Value] ),
        DATESINPERIOD ( Table1[RecordedDate], DATE ( 2019, 8, 1 ), 1, MONTH ),
        DATESINPERIOD ( Table1[EffectiveDate], DATE ( 2019, 8, 1 ), 3, YEAR )
    )
  • Cmcmahan's avatar
    Cmcmahan
    7 years ago

    It sounds like you did your unpivoting in a way that made your data much harder to work with.  If every record has a value for each of these dates, why unpivot them in the first place?  

     

    If you toally, absolutely, 100%, for sure had to unpivot this data (I would strongly recommend re-evaluating that decision), then you can use CALCULATE to change the context of the DATESINPERIOD functions. 

     

    This starts wildly complicating your query plan, and you lose a LOT of performance due to the constant context switching that needs to be done.  In fact, I'm not sure it can be done without re-pivoting the data in the measure, since DAX really doesn't like switching between different rows.  It may be possible if you have a second related table that you group by, but the query gets really messy, and I don't want to start guessing at possible data models you may be using.