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.
Hi all,
I am trying to calculate the total sales for the last 3rd or 2nd day in my sales table based on a selected date. My model have 2 tables:
1- Sales table which holds products, value and dates (some dates are blank).
2-Dates table
I have an active relationship between Dates[Date] & Sales[Date].
My aim is to have a measure to calculate total sales in the last N days from the selected date & ignoring blank dates.
For example when I select the 29/04/2020, I should be able to see the below table:
Not sure how to:
a- Show the last 3 days (29, 22 and 21) in columns dynamicaly.
b- Measure to calaclate the total sales based on the selection.
Hopefully that make sense. Attached is the sample PBIX file.
Many thanks
Solved! Go to Solution.
Hi @H_insight ,
First delete the relationship between table Dates and Sales,see below:(if delete the relationshiop is not a good choice,then create a new calendar table and use its dates for calculation)
Then create a measure as below:
Measure =
VAR a =
CALCULATETABLE (
TOPN (
3,
FILTER ( VALUES ( Sales[Date] ), Sales[Date] <= SELECTEDVALUE ( Dates[Date] ) ),
[Date], DESC
),
ALLSELECTED ( Sales )
)
RETURN
IF (
SELECTEDVALUE ( Dates[Date] ) = BLANK (),
MAX ( 'Sales'[Values] ),
CALCULATE ( SUM ( Sales[Values] ), FILTER ( Sales, Sales[Date] IN a ) )
)
Finally you will see:
For the related .pbix file ,pls click here.
Hi @H_insight ,
First delete the relationship between table Dates and Sales,see below:(if delete the relationshiop is not a good choice,then create a new calendar table and use its dates for calculation)
Then create a measure as below:
Measure =
VAR a =
CALCULATETABLE (
TOPN (
3,
FILTER ( VALUES ( Sales[Date] ), Sales[Date] <= SELECTEDVALUE ( Dates[Date] ) ),
[Date], DESC
),
ALLSELECTED ( Sales )
)
RETURN
IF (
SELECTEDVALUE ( Dates[Date] ) = BLANK (),
MAX ( 'Sales'[Values] ),
CALCULATE ( SUM ( Sales[Values] ), FILTER ( Sales, Sales[Date] IN a ) )
)
Finally you will see:
For the related .pbix file ,pls click here.
Hi @v-kelly-msft ,
Amazing! Thank you for sharing the solution. It works as expected and most importantently your dax has no impact on the model performance.
I would be greatful if you can share how to get the total sales for:
- Last day of previous month
- Last day of previous week
Kind regards,
Hesham
Hi @v-kelly-msft ,
Any chance you can help me to get the total sales for:
- Last day of previous month
- Last day of previous week
Many thanks
Hesham
Hi @H_insight,
Check this file as example: Download PBIX
The idea is to have a disconected date table and for the value get the last 3 or n dates in your sales table, iterate on sales table and check if is within those 3 or n dates and sum by date.
If you consider it as a solution, please mark as a solution and kudos.
Ricardo
Hi @camargos88 ,
Thanks for providing your solution. The solution did work, however there were an additonal impact on the model perfomance. It was consumsing a lot of memory, therefore it was not ideal.
Best Regards,
Hesham
Refer , if this can help
Rolling 3 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date],max(Sales[Sales Date]),-3,DAY))
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
109 | |
98 | |
77 | |
66 | |
54 |
User | Count |
---|---|
144 | |
104 | |
100 | |
86 | |
64 |