Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Creating a custom Date Slicer

Hello,

 

I am trying to create a parameter/variable/formula to return a given period of time.  I have three periods.

 

I want the user to be able to select a Marketing Campaign Name and the data switches based on the campaign slicer to the dates below.

 

Marketing campaign

Penetration: 1/1/18 to 5/25/2018

BOGO: 5/26/18 to 7/4/18

Contracted Deals: 7/5/18 - 10/15/18

 

I started by making a table with the names of the campaigns.

 

Marketing CampaignIndex
Penetration1
BOGO2
Contracted Deals3

 

I included an index, and sorted by the index, so when I add the table values as a card, they appear in the desired order.

 

After that I don't know how to set up my variables or create measures to use in the switch formula.  I don't think I need parameters in this case, but I don't know how to set the date range either way.  I'm less than 40 hours into building DAX.

 

After I have my variables I think I can use Switch(True(),

                                                                         [Campaign Selected] = "Penetration", [Penetration] ),

                                                                         [Campaign Selected] = "BOGO", [BOGO] ),

                                                                         [Campaign Selected] = "Contracted Deals", [Contracted Deals] ),

                                                                   [????] )

 

But I'm going to get caught up again because I don't how to create a time period for the false period which would be 1/1/18 - 10/15/18. I guess another option would be to have four periods with one being the full period.

 

Appreciate the help!

 

 

6 Replies

  • For your campaigns add two more columns to the reference table. Start Date and End Date.

    Then when a campaign is selected you can filter your dates table using DATESBETWEEN() with the selected campaign's dates. 

    This will then automatically filter your transaction table for that date range.

     

    Note: If you only compare against a single value you can simplify the switch statement like this:

     Switch([Campaign Selected],"Penetration", [Penetration],"BOGO", [BOGO],"Contracted Deals", [Contracted Deals] ,or else) 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for the help.

       

      I couldn't figure out how to write the datesbetween formula, and I actually have data that goes into dynamic dates.  I couldn't figure out how to write into a table MAX(Date) or TODAY(), so I couldn't take this approach.

       

      Appreciate the help though!

  • AllisonKennedy's avatar
    AllisonKennedy
    Community Champion
    What is the formula for [Contracted Deals]?

    I assume it includes the date range, so build something similar with the false condition.
  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

     

    Table:

     

    Test:

     

    Calendar(a calculated table):

    Calendar = CALENDAR(DATE(2015,1,1),DATE(2020,12,31))

     

    There is no relationship between tables. You may create two measures as below.

    DateRange = 
    var _cam = SELECTEDVALUE('Table'[Marketing Campaign])
    return
    IF(
        ISFILTERED(Test[Marketing Campaign]),
        SWITCH(
            TRUE(),
            "Penetration" in DISTINCT(Test[Marketing Campaign])&&_cam = "Penetration",
            "1/1/18 to 5/25/2018",
            "BOGO" in DISTINCT(Test[Marketing Campaign])&&_cam = "BOGO",
            "5/26/18 to 7/4/18",
            "Contracted Deals" in DISTINCT(Test[Marketing Campaign])&&_cam = "Contracted Deals",
            "7/5/18 to 10/15/18",
            BLANK()
        )
    )
    
    Visual Control = 
    var _date = SELECTEDVALUE('Calendar'[Date])
    return
    IF(
        ISFILTERED(Test[Marketing Campaign]),
        IF(
            HASONEVALUE(Test[Marketing Campaign]),
            SWITCH(
                SELECTEDVALUE(Test[Marketing Campaign]),
                "Penetration",
                IF(
                    _date>=DATE(2018,1,1)&&
                    _date<=DATE(2018,5,25),
                    1,0
                ),
                "BOGO",
                IF(
                    _date>=DATE(2018,5,26)&&
                    _date<=DATE(2018,7,4),
                    1,0
                ),
                "Contracted Deals",
                IF(
                    _date>=DATE(2018,7,5)&&
                    _date<=DATE(2018,10,15),
                    1,0
                )
            ),
            0
        ),
        0
    )

     

    Finally you need to put 'Visual Control' measure in the corresponding visual level filter to filter out the result.

     

     

     

    Best Regards

    Allan

     

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

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for the help.

       

      I couldn't get it to work until I figured out I needed to create a new table, and write it as a formula.

       

      The solution worked once I figured that part out.  I had to add in some dynamic dates and did that by adjusting the date(2020,5,1) to MAX(DATE) in the two DAX formulas.

       

      However when I try to deselect the certain period to see the overall time frame, it doesn't work.  

       

       

      • v-alq-msft's avatar
        v-alq-msft
        Community Support

        Hi, Anonymous 

         

        I am not very clear about your requirement. Could you please show us some sample data and expected results with OneDrive for business? Do mask sensitive data before uploading. Thanks.

         

        Best Regards

        Allan