Forum Discussion

AbbasAsaria90's avatar
8 years ago
Solved

Issue combining VAR and CALCULATE( ..., FILTER

I have a table called `pd_nb_leads`, which is a table of our sales leads. One of the columns is `Date deal created`, and another is `Deal Origin`.   I'm looking to create a graph showing the moving...
  • danextian's avatar
    8 years ago

    Hi AbbasAsaria90,

     

    For your measure, if your CurrentDate variable is simply equal to the dates in the date column then you can simply write your formula as 

     

     

    Leads created, last 28 days =
    COUNT ( pd_nb_leads[Deals] )

     

    because hen you place your Date colum and the Leads Created, day measure inside the table, the value that is being returned by the measure is being filtered by the value in the Date column. 

     

     

     

    Now for your second measure, the same thing happens. It is being filtered by the value from the Date column. Thus your seeing the same result. To achieve your desired result, you need to create a separate Calendar table and then create a relationship between the generated date column from Calendar and Date column from your fact table. You can create in a calculated calendar table in dax by using CALENDAR FUNCTION. Example:

     

     

    CALENDAR =
    CALENDAR (
        MIN ( 'X Axis: Day Created'[Date].DATE ),
        MAX ( 'X Axis: Day Created'[Date].DATE )
    )

     

     

    which is dynamically created based on the earliest and latest dates from Fact. Now, create another measure

     

    Leads created, last 28 days =
    CALCULATE (
        COUNT ( pd_nb_leads[Deals] ),
        DATESINPERIOD ( Calendar[Date], LASTDATE ( Calendar[Date] ), -28, DAY )
    )

    Put this measure and the date table from calendar to a table.

     

    You can find a good tutorial here: https://powerpivotpro.com/2013/07/moving-averages-sums-etc/