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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
JoeCochran
Frequent Visitor

Count of Locations with Sum of Values between min-max values

Hope I can describe this problem correctly! I have 2 tables, not joined to each other:

 

FACT Table

LocationCategory ACategory BValue
1AZ51
1BX100
2AZ56
2CY105
3BW61
3DQ110
4AQ52
4AZ102

 

Min/Max Table:

MinMax
150160
160170
170180
...continuing pattern 

 

...with each table having many, many more rows. What I am trying to do is come up with a DAX formula that will allow me to build a table visual with the following output:

 

MinMaxCount of Locations with Sum of [Values] between min/max
1501602
1601701
1701801

 

The DAX I have tried for the "Count of Locations..." column is this:

Count of Locations =
var minV = MIN(GroupsTbl[Min])
var maxV = MAX(GroupsTbl[Max])
var sumValues = SUM(FactTbl[Value])

var locCount = CALCULATE(DISTINCTCOUNT(FactTbl[Location]),FILTER(FactTbl,sumValues>=minV),FILTER(FactTbl,sumValues<maxV))

return locCount

 

...but this returns an output that is not paritioned by the Location column. Something like this using the example above:

MinMaxCount of Locations with Sum of Values between min/max
6406504

 

This would be pretty easy to solve by pre-aggregating the table to remove the 2 "Category" columns - but I need to be able to filter with slicers on those columns, and have the count of locations (and sum of values) update based on the selected values from the slicers.

 

I hope this is enough explanation - apologies if not! Happy to provide more info/answer questions! 🙂

 

Any help would be greatly appreciated. Thanks -Joe

1 ACCEPTED SOLUTION
lperr
New Member

Hey Joe,

 

This formula should provide the desired output based on the details required: 

Count of Locations =
VAR MinValue = MIN(GroupsTbl[Min])
VAR MaxValue = MAX(GroupsTbl[Max])
RETURN
    COUNTROWS(
        FILTER(
            SUMMARIZE(
                FactTbl,
                FactTbl[Location],
                "LocationSum", SUM(FactTbl[Value])
            ),
            [LocationSum] >= MinValue && [LocationSum] < MaxValue
        )
    )

What This Formula Does

  1. SUMMARIZE:

    • This groups the FactTbl by Location and calculates the total Value for each location. The result is a temporary table with two columns: Location and LocationSum.
  2. FILTER:

    • It filters the summarized table to include only the rows where LocationSum falls between the Min and Max values from GroupsTbl.
  3. COUNTROWS:

    • Finally, it counts how many rows (locations) meet the filter criteria.

Why Use This Approach?

  • Pre-Aggregation: By summarizing the data upfront, the formula simplifies the calculation and reduces complexity.
  • Dynamic Filtering: It works seamlessly with slicers applied to Category A and Category B since it directly references FactTbl.
  • Performance: This method is faster and more efficient than alternatives like EARLIER or nested SUMX, especially when working with large datasets.

View solution in original post

3 REPLIES 3
JoeCochran
Frequent Visitor

Thanks to both for the help!!

techies
Super User
Super User

Hi @JoeCochran please try this

 

Location Count in Range =
VAR MinVal = SELECTEDVALUE(BandTable[Min])
VAR MaxVal = SELECTEDVALUE(BandTable[Max])
RETURN
CALCULATE(
    COUNTROWS(
        FILTER(
            ADDCOLUMNS(
                VALUES(Fact[Location]),
                "TotalValue", CALCULATE(SUM(Fact[Value]))
            ),
            [TotalValue] >= MinVal && [TotalValue] < MaxVal
        )
    )
)
 
techies_0-1747247602945.png

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
lperr
New Member

Hey Joe,

 

This formula should provide the desired output based on the details required: 

Count of Locations =
VAR MinValue = MIN(GroupsTbl[Min])
VAR MaxValue = MAX(GroupsTbl[Max])
RETURN
    COUNTROWS(
        FILTER(
            SUMMARIZE(
                FactTbl,
                FactTbl[Location],
                "LocationSum", SUM(FactTbl[Value])
            ),
            [LocationSum] >= MinValue && [LocationSum] < MaxValue
        )
    )

What This Formula Does

  1. SUMMARIZE:

    • This groups the FactTbl by Location and calculates the total Value for each location. The result is a temporary table with two columns: Location and LocationSum.
  2. FILTER:

    • It filters the summarized table to include only the rows where LocationSum falls between the Min and Max values from GroupsTbl.
  3. COUNTROWS:

    • Finally, it counts how many rows (locations) meet the filter criteria.

Why Use This Approach?

  • Pre-Aggregation: By summarizing the data upfront, the formula simplifies the calculation and reduces complexity.
  • Dynamic Filtering: It works seamlessly with slicers applied to Category A and Category B since it directly references FactTbl.
  • Performance: This method is faster and more efficient than alternatives like EARLIER or nested SUMX, especially when working with large datasets.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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