Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
LambaR
Frequent Visitor

Add custom dates parameter in DAX query

Hi,

Power Bi Test File 

I am interested in adding a custom date parameter, such as Start Date and End Date, to my DAX query. Currently, I am utilizing a Custom Date Range Slicer, offering options like "Last30Days," "Yesterday," "WTD," "MTD," and "YTD." However, I would like to introduce a "Custom Date" option, allowing users to select a specific Start Date and End Date tailored to their needs, similar to the Date Slicer but for a custom date range.

LambaR_2-1707305586000.png

 

 

Here is the current structure of my tables:

  • Master
  • Calendar (manually created)
  • SelectedDateRange (manually created)

My approach so far:

 

Step 1: Calculate all the dates for "Last30Days," "Yesterday," "WTD," "MTD," and "YTD," creating a new table named SelectedDateRange. (Refer below codes)

Step 2: Create a Calendar table encompassing dates from '2022-01-01' to today()-1.

Step 3: Establish a relationship between SelectedDateRange and the Calendar table based on Dates (Cardinality: Many to One).

Step 4: Establish another relationship between the Calendar and Master tables based on Dates (Cardinality: One to Many).

 

Now, I am contemplating the addition of StartDate and EndDate as custom parameters, allowing users to select them in the SelectedDateRange table based on their preferences.

 

I would appreciate any suggestions or guidance on achieving this goal. If there is a more efficient or preferable way to accomplish the same objective, please share your insights based on your expertise.

 

SelectedDateRange Codes:

 

 

SelectedDateRange = 
VAR TodayDate = TODAY()-1
VAR YearStart = CALCULATE(STARTOFYEAR('Master'[Date]), YEAR('Master'[Date]) = YEAR(TODAY()) )
VAR MonthStart = CALCULATE(STARTOFMONTH('Master'[Date]), YEAR('Master'[Date]) = YEAR(TODAY()), MONTH('Master'[Date]) = MONTH(TODAY()) )
VAR DaysToMonday = TodayDate - (WEEKDAY(TodayDate, 2) - 1)
VAR Yesterday = TodayDate
VAR Last30Days = TodayDate - 30

VAR Result =
UNION(
    ADDCOLUMNS(
        CALENDAR(YearStart, TodayDate),
        "Selection", "YTD",
        "Id",5
    ),
    ADDCOLUMNS(
        CALENDAR(MonthStart, TodayDate),
        "Selection", "MTD",
        "Id",4
    ),
    ADDCOLUMNS(
        CALENDAR(DaysToMonday, TodayDate),
        "Selection", "WTD",
        "Id",3
    ),
    ADDCOLUMNS(
        CALENDAR(Yesterday, TodayDate),
        "Selection", "Yesterday",
        "Id",2
    ),
    ADDCOLUMNS(
        CALENDAR(Last30Days, TodayDate),
        "Selection", "Last30Days",
        "Id",1
    )
)

RETURN
Result

 

 

 

Relationship Snapshot :

LambaR_0-1707305096260.png

Thank you.

2 REPLIES 2
Anonymous
Not applicable

Hi @LambaR ,

 

I can't open the pbix link you have provided. Can you share it again?

vkongfanfmsft_0-1707380935001.png

 

Best Regards,
Adamk Kong

Hi @Anonymous 

 

You can download the file using below link.
Power Bi Test File_Updated  

 

Please let me know if still unable to access the report.

 

 

Thanks

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.