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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

DAX to calculate whether the sum for the 3 days is above 72 hours

Hi everyone,

 

I am trying to build a measure to see whether the sum of a column for the last 3 days is greater than 72 days. If it is, then I need to ignore them from my Month-Year total.

 

Example in the table below, as the sum of 1/5 to 3/5 is >= 72, I need to show 0 as the value. Where the sum is < 72, e.g. 2/5 to 4/5 is 63 then I need to show 15 etc. I then need to sum up all the values for a Month-Year total i.e. 43.5 below.

 

I have tried using DATESINPERIOD as below and it sums it correctly at a date level. I am unsure how to ignore values above 72 and sum the Month-Year to 43.5

 

CALCULATE(SUM(Actualhours),

   DATESINPERIOD(Date[Date], MIN(Date[Date], -3, DAY)

)

 

I also need to add a filter to the measure to exclude a value but I can't seem to be able to use DATESINPERIOD and FILTER together as.

 

CALCULATE(SUM(Actualhours),

   DATESINPERIOD(Date[Date], MIN(Date[Date], -3, DAY),

   FILTER(Table1, Table1[ColumnA] <> "ABC")

)

 

Any suggestions?

 

YearMonthDateActualHoursSumOfLast3DaysValueNeeded
May-181/05/201824240
May-182/05/201824480
May-183/05/201824720
May-184/05/2018156315
May-185/05/2018145314
May-186/05/201814.543.514.5
May-187/05/20182452.50
May-188/05/20182462.50
May-189/05/201824720
  187.5 43.5
1 ACCEPTED SOLUTION

Hi,

 

Does this work?

 

ReplaceNew =
    SUMX(SUMMARIZE(VALUES(MasterCalendar[Date]),[Date],"ABCD",
        IF(CALCULATE(SUM(TableFromExcel[ActualHours]),
            DATESBETWEEN(MasterCalendar[Date], MIN(MasterCalendar[Date])-2, MIN(MasterCalendar[Date]))) >= 72, 0,
               CALCULATE(SUM(TableFromExcel[ActualHours]), TableFromExcel[Category] = "Planned Maintenance"))
        ),[ABCD]
    )


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

14 REPLIES 14
Ashish_Mathur
Super User
Super User

Hi,

 

Share the link from where i can download your PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi,

 

I think your replaces measure should be this

 

=SUMX(SUMMARIZE(VALUES(MasterCalendar[Date]),[Date],"ABCD",if(CALCULATE(SUM(TableFromExcel[ActualHours]),DATESBETWEEN(MasterCalendar[Date],MIN(MasterCalendar[Date])-3,MIN(MasterCalendar[Date])))>=72,0,SUM(TableFromExcel[ActualHours]))),[ABCD])

 

Also, remove Shift Date from the Row labels section of the visual and drag Date from the MasterCalendar Table instead.

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Thanks. That worked as expected for all but 1 cell. Can you explain the logic of using "ABCD"?

 

I added in a filter so that the measure only looks at Planned Maintenance as I want take away the need of  a slicer. My adjusted measure below.

 

With your measure or mine with the added filter, one cell isn't calulcated as expected. See picture below. Given 25/05, 26/05 and 27/05 sum to 52.65, I would expect 27/05 to show 4.65 in [ReplaceNew]. Any reason why this isn't the case?

 

ReplaceNew =
    SUMX(SUMMARIZE(VALUES(MasterCalendar[Date]),[Date],"ABCD",
        IF(CALCULATE(SUM(TableFromExcel[ActualHours]),
            DATESBETWEEN(MasterCalendar[Date], MIN(MasterCalendar[Date])-3, MIN(MasterCalendar[Date]))) >= 72, 0,
               CALCULATE(SUM(TableFromExcel[ActualHours]), FILTER(TableFromExcel, TableFromExcel[Category] = "Planned Maintenance")))
        ),[ABCD]
    )

 

DAX.PNG

Hi,

 

Does this work?

 

ReplaceNew =
    SUMX(SUMMARIZE(VALUES(MasterCalendar[Date]),[Date],"ABCD",
        IF(CALCULATE(SUM(TableFromExcel[ActualHours]),
            DATESBETWEEN(MasterCalendar[Date], MIN(MasterCalendar[Date])-2, MIN(MasterCalendar[Date]))) >= 72, 0,
               CALCULATE(SUM(TableFromExcel[ActualHours]), TableFromExcel[Category] = "Planned Maintenance"))
        ),[ABCD]
    )


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

I changed your measure slightly as below and works as expected. Thanks.

 

Could you explain the logic around "ABCD"?

 

ReplaceNew = 
SUMX(

  SUMMARIZE(

    VALUES(MasterCalendar[Date]),[Date],"ABCD", 

      IF(CALCULATE(SUM(TableFromExcel[ActualHours]), 

        DATESBETWEEN(MasterCalendar[Date], MIN(MasterCalendar[Date]) - 2, MIN(MasterCalendar[Date]))) >= 72,

        0, CALCULATE(SUM(TableFromExcel[ActualHours]), FILTER(TableFromExcel, TableFromExcel[Category] = "Planned Maintenance")

      )

    )

  ), ABCD]

)

Hi,

 

ABCD is the title of the virtual column created via the SUMMARIZE function.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-shex-msft
Community Support
Community Support

Hi @Anonymous,

 

You can try to use below measrue to achieve your requiremnt:

Replaced Value = 
VAR currDate =
    MAX ( Table1[Date] )
VAR rolling =
    CALCULATE (
        SUM ( [ActualHours] ),
        FILTER ( ALL ( Table1 ), [Date] > currDate - 3 && [Date] <= currDate )
    )
RETURN
    IF (
        rolling < 72
            && COUNTROWS ( FILTER ( ALLSELECTED ( Table1[Date] ), [Date] <= currDate ) )
                >= 3,
        SELECTEDVALUE ( Table1[ActualHours] ),
        0
    )

Rolling value = 
SUMX (
    FILTER ( ALLSELECTED ( Table1 ), [Date] <= MAX ( [Date] ) ),
    [Replaced Value]
)

17.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Thanks Xiaoxin. I have applied you logic, however, I am getting zeros in the 1st measure and the 2nd measure is taking too long to calculate:

 

I have used a MasterCalendar Date column instead of the table Date column which has a one to many relationship. I have applied the measure as below and the result is given 0 to all dates included the YearMonth column. See below.

 

When I add the measure [Rolling value] to the table, it's extremely slow to calculate. Measure is

 

Replaced Value =
VAR currDate =
    MAX ( PBI_MASTER_CALENDAR[Date] )
VAR rolling =
    CALCULATE (
        SUM ( Table1[ActualHours] ),
        FILTER ( ALL ( PBI_MASTER_CALENDAR ), [Date] > currDate - 3 && [Date] <= currDate )
    )
RETURN
    IF (
        rolling < 72
            && COUNTROWS ( FILTER ( ALLSELECTED ( PBI_MASTER_CALENDAR[Date] ), [Date] <= currDate ) )
                >= 3,
        SELECTEDVALUE ( Table1[ActualHours] ),
        0
    )

 

Rolling value =
SUMX (
    FILTER ( ALLSELECTED ( Table1), [ShiftDate] <= MAX ( PBI_MASTER_CALENDAR[Date] ) ),
    [Replaced Value]
)

 

YearMonthDateActualHoursReplaced Value
Jan-171/01/201795.70
Jan-172/01/201795.90
Jan-173/01/201795.70
Jan-174/01/201793.70
Jan-175/01/201785.40
Jan-176/01/201795.50
Jan-177/01/2017102.60
Jan-178/01/201788.20
Jan-179/01/201795.20
Anonymous
Not applicable

Looks like the result is what you gave, so thanks. However, I need to filter this data down a bit so when looking at granular level the measure applies.

 

Example, in the attached screenshot, I need the measure to calculate Planned Maintenance only (not a slicer).  For the yellow cells, I would expect the [Replaced Value] to be the same as ActualHours.

 

 

 

 

DAX.PNG

Hi @Anonymous,

 

If you can please share some sample data or pbix file for test and modify formula.
Notice: Please do mask on sensitive data before sharing.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Hi, I have attached a sample file.

 

When looking at the data, Dragline 301 for Planned Maintenance only on 30/04, 01/05 and 03/05 should be 0 as the sum of these 3 days are >= 72 hours. Same with 01/05, 02/05 and 03/05. All other amounts for Dragline 301 for Planned Maintenance should be the same as ActualHours.

 

Also, you'll notice in the YearMonth field, the substotal is showing blank for [Replaced Value]. This needs to be the sum of all the values in [Replaced Value] for that month.

 

Thanks.

Anonymous
Not applicable

https://1drv.ms/u/s!ArxsIP-W81kFgppleAo0lyKh8f-hTQ

 

File is saved here, as I couldn't figure out how to attach a .pbix file

Anonymous
Not applicable

Basically, the outcome here is to ignore where there are 3 consecutive days of a summed total greater than 72 and sum the rest of the values at a Month-Year level with filters.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.