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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
nmamm
Frequent Visitor

metric by date range with multiple sequential observations per category

I have a data set which represents the occupancy of apartment units: 

 

nmamm_0-1675801376587.png

I would like to report on the number of occupied units per day, which I figure should be done in the following way:

for each unitID, get the record with the Max(Filter(Table, Occured Date<Current Date)) then count the records with a status of "Occupied", then plot that data over time against my date table. My date table is linked to the occured date.

 

For example, I would expect the # occupied units:

-as of 5/2/2022 to be 5

-as of 2/1/2023 to be 4 (Unit ID #5 has a more recent record dated 1/1/2023 with a Status = "Occupied - On Notice"

 

 

 

1 ACCEPTED SOLUTION
nmamm
Frequent Visitor

@Greg_Deckler I think I got it..

% Mkt Occ = 
VAR tmpunitStatus = ADDCOLUMNS(unitstatus,"Effective Date",IF(ISBLANK([End]),TODAY(),[End]))
VAR tmpTable =  
    FILTER(
        GENERATE(
               tmpunitStatus,
            CalendarTable
        ),
    
       and( And([Date] <= [End], [Date] >= [Start]),[Status]="Occupied")
    )

RETURN COUNTROWS(tmpTable)/DISTINCTCOUNT([UnitID])

 

I realized part of the issue was that the fact table was linked to the date table, which was forcing unwanted behavior. When I plot this by day, it produces expected results. However, if you try summarize the data by any other time dimension (particularly because it is rows of days that is used in the generate function) then I get sums of daily amounts (of course) which I don't want. Therefore, inspired by this I came up with the following solution:

% Mkt Occ (Current) = 
VAR tmpTable =  
    FILTER(
unitstatus,
       and( And(max(CalendarTable[Date]) <= [End], max(CalendarTable[Date]) >= [Start]),[Status]="Occupied")
    )

RETURN COUNTROWS(tmpTable)/DISTINCTCOUNT(unitstatus[UnitID])

Inspired by your suggested solution, dynamically filter the fact table based on the max date of the calendar table, which itself is dynamic based on the time dimension being plotted. 

View solution in original post

7 REPLIES 7
nmamm
Frequent Visitor

@Greg_Deckler thanks for your reply! I think you are on to something. Using the periodic billing file as an example, the complexity I have is that "Customer A" is repeated, and I only want to count some of their orders, and not others.

@nmamm Can you provide sample data to work with?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@nmamm Can you provide sample data to work with?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Where Start and End is equivalent to the example columns begin date and until date

UnitIDStatusOccuredStartEnd
1Occupied12/1/202112/1/202111/30/2022
2Occupied12/1/202112/1/202111/30/2022
3Occupied12/1/202112/1/202111/30/2022
4Occupied12/1/202112/1/202111/30/2022
1Occupied12/1/202212/1/2022 
2Occupied12/1/202212/1/2022 
3Occupied12/1/202212/1/2022 
4Occupied12/1/202212/1/2022 
5Occupied4/1/20214/1/20213/31/2022
5Occupied4/1/20224/1/202212/31/2022
5Occupied - On Notice1/1/20231/1/2023 
nmamm
Frequent Visitor

@Greg_Deckler I think I got it..

% Mkt Occ = 
VAR tmpunitStatus = ADDCOLUMNS(unitstatus,"Effective Date",IF(ISBLANK([End]),TODAY(),[End]))
VAR tmpTable =  
    FILTER(
        GENERATE(
               tmpunitStatus,
            CalendarTable
        ),
    
       and( And([Date] <= [End], [Date] >= [Start]),[Status]="Occupied")
    )

RETURN COUNTROWS(tmpTable)/DISTINCTCOUNT([UnitID])

 

I realized part of the issue was that the fact table was linked to the date table, which was forcing unwanted behavior. When I plot this by day, it produces expected results. However, if you try summarize the data by any other time dimension (particularly because it is rows of days that is used in the generate function) then I get sums of daily amounts (of course) which I don't want. Therefore, inspired by this I came up with the following solution:

% Mkt Occ (Current) = 
VAR tmpTable =  
    FILTER(
unitstatus,
       and( And(max(CalendarTable[Date]) <= [End], max(CalendarTable[Date]) >= [Start]),[Status]="Occupied")
    )

RETURN COUNTROWS(tmpTable)/DISTINCTCOUNT(unitstatus[UnitID])

Inspired by your suggested solution, dynamically filter the fact table based on the max date of the calendar table, which itself is dynamic based on the time dimension being plotted. 

@nmamm Outstanding!



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

@nmamm 
Take a look at these two Quick Measures as I think you want something like them.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Open-Tickets/m-p/409364
https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Billing/m-p/409365



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.