The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Here are the two most effective methods to create a contiguous range of dates between two date columns such as [Start Date] and [End Date]
Where would I use this? With customer subscriptions, events, classes, hotel stays and more. Essentially anywhere you have a start date and an end date and you need to track and report on the days in between those two dates.
In this scenario we'll be using Hotel Check-in and Check-out dates. You may think... why would I want to do this? The purpose is so that you can look at hotel occupancy or occupancy rates over time, on a timeline (ie Line Chart) by day, week, month, quarter, year etc.
Question?
Should I use DAX to create a calculated table?
OR
Should I use PowerQuery M to create the table?
Answer
Your date range table will be a mere fraction of the size if you import through PowerQuery vs creating it using DAX
DAX Method
Now let's dive into the DAX method. There are other methods out there, but this method I have created performs 50% to 80% faster during the query stage vs all other methods.
In Power BI Desktop, on the "Home" ribbon click on "New Table" and paste in the below DAX to create your calculated table.
Occupancy Days =
SELECTCOLUMNS(
GENERATE(
'hotel_guests',
DATESBETWEEN(
'calendar'[Date],
'hotel_guests'[Check-in Date],
'hotel_guests'[Check-out Date])
),
"Guest Email",[Email], //note the email column comes from the hotel_guests table
"Occupancy Date",[Date] //note the date column comes from the column output of the datesbetween function
)
The end result is a two column table with [Guest Email] and [Occupancy Date] where for each guest it includes a row for each night they stayed at the hotel.
PowerQuery M Method
As a test you can employ both methods (in separate PBIX files), and benchmark the resulting file size of your PBIX files, as well as, computational and refresh run times. The PowerQuery method will provide better overall results.
Looking to learn more? I teach on weekends for Divergence Academy. We're always running classes
Would you kindly advise how I can achieve similar results, but with year_quarter? Like 2024_1, 2024_2... Etc.
Thank you
Hi, @TrickMasterPC
Thank you for sharing! You can Mark your post as an answer, so it will be easier for other users to search.
Best Regards