Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
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 Campaign | Index |
Penetration | 1 |
BOGO | 2 |
Contracted Deals | 3 |
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!
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.
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.
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
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)
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!
Copying DAX from this post? Click here for a hack to quickly replace it with your own table names
Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C
I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
111 | |
100 | |
80 | |
64 | |
58 |
User | Count |
---|---|
148 | |
111 | |
93 | |
84 | |
66 |