Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
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
Solved! Go to 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.
Hi,
With the relationship well set, try this measure
=CALCULATE(SUM('Sales'[Sales]),DATESBETWEEN('Date'[Date],MAX('Date'[Calendar WeekNumber]),MAX('Date'[Calendar WeekNumber])-28))
Hope this helps.
I don't see any bars on the chart. No matter what week I select.
Here is my data model: The 'DateSelection' table is not used in your calculated measure and Calculations table is just me storing the measures. There is a 1-Many relationship between Sales and Date table
And here is the measure:
Any ideas why this doesn't work?
Hi,
The relationship should be from the Order Date column of the Sales Table to the Date column of the Date Table.
Hi,
Share the link from where i can download your PBI file.
Here you go:- https://drive.google.com/drive/folders/19Qw-n4gV4ncsffU-PV58_PUI8PgmlRLu?usp=sharing
Page 1 is Marco's solution
Page 2 and [Last 4 Week Sales5] are your solution.
Hi,
I am unable to solve the problem with my formula. I believe you should stick to Marco's solution.
Couple points...
1) Datesbetween needs a date column Not a weeknumber. Also read this about Dates in PBI (why you are getting nothing)
2) But even if you use a date column using 28 (or 4 weeks times 7 days) will not give you 4 complete weeks but rather 28 consecutive days which is not the same.
Stick with the Marco Russo solution!
Hi @shivkonar
Please have a look at my method.
1.create relationship between sales table(sheet3) and date table(sheet4)
2. add "weeknum1" from the date table to the slicer, then create measure in the date table
selected week = SELECTEDVALUE(Sheet4[weeknum1])
3.create measures in the sales table
total sales =
CALCULATE (
SUM ( Sheet3[sales] ),
FILTER ( ALL ( Sheet3 ), [weeknum2] = SELECTEDVALUE ( Sheet3[weeknum2] ) )
) flag = IF(MAX([weeknum2])<=[selected week]&&MAX([weeknum2])>[selected week]-4,1,0)
4.add "weeknum2" column from the sales table to the Axis of the bar chart, then add "flag" measure to the Visual filter and select 1 to apply filter.
Best Regards
Maggie
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.
User | Count |
---|---|
141 | |
113 | |
104 | |
78 | |
64 |
User | Count |
---|---|
136 | |
125 | |
107 | |
70 | |
61 |