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
Munckin
New Member

Sum positive values per day and aggregate per week

Hello,

 

I am quite new to PowerBI and this is my first time posting here.

What I am trying to accomplish is to retrieve the sum of all sales that are above a certain day threshold and aggregate these values per week. 

 

I have the following example data: 

Sales

DateTimeNet sales
1-5-202313:00100
1-5-202314:0050
1-5-202315:0070
2-5-202312:00150
2-5-202316:0030
3-5-202310:00200
4-5-202311:00160
4-5-202311:0010
5-5-202313:0080
5-5-202314:00150
5-5-202315:0020
6-5-202315:00170
7-5-202312:00190
7-5-202316:0040

 

Threshold

DateThreshold
1-5-2023150
2-5-2023190
3-5-2023250
4-5-2023180
5-5-2023150
6-5-2023200
7-5-2023200

 

Above Threshold =
VAR Surplus =
   CALCULATE(SUM('Sales[Net sales]) - Threshold[Threshold])
RETURN
    IF(Surplus > 0, Surplus , BLANK()))
 
When I create a matrix visual on day aggregation everything works fine. But when I try to use weeks, the negative values are also included when the total of a week is above the threshold total of a week. 
 
I am very curious to learn how I can solve this problem.
Thank you in advance! 
 
 
1 ACCEPTED SOLUTION
andhiii079845
Super User
Super User

You need a dimensional table for the date to bring both tables together in a model:

Here is the measure, you have to calculated the difference within the dates 

andhiii079845_2-1679335602417.png

 

Measure =
VAR _sum = SUMMARIZE(DimDate,DimDate[Date],"_diff",if(sum(sales[Net sales])-sum(threshold[Threshold])>0,sum(sales[Net sales])-sum(threshold[Threshold]),BLANK()))
RETURN sumx(_sum,[_diff])

andhiii079845_0-1679335507235.png

 

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
andhiii079845
Super User
Super User

You need a dimensional table for the date to bring both tables together in a model:

Here is the measure, you have to calculated the difference within the dates 

andhiii079845_2-1679335602417.png

 

Measure =
VAR _sum = SUMMARIZE(DimDate,DimDate[Date],"_diff",if(sum(sales[Net sales])-sum(threshold[Threshold])>0,sum(sales[Net sales])-sum(threshold[Threshold]),BLANK()))
RETURN sumx(_sum,[_diff])

andhiii079845_0-1679335507235.png

 

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Wow, you are fast! Your solution works like a charm. I am not yet familiar with the summarize function of DAX, so i'm gonna try and figure out exactly how the formula works. Thank you very much! 🙂 

Enjoy your journey in DAX 🙂 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




andhiii079845
Super User
Super User

You calculated the sum in your var and than you check if the sum is greater as 0. 

If you want to check the sum per day row per row, you have to use sumx(). 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.