Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
shivkonar
Frequent Visitor

Bar Chart / Date Calculation

Hi,

 

I am trying to solve a made up problem. Imagine we have a 2 table model. The Sales table and a Date Table in a 1 to Many relationship.

 

On the Power BI report, there is a slicer for Date[Calendar Year] = 2018 and another slicer for Date[Calendar WeekNumber] and there is a bar chart.

 

The bar chart always will only show 4 bars, for example. The 4 bars will show [Total Sales] for 4 weeks from the selected Date[Calendar WeekNumber] going backwards.

 

Eg: User selects Date[Calendar WeekNumber] = 31
Bar chart will show 4 bars:
Bar 1: Total Sales for WeekNumber 28
Bar 2: Total Sales for WeekNumber 29
Bar 3: Total Sales for WeekNumber 30
Bar 4: Total Sales for WeekNumber 31

 

My approach was a "New Table" route in Power BI. The formula I came up with is as below:

 

Past 4 Weeks Sales = 
GROUPBY (
    FILTER(
        ALL('Sales'), 
        RELATED('Date'[Calendar WeekNumber]) >= SELECTEDVALUE('Date'[Calendar WeekNumber]) - 3 && 
        RELATED('Date'[Calendar WeekNumber]) <= SELECTEDVALUE('Date'[Calendar WeekNumber])),
    'Date'[Calendar WeekNumber],
    "Total Sales", SUMX ( CURRENTGROUP (), 'Sales'[Sales] )
)

This works when I hardcode the >= with 28 and <= with 31.

 

Question:
1. What is wrong with my approach? Why does this always return a blank table ? And why does this work with hard-coded conditions
2. Is there a more elegant way to solve this problem?

Thanks,
Shiv

1 ACCEPTED SOLUTION

Thanks @v-juanli-msft

 

There is a more elegant solution by Marco Russo.

 

Step 1:- Create a new DateSelection table, this table will be placed on the slicer

DateSelection = DISTINCT ( 'Date'[Calendar WeekNumber] )

Step 2:- Create the measure

Last 4 Week Sales = 
VAR LastWeek = SELECTEDVALUE ( 'DateSelection'[Calendar WeekNumber] )
VAR FirstWeek = LastWeek - 3
VAR Result =
    CALCULATE (
        SUM ( 'Sales'[Sales] ),
        KEEPFILTERS (
            'Date'[Calendar WeekNumber] >= FirstWeek
                && 'Date'[Calendar WeekNumber] <= LastWeek
        )
    )
RETURN Result

Step 3:- Create the bar chart, by placing Date[Calendar WeekNumber] on the axis and [Last 4 Week Sales] on the values.

 

So, now for any week selected, the bar chart will show 4 bars, one bar for the selected week and 3 previous week.

View solution in original post

10 REPLIES 10

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.