Creating Dynamic Measures for Previous Week and Week Before Previous Week in Power BI

In this blog, we will explore how to dynamically calculate previous week and week-before-previous-week measures in Power BI, even when transitioning between years. Handling week-based calculations can be challenging at the start of the year since Week 1 must reference Week 52 of the previous year, and Week 2 may need to reference Week 51. We will walk through the step-by-step process of creating these measures to ensure accurate comparisons across weeks.

Step 1: Creating the Base Measure

Before calculating the previous weeks' values, let's define our core measure: Cost per Conversion (CPC):

Cost per Conversion (CPC) =
DIVIDE(
SUM(Marketing[ad_spend]),
SUM('Marketing'[conversion_rate]) * SUM('Marketing'[audience_reach]),
0
)

This measure calculates the cost per conversion by dividing the total ad spend by the product of conversion rate and audience reach.

Step 2: Calculating Previous Week's Cost per Conversion

Now, let’s define a measure to retrieve the previous week’s CPC, considering the year transition:

Previous Week Cost per Conversion =
VAR Currentweek_ = WEEKNUM(TODAY(),2)
VAR Currnetyear_ = YEAR(TODAY())
VAR previousweek = IF(Currentweek_=1, 52, Currentweek_-1)
VAR previousyear_ = IF(Currentweek_ =1 , Currnetyear_ -1 , Currnetyear_)

RETURN
CALCULATE(
    [Cost per Conversion (CPC)],
    Marketing[Weeknum] = previousweek,
    Marketing[Year] = previousyear_
)

Here’s how this works:

  • If the current week is Week 1, the previous week should be Week 52 of the previous year.
  • Otherwise, it simply subtracts 1 from the current week.
  • The Year is adjusted accordingly.

Step 3: Calculating the Week Before Previous Week

Similarly, we need to compute Week Before Previous Week Cost per Conversion, ensuring a seamless transition between years:

Week Before Previous Week Cost per Conversion =
VAR Currentweek = WEEKNUM(TODAY(), 2)
VAR Currentyear = YEAR(TODAY())
VAR Weekbeforepreviousweek = IF(Currentweek = 1, 51, IF(Currentweek = 2, 52, Currentweek - 2))
VAR weekbeforepreviousyear = IF(Currentweek = 1, Currentyear - 1, IF(Currentweek = 52, Currentyear - 1, Currentyear))
RETURN
CALCULATE(
    [Cost per Conversion (CPC)],
    Marketing[Weeknum] = Weekbeforepreviousweek,
    Marketing[Year] = weekbeforepreviousyear
)

This measure ensures that:

  • If the current week is Week 1, it references Week 51 of the previous year.
  • If the current week is Week 2, it references Week 52 of the previous year.
  • Otherwise, it simply subtracts 2 from the current week.
  • The Year is adjusted accordingly.

Step 4. Comparing Cost per Conversion Values

To ensure that we only display CPC data when both previous weeks’ values are available, we create a comparison measure:

Cost per Conversion Comparison =
VAR previousweek_ = [Previous Week Cost per Conversion]
VAR weekbeforepreviousweek = [Week Before Previous Week Cost per Conversion]
VAR ispreviousweekdateavailable = NOT(ISBLANK(previousweek_))
VAR isweekbeforepreviousweekdataavailable = NOT(ISBLANK(weekbeforepreviousweek))
RETURN
IF(
   AND(ispreviousweekdateavailable, isweekbeforepreviousweekdataavailable),
   [Cost per Conversion (CPC)],
   BLANK()
)

This measure checks whether data exists for both previous weeks. If either of the weeks is missing, the measure returns blank to prevent misleading analysis.

How to Use These Measures in Power BI

Now that we have our measures, we can utilize them in different visualizations:

  • Waterfall Chart - To show CPC trends dynamically.
  • Card Visuals - To display individual CPC values for the previous week and the week before.
  • Conditional Formatting - Highlight variations when comparing CPC values.

Conclusion

By implementing these measures, you ensure that week-based comparisons remain accurate, even when transitioning between years. This approach not only prevents incorrect calculations but also improves data accuracy for reporting and decision-making. If you are looking to refine your Power BI reports further, feel free to reach out for expert assistance!