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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
bbui11
Frequent Visitor

Excluding Monthly Violations in Store Complaints Rolling Average DAX Calculation

Hi,

 

I have a requirement to create a limit value that determines whether the # of store complaints for a given month is considered a violation. The limit is calculated as a rolling average of prior months, but must exclude prior months that were violations.

 

As an example, the table below shows the store complaints for each month. The "Store Rolling Average Complaints Excluding Violations" value is what I am trying to get, but I run into a circular dependency error when attempting to exclude violations.

 

Store Complaints = # of complaints for the month

Store Rolling Average Complaints = The rolling average value that includes prior violation months. Just included it as a reference, since this rolling average calc is straightforward. 

Store Rolling Average Complaints Excluding Violations = The desired DAX measure, that calculates a rolling average value, but exclude prior violations.

Violations = Violation indicator for each month, and is marked as "Yes" when Store Complaints > Store Rolling Average Complaints Excluding Violations

 

*This needs to be a DAX measure because there are various filters that must be applied to the matrix

 

Please let me know if any addtl. info is needed. TIA.

 

 JanFebMarAprMayJunJulAugSepOctNovDec
Store Complaints63821151286815
Store Rolling Average Complaints 6.004.505.674.756.005.836.716.886.786.906.36
Store Rolling Average Complaints Excluding Violations 64.54.53.663.663.663.663.663.663.663
ViolationsNoNoYesNoYesYesYesYesYesYesNoYes
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @bbui11 ,

 

As far as I know, Dax doesn't support us to do looping.

I think you may try looping in Power Query. Here I have a test,  I hope it could help you.

 

let
    Source = Table.FromRecords({
        [Date=#date(2024, 1, 1), Complaints=6],
        [Date=#date(2024, 2, 1), Complaints=3],
        [Date=#date(2024, 3, 1), Complaints=8],
        [Date=#date(2024, 4, 1), Complaints=2],
        [Date=#date(2024, 5, 1), Complaints=11],
        [Date=#date(2024, 6, 1), Complaints=5],
        [Date=#date(2024, 7, 1), Complaints=12],
        [Date=#date(2024, 8, 1), Complaints=8],
        [Date=#date(2024, 9, 1), Complaints=6],
        [Date=#date(2024, 10, 1), Complaints=8],
        [Date=#date(2024, 11, 1), Complaints=1],
        [Date=#date(2024, 12, 1), Complaints=5]
    }),

    CalculateRollingAverage = (ComplaintsList as list, Index as number) as number =>
        let
            PreviousComplaints = List.FirstN(ComplaintsList, Index-1),
            ValidComplaints = List.Select(PreviousComplaints, each _ <= List.Average(PreviousComplaints)),
            RollingAverage = if List.Count(ValidComplaints) = 0 then 0 else List.Average(ValidComplaints)
        in
            RollingAverage
,
    AddRollingAverage = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    AddRollingAverageExcludingViolations = Table.AddColumn(AddRollingAverage, "Rolling Average Excluding Violations", each CalculateRollingAverage(AddRollingAverage[Complaints], [Index])),
    #"Changed Type" = Table.TransformColumnTypes(AddRollingAverageExcludingViolations,{{"Rolling Average Excluding Violations", Currency.Type}}),
    AddViolations = Table.AddColumn(#"Changed Type", "Violations", each if[Index] = 1 then "No" else if [Complaints] > [Rolling Average Excluding Violations] then "Yes" else "No"),
    #"Removed Columns" = Table.RemoveColumns(AddViolations,{"Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Rolling Average Excluding Violations", Int64.Type}})
in
    #"Changed Type1"

 

Result is as below.

vrzhoumsft_0-1725519041873.png

 

Best Regards,
Rico Zhou

 

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

1 REPLY 1
Anonymous
Not applicable

Hi @bbui11 ,

 

As far as I know, Dax doesn't support us to do looping.

I think you may try looping in Power Query. Here I have a test,  I hope it could help you.

 

let
    Source = Table.FromRecords({
        [Date=#date(2024, 1, 1), Complaints=6],
        [Date=#date(2024, 2, 1), Complaints=3],
        [Date=#date(2024, 3, 1), Complaints=8],
        [Date=#date(2024, 4, 1), Complaints=2],
        [Date=#date(2024, 5, 1), Complaints=11],
        [Date=#date(2024, 6, 1), Complaints=5],
        [Date=#date(2024, 7, 1), Complaints=12],
        [Date=#date(2024, 8, 1), Complaints=8],
        [Date=#date(2024, 9, 1), Complaints=6],
        [Date=#date(2024, 10, 1), Complaints=8],
        [Date=#date(2024, 11, 1), Complaints=1],
        [Date=#date(2024, 12, 1), Complaints=5]
    }),

    CalculateRollingAverage = (ComplaintsList as list, Index as number) as number =>
        let
            PreviousComplaints = List.FirstN(ComplaintsList, Index-1),
            ValidComplaints = List.Select(PreviousComplaints, each _ <= List.Average(PreviousComplaints)),
            RollingAverage = if List.Count(ValidComplaints) = 0 then 0 else List.Average(ValidComplaints)
        in
            RollingAverage
,
    AddRollingAverage = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    AddRollingAverageExcludingViolations = Table.AddColumn(AddRollingAverage, "Rolling Average Excluding Violations", each CalculateRollingAverage(AddRollingAverage[Complaints], [Index])),
    #"Changed Type" = Table.TransformColumnTypes(AddRollingAverageExcludingViolations,{{"Rolling Average Excluding Violations", Currency.Type}}),
    AddViolations = Table.AddColumn(#"Changed Type", "Violations", each if[Index] = 1 then "No" else if [Complaints] > [Rolling Average Excluding Violations] then "Yes" else "No"),
    #"Removed Columns" = Table.RemoveColumns(AddViolations,{"Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Rolling Average Excluding Violations", Int64.Type}})
in
    #"Changed Type1"

 

Result is as below.

vrzhoumsft_0-1725519041873.png

 

Best Regards,
Rico Zhou

 

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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.