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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Cumulative count over time, with previous month addition and subtractions

I have tricky series of calculations that I just can't get right, 

 

I have a table with an ID, opening date and closing date for stores

I then have a more updated table with just opening dates

and the same but for closings

Let's call the tables: Stores, Openings, Closings.

 

I want to create a matrix that for a single selected year shows me the following:

 JanuaryFebruaryMarchAprilMayJune
Active199198200200199199
Opened 2 1 1
Closed-1  -2  
Total198200200199199200

 

I've managed to create a measure that correcly shows me the current active per year/month, without considering the opened/closed from the other two tables. 

"Total" would easily be "Active + Openings + Closed"

Which I've then added to a new measure to get these from the previous month, so now that trails correctly. 

 

But now the issue is how do I get the "Total" to also get the new Active? which is dependent on total? 😄

 

Edit: it didn't actually really work as intended, 

my "active with openings&closings" that checks the total from previous month isn't properly working... 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I got it working, forgot to update here: using the following measures, and having them as rows in a matrix is possible when enabling the option "Switch values to rows" under visual - values - options 🙂  

 

cumulative active:
VAR SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR CurrentMonth = MAX('DateTable'[Month])

VAR PreviousActive =
    CALCULATE(
        [Active],
        FILTER(
            ALL('DateTable'), 
            'DateTable'[Month] = CurrentMonth - 1 &&
            'DateTable'[Year] = SelectedYear
        )
    )

RETURN
PreviousActive + 
CALCULATE(
    [Openings] + [Closings (negative)],
    FILTER(
        ALL('DateTable'), 
        'DateTable'[Month] < CurrentMonth &&
        'DateTable'[Year] = SelectedYear
    )

 

Running Total = 
VAR SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR CurrentMonth = MAX('DateTable'[Month])

RETURN
CALCULATE(
    [Active] + 
    SUMX(
        FILTER(ALL('DateTable'), 'DateTable'[Month] <= CurrentMonth &&
          'DateTable'[Year] = SelectedYear),
        [Openings] + [Closings (negative)]
    )
)

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

Based on the testing, creating the new table for matrix row and creating the relationship between tables.

vjiewumsft_0-1739950893993.png

Then, try using the following DAX formula to calculate the store number for each month.

Active = 
CALCULATE(
    COUNTROWS(Store),
    FILTER(
        ALLEXCEPT(Store, Store[Month]),
        Store[opening date] >= MIN('Table'[Date]) &&
        (ISBLANK(Store[closing date]) || Store[closing date] < MAX('Table'[Date]))
    )
)
Opened = 
CALCULATE(
    COUNTROWS(Openings),
    FILTER(
        ALLEXCEPT(Openings, Openings[Month]),
        Openings[Opening Date] <= MAX('Table'[Date]) &&
        Openings[Opening Date] > MIN('Table'[Date])
    )
) + 0
Closed = 
CALCULATE(
    -COUNTROWS(Closings),
    FILTER(
        ALLEXCEPT(Closings, Closings[Month]),
        Closings[closing date] <= MAX('Table'[Date]) &&
        Closings[closing date] > MIN('Table'[Date])
    )
) + 0
Measure switch = 
SWITCH(
    TRUE(),
    SELECTEDVALUE('Table statu'[status]) = "Active", [Active],
    SELECTEDVALUE('Table statu'[status]) = "Openings", [Opened],
    SELECTEDVALUE('Table statu'[status]) = "Closings", [Closed],
    [Active] + [Closed] + [Opened]
)

Drag the status column to the matrix row, drag the month column to the matrix column and drag the switch measure to the values.

vjiewumsft_1-1739950904386.png

vjiewumsft_2-1739950967308.png

 

Best Regards,

Wisdom Wu

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

Anonymous
Not applicable

@Anonymous thanks for the reply and assist!

 

The active measure doesn't give me the full number of active stores for that month when I filter on a year, only the ones with an opening date that year-month. So I need it to be cumulative up until the first date of selected year for it to work properly I think. And same with closing dates, I want to not count closing dates for selected year, but I want all closing dates before the selected year to be counted in so I get the correct amount of open stores. 

 

And maybe I misunderstand the switch measure, but I don't have a "status" column sadly. And I think that would also need to be calculated in some way since it depends on what date you're looking at the store? 

Anonymous
Not applicable

Hi ,

If you want below result to show up in the matrix, you need to create columns containing active, openings, closings as the measure can't be put inside the matrix rows. You could use the ALL function to remove the year filter, and then use <= to determine all dates before the selected year.

vjiewumsft_0-1740040301522.png

Best Regards,

Wisdom Wu

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

 

Anonymous
Not applicable

I got it working, forgot to update here: using the following measures, and having them as rows in a matrix is possible when enabling the option "Switch values to rows" under visual - values - options 🙂  

 

cumulative active:
VAR SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR CurrentMonth = MAX('DateTable'[Month])

VAR PreviousActive =
    CALCULATE(
        [Active],
        FILTER(
            ALL('DateTable'), 
            'DateTable'[Month] = CurrentMonth - 1 &&
            'DateTable'[Year] = SelectedYear
        )
    )

RETURN
PreviousActive + 
CALCULATE(
    [Openings] + [Closings (negative)],
    FILTER(
        ALL('DateTable'), 
        'DateTable'[Month] < CurrentMonth &&
        'DateTable'[Year] = SelectedYear
    )

 

Running Total = 
VAR SelectedYear = SELECTEDVALUE('DateTable'[Year])
VAR CurrentMonth = MAX('DateTable'[Month])

RETURN
CALCULATE(
    [Active] + 
    SUMX(
        FILTER(ALL('DateTable'), 'DateTable'[Month] <= CurrentMonth &&
          'DateTable'[Year] = SelectedYear),
        [Openings] + [Closings (negative)]
    )
)

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors