Dynamic Weekend using Parameter in PowerBI

I had a perfectly Working PowerBI report the last 7 Months , The data is refreshed daily, but we need to see the weekly Quantity basically on a rolling basis of the last 7 days.

When the data refresh, a calculated column generate a new weekly grouping based on the latest update from a Fact Table “Quantity Installed”, see example here

weekend.JPG

Everything Worked well, till someone asked if he can filter previous days, let’s say the report is Updated Monday, but he want to see the Data only till the previous Thursday , which is the official Cut off for the Client reporting.

Obviously it is easy just filter the data, but visually it is annoying as the weekend date is still Monday, calculated column change only when your refresh a report , Then they are fixed.

To show an example, this report show the total fatalities due to COVID19 per Week, The Cut off is 03/11/2020 which is a Tuesday, the weekly calendar is based on Tuesday as the end of the week

Now if you filter the data to the Previous Friday, the Weekend do not change, hence you get the impression that the total number for the Current week are going down which is not correct.

So, I asked in PowerBI forum how to do that, and Brian Maher solution is as columns do not change, we need to change the DAX,see link to the original solution

“The measure below checks to see if our date on the X-axis is the same day of the week as your slicer. If not it returns BLANK( ), if it is the same day of week, it sums the qty for the previous 7 days”

 Qty at 'week end' =
 VAR SelectedSlicerDate =
     SELECTEDVALUE ( 'Slicer Dates'[Date] )
 VAR ThisDateOnAxis =
     SELECTEDVALUE ( 'Calendar'[Date] )
 RETURN
     IF (
         WEEKDAY ( SelectedSlicerDate ) <> WEEKDAY ( ThisDateOnAxis ),
         BLANK (),
         CALCULATE (
             SUM ( 'Data Table'[qty] ),
             DATESBETWEEN ( 'Calendar'[Date], ThisDateOnAxis - 6, ThisDateOnAxis )
         )
     )

It is basically using a well know technique in DAX, show all the dimension value then anything with BLANK() result will be filtered out.

The DAX works perfectly, and solved the user request but , In my real case I had a lot of measures, changing all of them and risking introducing regression for a perfectly Working Data Model just for this small request was not worth it.

All I wanted is somehow make the column Weekend Dynamic at runtime, which was not possible till …… October 2020, we do have a new option, we can finally change the dimension dynamically and keep the DAX simple !!!!

if you have not heard about the New Parameter, maybe a good start is here.

My first attempt was to get the Day of the week in PowerQuery, but unfortunately Query Folding stopped Working and to be honest there is no clear way to know what’s supported or not as it is data Source specific, here I am using BigQuery.

Instead I create a simple calendar table with a column Day and 7 columns with all the possible week end ( Saturday, Sunday etc)

and then Create a new Parameter, with a default Day Thursday

Then Created a new Column “DynamicWeek” using this formula

I think we can agree the formula is trivial , a simple if then else

In order to make the parameter Dynamic, we create a new disconnected Table (not connected to any other Tables in the Model)

Then we Bind the Value of the Column Day to the Parameter

When the User changed the value of the disconnected Table using a Slicer

The Value get passed to the Parameter which Change the Value of the Column “Dynamic Parameter”

And here is the result

Update

a couple of hours after I published this blog, I got an idea that actually did Work.

Query folding does not work with the function Day of the Week, but Text.Middle did fold indeed, so all I had to do is create a new Disconnected Table like this

the same approach add a calculated column in the calendar Table

Just to be fancy, in the parameter, I used a Query that return always the latest date as a default value

and View of the Data Model

Created a new Measure that use the Disconnected Table as a filter Cut off, so the date get filtered

Death_cutoff = Var Sum_=sum(covid19new[daily_deaths])
return if (min(MstDates[Date])>SELECTEDVALUE(MstDates_Table_Cutoff[date]),BLANK(),Sum_)

I imagine you can search the date text and see if it is the end of the Month and generate Monthly calendar for example, a lot of flexibility.

notice here, because the Date Table is DirectQuery ( the fact and the parameter selection table are import) at least when using BigQuery the response time for the first select is around 3 seconds, but when you do the same selection, it become instantaneous as PowerBI cache the query result.

Now the Exciting part is which we don’t have yet, but apparently is coming next year at least for SnowflakeDB and Redshift, we will be able to write Custom SQL Query with Parameters in DirectQuery Mode, and then basically we will have finally “Dynamic Calculated Column“, ok if we have them in Import Mode, it is even better but let’s not get too Excited 🙂

DAX is extremely versatile , combine that with Dynamic column and we have a very powerful new tool, the future is exciting.

One thought on “Dynamic Weekend using Parameter in PowerBI”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s