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
Rionick
Regular Visitor

DAX Formula to group / summarize until sum in other column is met

Hello!

 

Imagine this situation:

Pick Up WeekVolume
2023-393.45
2023-4516.47
2023-4616.01
2023-472.85
2023-4816.76
2023-4922.71
2023-510.67
2023-5211.29
2023-5322.36
2024-18.16
2024-210.06

 

Would need a calculated column which can return the first weeknum of the group based on the running total of the volume column. Each group can have the max sum of 63. If by adding the next row we go over 63 a new group should start. The result would be like this:

Pick Up WKVolumeRunning TotalGrouped Pick Up WK
2023-393.453.452023-39
2023-4516.4719.922023-39
2023-4616.0135.932023-39
2023-472.8538.782023-39
2023-4816.7655.542023-39
2023-4922.7122.712023-49
2023-510.6723.382023-49
2023-5211.2934.672023-49
2023-5322.3657.042023-49

 

Running total is just to show where the count should stop, I would need the Grouped Pick Up WK column only.

 

Any help aproaching this problem would be most welcome!

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @Rionick,

I add two more calculate columns in the table to help remark the start pickup week, you can take a look the following calculate column formulas:

WeekNumber =
VALUE (
    SUBSTITUTE ( [Pick Up Week], "-", IF ( LEN ( [Pick Up Week] ) < 7, "0", "" ) )
)

ModRolling =
VAR _offset = 60
VAR rolling =
    CALCULATE (
        SUM ( 'Table'[Volume] ),
        FILTER ( 'Table', 'Table'[WeekNumber] <= EARLIER ( 'Table'[WeekNumber] ) )
    )
RETURN
    MOD ( rolling, _offset )

Grouped Pick Up WK = 
VAR _end =
    MAXX (
        FILTER (
            'Table',
            [WeekNumber] <= EARLIER ( 'Table'[WeekNumber] )
                && [ModRolling] > EARLIER ( 'Table'[ModRolling] )
        ),
        [WeekNumber]
    )
RETURN
    MINX ( FILTER ( 'Table', [WeekNumber] > _end ), [Pick Up Week] )

1.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.

View solution in original post

1 REPLY 1
v-shex-msft
Community Support
Community Support

HI @Rionick,

I add two more calculate columns in the table to help remark the start pickup week, you can take a look the following calculate column formulas:

WeekNumber =
VALUE (
    SUBSTITUTE ( [Pick Up Week], "-", IF ( LEN ( [Pick Up Week] ) < 7, "0", "" ) )
)

ModRolling =
VAR _offset = 60
VAR rolling =
    CALCULATE (
        SUM ( 'Table'[Volume] ),
        FILTER ( 'Table', 'Table'[WeekNumber] <= EARLIER ( 'Table'[WeekNumber] ) )
    )
RETURN
    MOD ( rolling, _offset )

Grouped Pick Up WK = 
VAR _end =
    MAXX (
        FILTER (
            'Table',
            [WeekNumber] <= EARLIER ( 'Table'[WeekNumber] )
                && [ModRolling] > EARLIER ( 'Table'[ModRolling] )
        ),
        [WeekNumber]
    )
RETURN
    MINX ( FILTER ( 'Table', [WeekNumber] > _end ), [Pick Up Week] )

1.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.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.