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

Measure to calculate Running sum of n rows for each group

I have this data with Group and Values. I want to sum "Values" on a rolling window of 4 rows sorted by "Date". I want this calculation seperately for each "Group". 

 

DateGroupValueResult
2/1/202210 
2/2/202211 
2/3/202210 
2/4/2022112 (0+1+0+1)
2/5/2022113 (1+0+1+1)
2/1/202220 
2/2/202221 
2/3/202220 
2/4/2022212
2/5/2022213
1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

Value measure: = 
SUM( Data[Value] )

 

Four rows rolling sum: =
VAR fourlatestdays =
    TOPN (
        4,
        FILTER (
            ALL ( Data ),
            Data[Group] = MAX ( Data[Group] )
                && Data[Date] <= MAX ( Data[Date] )
        ),
        Data[Date], DESC
    )
VAR result =
    CALCULATE (
        [Value measure:],
        FILTER ( ALL ( Data ), Data[Group] = MAX ( Data[Group] ) ),
        fourlatestdays
    )
RETURN
    IF (
        HASONEVALUE ( Data[Date] ),
        IF ( COUNTROWS ( fourlatestdays ) < 4, BLANK (), result )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

Value measure: = 
SUM( Data[Value] )

 

Four rows rolling sum: =
VAR fourlatestdays =
    TOPN (
        4,
        FILTER (
            ALL ( Data ),
            Data[Group] = MAX ( Data[Group] )
                && Data[Date] <= MAX ( Data[Date] )
        ),
        Data[Date], DESC
    )
VAR result =
    CALCULATE (
        [Value measure:],
        FILTER ( ALL ( Data ), Data[Group] = MAX ( Data[Group] ) ),
        fourlatestdays
    )
RETURN
    IF (
        HASONEVALUE ( Data[Date] ),
        IF ( COUNTROWS ( fourlatestdays ) < 4, BLANK (), result )
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks for helping me out!

 

Now i am trying to use this rolling sum in a card visual,Can you also help me with my query below?


I am using a dashboard to track latest data from "Rolling_sum" column. This "Rolling_sum" column is Whole number data type. Whenever i am trying to use it as a card it shows aggregation. I just want to show the latest information without any aggregation just like any text column.

 

I have tried creating a new measure which duplicates the "Rolling_sum" column using "FIXED" to convert it into text. But, using it in a card dosent allow me to choose "First" or "Last" values.

 

ank47_0-1650121593889.png

 

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.