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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Need cumulative sum with condition

Hi All, 

 

Hope everyone is well and safe in this pandemic. Iam new to Power BI and working on few calculations that need your help.

 

Sandeep2021_0-1625548755622.png

As shown above i have week and weekly confirmed cases as columns, "Up/down Trend Test" is a measure that returns if the cases from current and previous week are decreased or increased. logic is as below.

 

Up/Down Trend Test =
var a=sum(districts[Weekly Confirmed Cases])
var d=MAXX(districts,districts[Week])
var b=CALCULATE(sum(districts[Weekly Confirmed Cases]),districts[Week]=d-1)
var c=IF(a<b,"Down","Up")
return c
 
Now my requirement is i want to return the count of consecutive downs . if cases are decreasing constantly for few weeks it should cumulatively count the weeks , if there is any up in middle, it should stop and again when the next down comes it should start again from 1.
 
Please help me with above requirement.
 
Expected output
 
Sandeep2021_1-1625548950035.png

 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Try this:

StartWeek =
VAR ThisWeek =
    MAX ( districts[Week] )
VAR LastWeek = ThisWeek - 1
VAR ThisTrend = [Up/Down Trend Test]
VAR LastTrend =
    IF (
        ThisWeek > 1,
        CALCULATE ( [Up/Down Trend Test], districts[Week] = LastWeek )
    )
RETURN
    IF ( ThisTrend <> LastTrend, ThisWeek )
consecutive count =
VAR CalStartWeek =
    MAXX (
        FILTER (
            ALLSELECTED ( districts[Week] ),
            districts[Week] <= MAX ( districts[Week] )
        ),
        [StartWeek]
    )
VAR ThisWeek =
    MAX ( districts[Week] )
RETURN
    CALCULATE (
        COUNTROWS ( districts ),
        districts[Week] >= CalStartWeek
            && districts[Week] <= ThisWeek
    )

count.PNG

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Try this:

StartWeek =
VAR ThisWeek =
    MAX ( districts[Week] )
VAR LastWeek = ThisWeek - 1
VAR ThisTrend = [Up/Down Trend Test]
VAR LastTrend =
    IF (
        ThisWeek > 1,
        CALCULATE ( [Up/Down Trend Test], districts[Week] = LastWeek )
    )
RETURN
    IF ( ThisTrend <> LastTrend, ThisWeek )
consecutive count =
VAR CalStartWeek =
    MAXX (
        FILTER (
            ALLSELECTED ( districts[Week] ),
            districts[Week] <= MAX ( districts[Week] )
        ),
        [StartWeek]
    )
VAR ThisWeek =
    MAX ( districts[Week] )
RETURN
    CALCULATE (
        COUNTROWS ( districts ),
        districts[Week] >= CalStartWeek
            && districts[Week] <= ThisWeek
    )

count.PNG

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@Anonymous ,

a new colum=

var _last = maxx(filter(Table, [Week] =earlier([Week]) -1) ,[Weekly confirmed cases])

return

if([Weekly confirmed cases] >_last , "up", "down")

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

@amitchandak 

 

I cant use this for calculated column because i want these values to be changed based on selection in slicers. moreover the formula you suggested seems to return down/up , but i need to print number which gives consecutive downs

@Anonymous , Create week Rank if you have year week, or use week. But prefer to move week to a separate table

new column in date/week table

Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYWW format


measures  - use week in place week rank if needed


This Week = CALCULATE(sum('Table'[Weekly confirmed cases]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(sum('Table'[Weekly confirmed cases]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))

 

Status measure =

if([ThisWeek] >[Last Week], "up", "down")

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.