cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
jimbob741 Frequent Visitor
Frequent Visitor

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

Hello,

 

I've only just started with Power BI and DAX very recently, so am quite stuck with relation to this problem i'm having.

 

I would like a bar chart that will display 3 different results based on the week number of the year I have selected. I would like to select the week (e.g. 'week 35'), then from there I would like it to calculate the previous week, and previous week last year and display the results side by side.

 

The data is litterally just a 'Week Number' column with a 'Users' count as the value.

 

Please could someone help me with the steps needed to achieve this?

 

Many thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
v-haibl-msft Super Contributor
Super Contributor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

@jimbob741

 

I’d like to give you a sample as below. The .pbix file has been upload here. I assume we have a table like below.

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_1.jpg

 

We can first create a calendar table and create two columns with following formula. Create a relationship between these two tables with Date key.

Calendar = CALENDAR ( "1/1/2015", "12/31/2016" )
WeekNum = WEEKNUM ( 'Calendar'[Date] )
Year = YEAR ( 'Calendar'[Date] )

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_2.jpg

 

In original table, also create a WeekNum column.

WeekNum = RELATED ( 'Calendar'[WeekNum] )

Then we only need to create two measures to get 'Last Week' and 'This Week Last Year' data.

Last Week = 
VAR CurrentWeekNum =
    MAX ( 'Calendar'[WeekNum] )
VAR CurrentYear =
    MAX ( 'Calendar'[Year] )
RETURN
    (
        CALCULATE (
            SUM ( Table1[Users] ),
            FILTER (
                ALL ( 'Calendar' ),
                'Calendar'[Year] = CurrentYear
                    && 'Calendar'[WeekNum]
                    = CurrentWeekNum - 1
            )
        )
)
This Week Last Year = 
VAR CurrentWeekNum =
    MAX ( 'Calendar'[WeekNum] )
VAR CurrentYear =
    MAX ( 'Calendar'[Year] )
RETURN
    (
        CALCULATE (
            SUM ( Table1[Users] ),
            FILTER (
                ALL ( 'Calendar' ),
                'Calendar'[Year]
                    = CurrentYear - 1
                    && [WeekNum] = CurrentWeekNum
            )
        )
    )

Drag two slicers for Year & WeekNum and a clustered bar chart into canvas.

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_3.jpg

 

Best Regards,

Herbert

6 REPLIES 6
v-haibl-msft Super Contributor
Super Contributor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

@jimbob741

 

I’d like to give you a sample as below. The .pbix file has been upload here. I assume we have a table like below.

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_1.jpg

 

We can first create a calendar table and create two columns with following formula. Create a relationship between these two tables with Date key.

Calendar = CALENDAR ( "1/1/2015", "12/31/2016" )
WeekNum = WEEKNUM ( 'Calendar'[Date] )
Year = YEAR ( 'Calendar'[Date] )

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_2.jpg

 

In original table, also create a WeekNum column.

WeekNum = RELATED ( 'Calendar'[WeekNum] )

Then we only need to create two measures to get 'Last Week' and 'This Week Last Year' data.

Last Week = 
VAR CurrentWeekNum =
    MAX ( 'Calendar'[WeekNum] )
VAR CurrentYear =
    MAX ( 'Calendar'[Year] )
RETURN
    (
        CALCULATE (
            SUM ( Table1[Users] ),
            FILTER (
                ALL ( 'Calendar' ),
                'Calendar'[Year] = CurrentYear
                    && 'Calendar'[WeekNum]
                    = CurrentWeekNum - 1
            )
        )
)
This Week Last Year = 
VAR CurrentWeekNum =
    MAX ( 'Calendar'[WeekNum] )
VAR CurrentYear =
    MAX ( 'Calendar'[Year] )
RETURN
    (
        CALCULATE (
            SUM ( Table1[Users] ),
            FILTER (
                ALL ( 'Calendar' ),
                'Calendar'[Year]
                    = CurrentYear - 1
                    && [WeekNum] = CurrentWeekNum
            )
        )
    )

Drag two slicers for Year & WeekNum and a clustered bar chart into canvas.

How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data_3.jpg

 

Best Regards,

Herbert

jimbob741 Frequent Visitor
Frequent Visitor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

Thank you very much Herbert, much appreciated.

 

Your solution seems to be exactly what im after, I will give it a go and let you know how I got on!

v-haibl-msft Super Contributor
Super Contributor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

@jimbob741

 

Has your problem gotten solved?

 

Best Regards,

Herbert

jimbob741 Frequent Visitor
Frequent Visitor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

Perfectly Herbert, thank you! Appologies for not confirming sooner.

ShawnPrice Regular Visitor
Regular Visitor

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

I was trying to recreate this and it works well, I but instead of Users being a number I have Call Reps which is a text field. Can I do something like this with that, I can't seem to figure it out. Thanks.

ElliotP Member
Member

Re: How to create Bar chart comparing 'This Week', 'Last Week', and 'This Week Last Year' data

@v-haibl-msftAmazing Solution.

 

I'm trying to use the same code; I have it working for my purpose except I can't seem to use my Datekey or a calculated column in my date table, I have to use the WeekNum value otherwise it doens't work; thoughts?