skip to Main Content

power pivot to power bi

A Power BI Technique Mined from the Power Pivot Archives

Below, you will find one of our all-time favorite Power BI techniques. In the ten (yes ten) years we’ve been operating this site, we’ve written over 1,000 articles on Power BI.  Hard to imagine - even for us - but true.

Years ago, we first wrote up this technique in the context of Power Pivot – Power BI’s cousin and predecessor, which we like to call “Power BI in Excel.” 

Since the two products share the same brains (DAX and M), this technique is more relevant today than ever. Everything below is 100% legit for Power BI – the same data model “shape,” the same DAX, etc. – but now you can use it to power up your Power BI visuals, and not just PivotTables.  Enjoy! Smile


Guess Post by Scott Senkeresty at Tiny Lizard

imageJust a quick and practical tip today.

We have a really typical looking Date table.  However, we are going to be drawing some pretty charts summarized by weeks, and our business defines “end of week” at Saturday.  So, we need a new column in our Date table that stores this “Week Ending” date for each row.

The first thought to occur to me was “well, for each Year&WeekOfYear, I just want to grab the max date”.   That sounded easy enough… EARLIER() no longer scares me…

[WeekEnding] =
   CALCULATE(MAX(Calendar[Dates]),
FILTER(ALL(Calendar),
Calendar[Year] = EARLIER(Calendar[Year]) &&
Calendar[WeekOfYear] =
EARLIER(Calendar[WeekOfYear])
)
)

And for a brief moment, I felt very proud of my calculated column.  There were lots of rows that were totally correct.  Most of them in fact!   However, closer inspection found two problems:

1) 4/24 showed up as a WeekEnding, because my calendar table stopped there, even though it wasn’t a Saturday.    I wasn’t feeling horrible at that point… that felt solvable.

2) 12/31 showed up as WeekEnding because of the year change.   That felt harder to solve.

Luckily, I had a brief moment of clarity, and recalled Rob saying just yesterday “Well, dates are just integers… that is why you can subtract 1 to get to yesterday’s date”.   Hmmm, brain churning, thinking, … typing! image

[ModTest] =
MOD(Calendar[Dates], 7)

You can see the results at right.  Tell me they aren’t super sexy and totally promising!

All we need to do is that MOD() value, which is just a remainder after an integer division, to add an appropriate number of days to each row!

In my case (week ending Saturday), I want to add 0 days on Saturdays, 1 day on Fridays, 2 days on Thursday, etc.

And my magic calculated column ended up being:

=Calendar[Dates] –
MOD(Calendar[Dates]-1, 7) + 6

Depending on the particular day of the week you imagewant to use for your WeekEnding, that -1 in the equation will change.  To save you the trouble I have created the very fancy chart at left.

I am not including a final picture of the correct results.  It’s my little test to see if you trust me.  Enjoy!

This Post Has 11 Comments
  1. I used existing date functions and worked out well for me. Here is the formula:
    =Calendar[Dates]+(7-WEEKDAY(Calendar[Dates]))

    this is the formula for Saturday as your weekending day

    As we already know 7 days a week, weekday() function returns integer value represents the day of week.

    when i used this i got the weekednigs correctly even my table stopped in the middle of the week.

    Thanks

  2. I’ve had to add a Week Ending column and I can use this formula in Excel and SSAS Tabular cube.

    For Week ending Fridays…. =date(year([YourDateColumn]),Month([YourDateColumn]),Day([YourDateColumn]))+7 – Weekday(date(year([YourDateColumn]),Month([YourDateColumn]),Day([YourDateColumn]))+1)

    For Week ending Sundays… =date(year([YourDateColumn]),Month([YourDateColumn]),Day([YourDateColumn]))+9 – Weekday(date(year([YourDateColumn]),Month([YourDateColumn]),Day([YourDateColumn]))+1)

    1. This is considering that the week commences on a “Sunday”. However, for a week commencing on “Monday” I have modified the formula as follows:

      =dimDate[Date]+MOD(7-WEEKDAY(dimDate[dimWeekDay]),7)

  3. Week Start & End Date From Date

    WeekStartDate = [DateColumn] – (WEEKDAY([DateColumn])-1)
    WeekEndDate = [DateColumn] + (7-WEEKDAY([DateColumn]))

  4. Thanks this also works for me with the Friday as the weekending. Saturdays = ‘Calendar'[Date]+MOD(6-WEEKDAY(‘Calendar'[Date]),7).

Leave a Comment or Question